[njs] Fixed truth value of JSON numbers in JSON.parse().

Dmitry Volyntsev xeioex at nginx.com
Tue Jun 11 14:35:15 UTC 2019


details:   https://hg.nginx.org/njs/rev/028277f5c551
branches:  
changeset: 1004:028277f5c551
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jun 11 17:34:59 2019 +0300
description:
Fixed truth value of JSON numbers in JSON.parse().

This closes #180 issue on Github.

diffstat:

 njs/njs_json.c           |   3 ++-
 njs/test/njs_unit_test.c |  24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletions(-)

diffs (48 lines):

diff -r c46dca06edb1 -r 028277f5c551 njs/njs_json.c
--- a/njs/njs_json.c	Mon Jun 10 21:40:11 2019 -0400
+++ b/njs/njs_json.c	Tue Jun 11 17:34:59 2019 +0300
@@ -876,8 +876,9 @@ njs_json_parse_number(njs_json_parse_ctx
     start = p;
     num = njs_number_dec_parse(&p, ctx->end);
     if (p != start) {
-        *value = njs_value_zero;
         value->data.u.number = sign * num;
+        value->type = NJS_NUMBER;
+        value->data.truth = njs_is_number_true(num);
 
         return p;
     }
diff -r c46dca06edb1 -r 028277f5c551 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Jun 10 21:40:11 2019 -0400
+++ b/njs/test/njs_unit_test.c	Tue Jun 11 17:34:59 2019 +0300
@@ -11724,6 +11724,30 @@ static njs_unit_test_t  njs_test[] =
                  "o.b = 3; o.b"),
       nxt_string("3") },
 
+    { nxt_string("JSON.parse('2') || 10"),
+      nxt_string("2") },
+
+    { nxt_string("JSON.parse('0') || 10"),
+      nxt_string("10") },
+
+    { nxt_string("JSON.parse('-0') || 10"),
+      nxt_string("10") },
+
+    { nxt_string("JSON.parse('\"a\"') || 10"),
+      nxt_string("a") },
+
+    { nxt_string("JSON.parse('\"\"') || 10"),
+      nxt_string("10") },
+
+    { nxt_string("JSON.parse('true') || 10"),
+      nxt_string("true") },
+
+    { nxt_string("JSON.parse('false') || 10"),
+      nxt_string("10") },
+
+    { nxt_string("JSON.parse('null') || 10"),
+      nxt_string("10") },
+
     { nxt_string("var o = JSON.parse('{}', function(k, v) {return v;}); o"),
       nxt_string("[object Object]") },
 


More information about the nginx-devel mailing list