[njs] Math.trunc() method.

Valentin Bartenev vbart at nginx.com
Mon Oct 31 13:29:20 UTC 2016


details:   http://hg.nginx.org/njs/rev/1f68ffabcea9
branches:  
changeset: 232:1f68ffabcea9
user:      Valentin Bartenev <vbart at nginx.com>
date:      Mon Oct 31 16:25:54 2016 +0300
description:
Math.trunc() method.

diffstat:

 njs/njs_math.c           |  27 +++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  30 ++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)

diffs (84 lines):

diff -r b0b46036d54e -r 1f68ffabcea9 njs/njs_math.c
--- a/njs/njs_math.c	Mon Oct 31 15:56:49 2016 +0300
+++ b/njs/njs_math.c	Mon Oct 31 16:25:54 2016 +0300
@@ -390,6 +390,25 @@ njs_object_math_tan(njs_vm_t *vm, njs_va
 }
 
 
+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
         .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 -r b0b46036d54e -r 1f68ffabcea9 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Oct 31 15:56:49 2016 +0300
+++ b/njs/test/njs_unit_test.c	Mon Oct 31 16:25:54 2016 +0300
@@ -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"),



More information about the nginx-devel mailing list