From 115ad9a41ce22811d0ed99c5fbca0e963fef634c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Sun, 8 Sep 2019 23:49:27 +0200 Subject: [PATCH] Fix JSON.stringify for non-serializable properties (#3083) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3082. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- .../ecma/builtin-objects/ecma-builtin-json.c | 21 +++++---- tests/jerry/regression-test-issue-3082.js | 45 +++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 tests/jerry/regression-test-issue-3082.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c index b5e47b3d9..d84d68593 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c @@ -950,8 +950,8 @@ ecma_builtin_json_serialize_object (ecma_json_stringify_context_t *context_p, /* /* ecma_builtin_json_serialize_property already appended the result. */ JERRY_ASSERT (ecma_is_value_empty (result)); - last_prop = ecma_stringbuilder_get_size (&context_p->result_builder); ecma_stringbuilder_append_byte (&context_p->result_builder, LIT_CHAR_COMMA); + last_prop = ecma_stringbuilder_get_size (&context_p->result_builder); } else { @@ -962,15 +962,18 @@ ecma_builtin_json_serialize_object (ecma_json_stringify_context_t *context_p, /* ecma_value_p = ecma_collection_iterator_next (ecma_value_p); } - /* Remove the last comma. */ - ecma_stringbuilder_revert (&context_p->result_builder, last_prop); - - if (last_prop != left_brace && has_gap) + if (last_prop != left_brace) { - /* We appended at least one element, and have a separator, so must append the stepback. */ - ecma_stringbuilder_append_raw (&context_p->result_builder, - ecma_stringbuilder_get_data (&context_p->indent_builder), - stepback_size); + /* Remove the last comma. */ + ecma_stringbuilder_revert (&context_p->result_builder, last_prop - 1); + + if (has_gap) + { + /* We appended at least one element, and have a separator, so must append the stepback. */ + ecma_stringbuilder_append_raw (&context_p->result_builder, + ecma_stringbuilder_get_data (&context_p->indent_builder), + stepback_size); + } } ecma_stringbuilder_append_byte (&context_p->result_builder, LIT_CHAR_RIGHT_BRACE); diff --git a/tests/jerry/regression-test-issue-3082.js b/tests/jerry/regression-test-issue-3082.js new file mode 100644 index 000000000..a339a0ae1 --- /dev/null +++ b/tests/jerry/regression-test-issue-3082.js @@ -0,0 +1,45 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var obj = { + a: "str", + b: function() {}, + c: true +} + +assert (JSON.stringify (obj) === "{\"a\":\"str\",\"c\":true}"); +assert (JSON.stringify (obj, null, 2) === "{\n \"a\": \"str\",\n \"c\": true\n}"); + +var obj = { + f: function() {} +} + +assert (JSON.stringify (obj) === "{}"); +assert (JSON.stringify (obj, null, 2) === "{}"); + +var obj = { + f: function() {}, + a: null +} + +assert (JSON.stringify (obj) === "{\"a\":null}"); +assert (JSON.stringify (obj, null, 2) === "{\n \"a\": null\n}"); + +var obj = { + a: false, + f: function() {} +} + +assert (JSON.stringify (obj) === "{\"a\":false}"); +assert (JSON.stringify (obj, null, 2) === "{\n \"a\": false\n}");