[njs] Fixed handling of Symbol values in JSON.stringify().

Dmitry Volyntsev xeioex at nginx.com
Sat Nov 23 11:29:18 UTC 2019


details:   https://hg.nginx.org/njs/rev/4367a0615234
branches:  
changeset: 1253:4367a0615234
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Sat Nov 23 01:02:04 2019 +0300
description:
Fixed handling of Symbol values in JSON.stringify().

diffstat:

 src/njs_json.c           |   3 +++
 src/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 15 insertions(+), 0 deletions(-)

diffs (63 lines):

diff -r 180b28e542b6 -r 4367a0615234 src/njs_json.c
--- a/src/njs_json.c	Sat Nov 23 00:09:26 2019 +0300
+++ b/src/njs_json.c	Sat Nov 23 01:02:04 2019 +0300
@@ -1128,6 +1128,7 @@ njs_json_pop_stringify_state(njs_json_st
 
 #define njs_json_is_object(value)                                             \
     (((value)->type == NJS_OBJECT)                                            \
+     || ((value)->type == NJS_OBJECT_SYMBOL)                                  \
      || ((value)->type == NJS_ARRAY)                                          \
      || ((value)->type >= NJS_REGEXP))
 
@@ -1211,6 +1212,7 @@ njs_json_stringify_iterator(njs_vm_t *vm
             }
 
             if (njs_is_undefined(value)
+                || njs_is_symbol(value)
                 || njs_is_function(value)
                 || !njs_is_valid(value))
             {
@@ -1559,6 +1561,7 @@ njs_json_append_value(njs_json_stringify
 
     case NJS_UNDEFINED:
     case NJS_NULL:
+    case NJS_SYMBOL:
     case NJS_INVALID:
     case NJS_FUNCTION:
     default:
diff -r 180b28e542b6 -r 4367a0615234 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Sat Nov 23 00:09:26 2019 +0300
+++ b/src/test/njs_unit_test.c	Sat Nov 23 01:02:04 2019 +0300
@@ -14264,6 +14264,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("JSON.stringify(undefined)"),
       njs_str("undefined") },
 
+    { njs_str("JSON.stringify(Symbol())"),
+      njs_str("undefined") },
+
     { njs_str("JSON.stringify({})"),
       njs_str("{}") },
 
@@ -14279,6 +14282,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("JSON.stringify({a:1, b:undefined})"),
       njs_str("{\"a\":1}") },
 
+    { njs_str("JSON.stringify({a:1, b:Symbol()})"),
+      njs_str("{\"a\":1}") },
+
     { njs_str("var o = {a:1, c:2};"
                  "Object.defineProperty(o, 'b', {enumerable:false, value:3});"
                  "JSON.stringify(o)"),
@@ -14290,6 +14296,12 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("JSON.stringify(RegExp())"),
       njs_str("{}") },
 
+    { njs_str("JSON.stringify(Object(Symbol()))"),
+      njs_str("{}") },
+
+    { njs_str("var s = Object(Symbol()); s.test = 'test'; JSON.stringify(s)"),
+      njs_str("{\"test\":\"test\"}") },
+
     { njs_str("JSON.stringify(SyntaxError('e'))"),
       njs_str("{}") },
 


More information about the nginx-devel mailing list