]> git.kaiwu.me - njs.git/commitdiff
Fixed toString() for -0.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 27 Aug 2018 13:06:33 +0000 (16:06 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 27 Aug 2018 13:06:33 +0000 (16:06 +0300)
njs/njs_vm.c
njs/njs_vm.h
njs/test/njs_unit_test.c
nxt/nxt_dtoa.c

index e60a5bb7724764c683730c18ee073df959505a13..a63378a62076b31fc3f163a2341346efd228bd82 100644 (file)
@@ -88,6 +88,7 @@ const njs_value_t  njs_string_boolean =     njs_string("boolean");
 const njs_value_t  njs_string_false =       njs_string("false");
 const njs_value_t  njs_string_true =        njs_string("true");
 const njs_value_t  njs_string_number =      njs_string("number");
+const njs_value_t  njs_string_minus_zero =  njs_string("-0");
 const njs_value_t  njs_string_minus_infinity =
                                             njs_string("-Infinity");
 const njs_value_t  njs_string_plus_infinity =
@@ -3310,7 +3311,16 @@ again:
             }
         }
 
-        ret = njs_primitive_value_to_string(vm, &value, &value);
+        if (nxt_slow_path((value.type == NJS_NUMBER
+                            && value.data.u.number == 0
+                            && signbit(value.data.u.number))))
+        {
+            value = njs_string_minus_zero;
+            ret = NXT_OK;
+
+        } else {
+            ret = njs_primitive_value_to_string(vm, &value, &value);
+        }
 
         if (nxt_fast_path(ret == NXT_OK)) {
             size = value.short_string.size;
index ea9619887d1af539b7595ce13c4cb1004a128191..ee25659803c53358266e6dc28396e2474f717dd6 100644 (file)
@@ -1269,6 +1269,7 @@ extern const njs_value_t  njs_string_null;
 extern const njs_value_t  njs_string_false;
 extern const njs_value_t  njs_string_true;
 extern const njs_value_t  njs_string_native;
+extern const njs_value_t  njs_string_minus_zero;
 extern const njs_value_t  njs_string_minus_infinity;
 extern const njs_value_t  njs_string_plus_infinity;
 extern const njs_value_t  njs_string_nan;
index 38c4fd6a3dcced2f0893e9012b5b4e8d0cc0b8ff..a42f0d755182f2e6d70cb08908fb7bed37068fd2 100644 (file)
@@ -420,6 +420,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("1 + 1 + '2' + 1 + 1"),
       nxt_string("2211") },
 
+    { nxt_string("'gg' + -0"),
+      nxt_string("gg0") },
+
     { nxt_string("1.2 - '5.7'"),
       nxt_string("-4.5") },
 
@@ -8976,6 +8979,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("JSON.stringify(123)"),
       nxt_string("123") },
 
+    { nxt_string("JSON.stringify(-0)"),
+      nxt_string("0") },
+
     { nxt_string("JSON.stringify(0.00000123)"),
       nxt_string("0.00000123") },
 
index 6453f87f495086d51565eecf16674d0fd0fd8db4..20ea2ebe43a2f179acb63de2b6b8d807ac96e99e 100644 (file)
@@ -346,18 +346,18 @@ nxt_dtoa(double value, char *start)
     minus = 0;
     p = start;
 
-    if (signbit(value)) {
-        *p++ = '-';
-        value = -value;
-        minus = 1;
-    }
-
     if (value == 0) {
         *p++ = '0';
 
         return (p - start);
     }
 
+    if (signbit(value)) {
+        *p++ = '-';
+        value = -value;
+        minus = 1;
+    }
+
     length = nxt_grisu2(value, p, &dec_exp);
 
     length = nxt_prettify(p, length, dec_exp);