From: Valentin Bartenev Date: Mon, 31 Oct 2016 13:28:12 +0000 (+0300) Subject: Math.sign() method. X-Git-Tag: 0.1.5~22 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=ef73841d573bb6962a1c463e6283f8adf59fe8da;p=njs.git Math.sign() method. --- diff --git a/njs/njs_math.c b/njs/njs_math.c index 72729975..ac6ccc9e 100644 --- a/njs/njs_math.c +++ b/njs/njs_math.c @@ -333,6 +333,29 @@ njs_object_math_round(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } +static njs_ret_t +njs_object_math_sign(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + + if (nargs > 1) { + num = args[1].data.u.number; + + if (!njs_is_nan(num) && num != 0) { + num = signbit(num) ? -1 : 1; + } + + } else { + num = NJS_NAN; + } + + njs_number_set(&vm->retval, num); + + return NXT_OK; +} + + static njs_ret_t njs_object_math_sin(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, njs_index_t unused) @@ -567,6 +590,14 @@ static const njs_object_prop_t njs_math_object_properties[] = NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("sign"), + .value = njs_native_function(njs_object_math_sign, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, + { .type = NJS_METHOD, .name = njs_string("sin"), diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index c0f4cf9e..dd996236 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -5373,6 +5373,24 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.pow()"), nxt_string("NaN") }, + { nxt_string("Math.sign(5)"), + nxt_string("1") }, + + { nxt_string("Math.sign(-5)"), + nxt_string("-1") }, + + { nxt_string("Math.sign(0)"), + nxt_string("0") }, + + { nxt_string("Math.sign(-0.0)"), + nxt_string("-0") }, + + { nxt_string("Math.sign(NaN)"), + nxt_string("NaN") }, + + { nxt_string("Math.sign()"), + nxt_string("NaN") }, + { nxt_string("Math.trunc(3.9)"), nxt_string("3") },