[njs] Fixed JSON.stringify() for arrays resizable via getters.

Alexander Borisov alexander.borisov at nginx.com
Tue Oct 6 17:52:32 UTC 2020


details:   https://hg.nginx.org/njs/rev/036a4fbf9204
branches:  
changeset: 1537:036a4fbf9204
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Tue Oct 06 20:51:56 2020 +0300
description:
Fixed JSON.stringify() for arrays resizable via getters.

diffstat:

 src/njs_json.c           |  3 ++-
 src/test/njs_unit_test.c |  4 ++++
 2 files changed, 6 insertions(+), 1 deletions(-)

diffs (34 lines):

diff -r da7b98611f57 -r 036a4fbf9204 src/njs_json.c
--- a/src/njs_json.c	Tue Oct 06 20:24:21 2020 +0300
+++ b/src/njs_json.c	Tue Oct 06 20:51:56 2020 +0300
@@ -1077,6 +1077,7 @@ njs_json_push_stringify_state(njs_vm_t *
     state->key = NULL;
 
     if (njs_is_fast_array(value)) {
+        state->length = njs_array_len(value);
         state->type = NJS_JSON_ARRAY;
         state->array = 1;
 
@@ -1279,7 +1280,7 @@ njs_json_stringify_iterator(njs_vm_t *vm
                 njs_json_stringify_indent(stringify, &chain, 0);
             }
 
-            if (state->index >= njs_array_len(&state->value)) {
+            if (state->index >= state->length) {
                 njs_json_stringify_indent(stringify, &chain, -1);
                 njs_chb_append_literal(&chain,"]");
 
diff -r da7b98611f57 -r 036a4fbf9204 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Oct 06 20:24:21 2020 +0300
+++ b/src/test/njs_unit_test.c	Tue Oct 06 20:51:56 2020 +0300
@@ -16855,6 +16855,10 @@ static njs_unit_test_t  njs_test[] =
               "JSON.stringify(array)"),
       njs_str("[1,{},10]") },
 
+    { njs_str("var array = [1];"
+              "array[1] = {get value() {array[10] = 10}}; JSON.stringify(array)"),
+      njs_str("[1,{}]") },
+
     /* njs.dump(). */
 
     { njs_str("njs.dump({a:1, b:[1,,2,{c:new Boolean(1)}]})"),


More information about the nginx-devel mailing list