[njs] Fixed toString() for -0.
Dmitry Volyntsev
xeioex at nginx.com
Mon Aug 27 13:55:46 UTC 2018
details: http://hg.nginx.org/njs/rev/7ce43874f706
branches:
changeset: 585:7ce43874f706
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Mon Aug 27 16:06:33 2018 +0300
description:
Fixed toString() for -0.
diffstat:
njs/njs_vm.c | 12 +++++++++++-
njs/njs_vm.h | 1 +
njs/test/njs_unit_test.c | 6 ++++++
nxt/nxt_dtoa.c | 12 ++++++------
4 files changed, 24 insertions(+), 7 deletions(-)
diffs (91 lines):
diff -r b15051895c8e -r 7ce43874f706 njs/njs_vm.c
--- a/njs/njs_vm.c Mon Aug 27 14:59:59 2018 +0300
+++ b/njs/njs_vm.c Mon Aug 27 16:06:33 2018 +0300
@@ -88,6 +88,7 @@ const njs_value_t 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;
diff -r b15051895c8e -r 7ce43874f706 njs/njs_vm.h
--- a/njs/njs_vm.h Mon Aug 27 14:59:59 2018 +0300
+++ b/njs/njs_vm.h Mon Aug 27 16:06:33 2018 +0300
@@ -1269,6 +1269,7 @@ extern const njs_value_t njs_string_nul
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;
diff -r b15051895c8e -r 7ce43874f706 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Aug 27 14:59:59 2018 +0300
+++ b/njs/test/njs_unit_test.c Mon Aug 27 16:06:33 2018 +0300
@@ -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") },
diff -r b15051895c8e -r 7ce43874f706 nxt/nxt_dtoa.c
--- a/nxt/nxt_dtoa.c Mon Aug 27 14:59:59 2018 +0300
+++ b/nxt/nxt_dtoa.c Mon Aug 27 16:06:33 2018 +0300
@@ -346,18 +346,18 @@ nxt_dtoa(double value, char *start)
minus = 0;
p = start;
+ if (value == 0) {
+ *p++ = '0';
+
+ return (p - start);
+ }
+
if (signbit(value)) {
*p++ = '-';
value = -value;
minus = 1;
}
- if (value == 0) {
- *p++ = '0';
-
- return (p - start);
- }
-
length = nxt_grisu2(value, p, &dec_exp);
length = nxt_prettify(p, length, dec_exp);
More information about the nginx-devel
mailing list