[njs] Fixed ToInteger function in accordance with the specification.

Alexander Borisov alexander.borisov at nginx.com
Mon Apr 15 14:45:48 UTC 2019


details:   https://hg.nginx.org/njs/rev/f0b5b01a7c55
branches:  
changeset: 893:f0b5b01a7c55
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Mon Apr 15 17:23:21 2019 +0300
description:
Fixed ToInteger function in accordance with the specification.

According to ES6 type conversion 7.1.4.

diffstat:

 njs/njs_array.c  |   4 ++--
 njs/njs_number.c |  22 ++++++++++++----------
 njs/njs_number.h |   4 ++--
 3 files changed, 16 insertions(+), 14 deletions(-)

diffs (91 lines):

diff -r d3743d248ade -r f0b5b01a7c55 njs/njs_array.c
--- a/njs/njs_array.c	Sat Apr 13 23:38:53 2019 +0800
+++ b/njs/njs_array.c	Mon Apr 15 17:23:21 2019 +0300
@@ -493,7 +493,7 @@ njs_array_prototype_slice_continuation(n
         return njs_trap(vm, NJS_TRAP_NUMBER_ARG);
     }
 
-    start = (int32_t) njs_primitive_value_to_integer(njs_arg(args, nargs, 1));
+    start = njs_primitive_value_to_integer(njs_arg(args, nargs, 1));
     length = njs_primitive_value_to_integer(&slice->length);
 
     if (start < 0) {
@@ -510,7 +510,7 @@ njs_array_prototype_slice_continuation(n
 
     } else {
         if (!njs_is_undefined(njs_arg(args, nargs, 2))) {
-            end = (int32_t) njs_primitive_value_to_integer(&args[2]);
+            end = njs_primitive_value_to_integer(&args[2]);
 
         } else {
             end = length;
diff -r d3743d248ade -r f0b5b01a7c55 njs/njs_number.c
--- a/njs/njs_number.c	Sat Apr 13 23:38:53 2019 +0800
+++ b/njs/njs_number.c	Mon Apr 15 17:23:21 2019 +0300
@@ -73,7 +73,7 @@ njs_primitive_value_to_number(const njs_
 }
 
 
-uint32_t
+int32_t
 njs_primitive_value_to_integer(const njs_value_t *value)
 {
     return njs_number_to_integer(njs_primitive_value_to_number(value));
@@ -783,11 +783,9 @@ njs_number_parse_float(njs_vm_t *vm, njs
 }
 
 
-nxt_noinline uint32_t
-njs_number_to_integer(double num)
+nxt_inline int64_t
+njs_number_to_int64(double num)
 {
-    int64_t  i64;
-
 #if (NXT_NAN_TO_UINT_CONVERSION != 0)
     /*
      * PPC32: NaN and Inf are converted to 0x8000000080000000
@@ -810,13 +808,17 @@ njs_number_to_integer(double num)
      */
 
     if (fabs(num) > 9007199254740992.0) {
-        i64 = fmod(num, 4294967296.0);
-
-    } else {
-        i64 = num;
+        return (int64_t) fmod(num, 4294967296.0);
     }
 
-    return (uint32_t) i64;
+    return (int64_t) num;
+}
+
+
+nxt_noinline int32_t
+njs_number_to_integer(double num)
+{
+    return (int32_t) njs_number_to_int64(num);
 }
 
 
diff -r d3743d248ade -r f0b5b01a7c55 njs/njs_number.h
--- a/njs/njs_number.h	Sat Apr 13 23:38:53 2019 +0800
+++ b/njs/njs_number.h	Mon Apr 15 17:23:21 2019 +0300
@@ -13,7 +13,7 @@
 
 uint32_t njs_value_to_index(const njs_value_t *value);
 double njs_primitive_value_to_number(const njs_value_t *value);
-uint32_t njs_primitive_value_to_integer(const njs_value_t *value);
+int32_t njs_primitive_value_to_integer(const njs_value_t *value);
 double njs_number_dec_parse(const u_char **start, const u_char *end);
 uint64_t njs_number_oct_parse(const u_char **start, const u_char *end);
 uint64_t njs_number_bin_parse(const u_char **start, const u_char *end);
@@ -32,7 +32,7 @@ njs_ret_t njs_number_parse_int(njs_vm_t 
     nxt_uint_t nargs, njs_index_t unused);
 njs_ret_t njs_number_parse_float(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
-nxt_noinline uint32_t njs_number_to_integer(double num);
+nxt_noinline int32_t njs_number_to_integer(double num);
 
 
 nxt_inline nxt_int_t


More information about the nginx-devel mailing list