[njs] Added externals support in JSON.stringify().

Dmitry Volyntsev xeioex at nginx.com
Mon Nov 25 14:54:27 UTC 2019


details:   https://hg.nginx.org/njs/rev/3eb134dfa896
branches:  
changeset: 1255:3eb134dfa896
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Nov 25 17:53:19 2019 +0300
description:
Added externals support in JSON.stringify().

diffstat:

 src/njs_json.c           |  10 +++-------
 src/test/njs_unit_test.c |   5 +++--
 2 files changed, 6 insertions(+), 9 deletions(-)

diffs (42 lines):

diff -r cb81e4469b2c -r 3eb134dfa896 src/njs_json.c
--- a/src/njs_json.c	Sat Nov 23 12:52:06 2019 +0300
+++ b/src/njs_json.c	Mon Nov 25 17:53:19 2019 +0300
@@ -1092,13 +1092,8 @@ njs_json_push_stringify_state(njs_vm_t *
             state->keys = njs_array(&stringify->replacer);
 
         } else {
-            if (njs_is_external(value)) {
-                state->keys = njs_extern_keys_array(vm, value->external.proto);
-
-            } else {
-                state->keys = njs_value_own_enumerate(vm, value, NJS_ENUM_KEYS,
-                                                      stringify->keys_type, 0);
-            }
+            state->keys = njs_value_own_enumerate(vm, value, NJS_ENUM_KEYS,
+                                                  stringify->keys_type, 0);
 
             if (njs_slow_path(state->keys == NULL)) {
                 return NULL;
@@ -1129,6 +1124,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_EXTERNAL)                                       \
      || ((value)->type == NJS_ARRAY)                                          \
      || ((value)->type >= NJS_REGEXP))
 
diff -r cb81e4469b2c -r 3eb134dfa896 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Sat Nov 23 12:52:06 2019 +0300
+++ b/src/test/njs_unit_test.c	Mon Nov 25 17:53:19 2019 +0300
@@ -14317,8 +14317,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var e = URIError('e'); e.foo = 'E'; JSON.stringify(e)"),
       njs_str("{\"foo\":\"E\"}") },
 
-    { njs_str("JSON.stringify([$r])"),
-      njs_str("[null]") },
+    { njs_str("var r = JSON.parse(JSON.stringify($r));"
+              "[r.uri, r.host, r.props.a, njs.dump(r.vars), njs.dump(r.consts), r.header['02']]"),
+      njs_str("АБВ,АБВГДЕЁЖЗИЙ,1,{},{},02|АБВ") },
 
     { njs_str("JSON.stringify({get key() {throw new Error('Oops')}})"),
       njs_str("Error: Oops") },


More information about the nginx-devel mailing list