[njs] Fixed JSON.stringify() for objects inherited from Object prototype.

Dmitry Volyntsev xeioex at nginx.com
Fri Nov 17 16:06:04 UTC 2017


details:   http://hg.nginx.org/njs/rev/a6af47aab3f2
branches:  
changeset: 419:a6af47aab3f2
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Nov 17 18:55:07 2017 +0300
description:
Fixed JSON.stringify() for objects inherited from Object prototype.

diffstat:

 njs/njs_json.c           |  12 +++++++-----
 njs/test/njs_unit_test.c |   3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)

diffs (56 lines):

diff -r 06aadeb164a3 -r a6af47aab3f2 njs/njs_json.c
--- a/njs/njs_json.c	Mon Oct 09 20:37:02 2017 +0300
+++ b/njs/njs_json.c	Fri Nov 17 18:55:07 2017 +0300
@@ -1162,8 +1162,10 @@ njs_json_parse_exception(njs_json_parse_
 }
 
 
-#define njs_is_object_or_array(value)                                         \
-    (((value)->type == NJS_OBJECT) || ((value)->type == NJS_ARRAY))
+#define njs_json_is_object(value)                                             \
+    (((value)->type == NJS_OBJECT)                                            \
+     || ((value)->type == NJS_ARRAY)                                          \
+     || ((value)->type >= NJS_REGEXP))
 
 
 #define njs_json_stringify_append(str, len)                                   \
@@ -1280,7 +1282,7 @@ njs_json_stringify_continuation(njs_vm_t
 
             njs_json_stringify_append_key(&prop->name);
 
-            if (njs_is_object_or_array(&prop->value)) {
+            if (njs_json_is_object(&prop->value)) {
                 state = njs_json_push_stringify_state(vm, stringify,
                                                       &prop->value);
                 if (state == NULL) {
@@ -1371,7 +1373,7 @@ njs_json_stringify_continuation(njs_vm_t
                 return njs_json_stringify_replacer(vm, stringify, NULL, value);
             }
 
-            if (njs_is_object_or_array(value)) {
+            if (njs_json_is_object(value)) {
                 state = njs_json_push_stringify_state(vm, stringify, value);
                 if (state == NULL) {
                     return NXT_ERROR;
@@ -1397,7 +1399,7 @@ njs_json_stringify_continuation(njs_vm_t
         case NJS_JSON_ARRAY_REPLACED:
             state->type = NJS_JSON_ARRAY_CONTINUE;
 
-            if (njs_is_object_or_array(&stringify->retval)) {
+            if (njs_json_is_object(&stringify->retval)) {
                 state = njs_json_push_stringify_state(vm, stringify,
                                                       &stringify->retval);
                 if (state == NULL) {
diff -r 06aadeb164a3 -r a6af47aab3f2 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Oct 09 20:37:02 2017 +0300
+++ b/njs/test/njs_unit_test.c	Fri Nov 17 18:55:07 2017 +0300
@@ -8199,6 +8199,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("JSON.stringify({a:{}, b:[function(v){}]})"),
       nxt_string("{\"a\":{},\"b\":[null]}") },
 
+    { nxt_string("JSON.stringify(RegExp())"),
+      nxt_string("{}") },
+
     /* Ignoring named properties of an array. */
 
     { nxt_string("var a = [1,2]; a.a = 1;"


More information about the nginx-devel mailing list