From: Valentin Bartenev Date: Mon, 31 Oct 2016 13:25:54 +0000 (+0300) Subject: Math.trunc() method. X-Git-Tag: 0.1.5~23 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=01b7fb7d9ee276a7f576a9262e469b4c42ef479c;p=njs.git Math.trunc() method. --- diff --git a/njs/njs_math.c b/njs/njs_math.c index 42fbdb33..72729975 100644 --- a/njs/njs_math.c +++ b/njs/njs_math.c @@ -390,6 +390,25 @@ njs_object_math_tan(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, } +static njs_ret_t +njs_object_math_trunc(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; + + } else { + num = NJS_NAN; + } + + njs_number_set(&vm->retval, trunc(num)); + + return NXT_OK; +} + + static const njs_object_prop_t njs_math_object_properties[] = { { @@ -568,6 +587,14 @@ static const njs_object_prop_t njs_math_object_properties[] = .value = njs_native_function(njs_object_math_tan, 0, NJS_SKIP_ARG, NJS_NUMBER_ARG), }, + + /* ES6. */ + { + .type = NJS_METHOD, + .name = njs_string("trunc"), + .value = njs_native_function(njs_object_math_trunc, 0, + NJS_SKIP_ARG, NJS_NUMBER_ARG), + }, }; diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index ae73d240..c0f4cf9e 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -5373,6 +5373,36 @@ static njs_unit_test_t njs_test[] = { nxt_string("Math.pow()"), nxt_string("NaN") }, + { nxt_string("Math.trunc(3.9)"), + nxt_string("3") }, + + { nxt_string("Math.trunc(-3.9)"), + nxt_string("-3") }, + + { nxt_string("Math.trunc(0)"), + nxt_string("0") }, + + { nxt_string("Math.trunc(-0)"), + nxt_string("-0") }, + + { nxt_string("Math.trunc(0.9)"), + nxt_string("0") }, + + { nxt_string("Math.trunc(-0.9)"), + nxt_string("-0") }, + + { nxt_string("Math.trunc(Infinity)"), + nxt_string("Infinity") }, + + { nxt_string("Math.trunc(-Infinity)"), + nxt_string("-Infinity") }, + + { nxt_string("Math.trunc(NaN)"), + nxt_string("NaN") }, + + { nxt_string("Math.trunc()"), + nxt_string("NaN") }, + /* ES5FIX: "[object Math]". */ { nxt_string("Math"),