[njs] Added signed zero support in njs.dump().
Dmitry Volyntsev
xeioex at nginx.com
Mon Jun 24 23:37:30 UTC 2019
details: https://hg.nginx.org/njs/rev/892680a2eae0
branches:
changeset: 1015:892680a2eae0
user: Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date: Tue Jun 25 00:06:12 2019 +0300
description:
Added signed zero support in njs.dump().
This closes #184 issue on Github.
diffstat:
njs/njs_json.c | 18 ++++++++++++++++++
njs/test/njs_unit_test.c | 9 +++++++++
2 files changed, 27 insertions(+), 0 deletions(-)
diffs (54 lines):
diff -r 56dee6c2ad95 -r 892680a2eae0 njs/njs_json.c
--- a/njs/njs_json.c Sat Jun 22 23:35:52 2019 +0300
+++ b/njs/njs_json.c Tue Jun 25 00:06:12 2019 +0300
@@ -2166,6 +2166,14 @@ njs_dump_value(njs_json_stringify_t *str
case NJS_OBJECT_NUMBER:
value = &value->data.u.object_value->value;
+ if (nxt_slow_path(value->data.u.number == 0.0
+ && signbit(value->data.u.number)))
+ {
+
+ njs_dump("[Number: -0]");
+ break;
+ }
+
ret = njs_number_to_string(stringify->vm, &str_val, value);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
@@ -2273,6 +2281,16 @@ njs_dump_value(njs_json_stringify_t *str
return njs_json_buf_append(stringify, "]}", 2);
case NJS_NUMBER:
+ if (nxt_slow_path(value->data.u.number == 0.0
+ && signbit(value->data.u.number)))
+ {
+
+ njs_dump("-0");
+ break;
+ }
+
+ /* Fall through. */
+
case NJS_REGEXP:
case NJS_DATE:
case NJS_OBJECT_ERROR:
diff -r 56dee6c2ad95 -r 892680a2eae0 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Sat Jun 22 23:35:52 2019 +0300
+++ b/njs/test/njs_unit_test.c Tue Jun 25 00:06:12 2019 +0300
@@ -12470,6 +12470,15 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("njs.dump(njs) == `{version:'${njs.version}'}`"),
nxt_string("true") },
+ { nxt_string("njs.dump(-0)"),
+ nxt_string("-0") },
+
+ { nxt_string("njs.dump(Object(-0))"),
+ nxt_string("[Number: -0]") },
+
+ { nxt_string("njs.dump([0, -0])"),
+ nxt_string("[0,-0]") },
+
/* Built-in methods name. */
{ nxt_string(
More information about the nginx-devel
mailing list