From e4e2a809f92d7da2f56b4c8f3c5c323e4d22f8b3 Mon Sep 17 00:00:00 2001 From: Alexander Borisov Date: Tue, 6 Oct 2020 20:51:56 +0300 Subject: [PATCH] Fixed JSON.stringify() for arrays resizable via getters. --- src/njs_json.c | 3 ++- src/test/njs_unit_test.c | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/njs_json.c b/src/njs_json.c index 2a2e4dd8..dd886425 100644 --- a/src/njs_json.c +++ b/src/njs_json.c @@ -1077,6 +1077,7 @@ njs_json_push_stringify_state(njs_vm_t *vm, njs_json_stringify_t *stringify, 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_t *stringify, 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 --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 3cdcf173..f79e2b9e 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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)}]})"), -- 2.47.3