From 9134cb7c8cce7d29b64ee40afdda9b430137564b Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 17 Nov 2017 18:55:07 +0300 Subject: [PATCH] Fixed JSON.stringify() for objects inherited from Object prototype. --- njs/njs_json.c | 12 +++++++----- njs/test/njs_unit_test.c | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/njs/njs_json.c b/njs/njs_json.c index 4842052c..e07bc18a 100644 --- a/njs/njs_json.c +++ b/njs/njs_json.c @@ -1162,8 +1162,10 @@ njs_json_parse_exception(njs_json_parse_ctx_t *ctx, const char* msg, } -#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 *vm, njs_value_t *args, 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 *vm, njs_value_t *args, 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 *vm, njs_value_t *args, 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 --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 69caedd4..6a2385ec 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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;" -- 2.47.3