From d58c930e7e6f7c55ffefa2ad593bcca30c1864ad Mon Sep 17 00:00:00 2001 From: Rafal Walczyna Date: Fri, 26 Jun 2020 14:00:18 +0200 Subject: [PATCH] Update Symbol.split functions for String and Regexp to match new standard In newest ES standard limit parameter is calculated using ToUint32 function instead of ToLength. JerryScript-DCO-1.0-Signed-off-by: Rafal Walczyna r.walczyna@samsung.com --- .../ecma-builtin-string-prototype.c | 5 ++++- .../ecma/operations/ecma-regexp-object.c | 11 +++++++++- tests/jerry/es.next/regexp-prototype-split.js | 20 +++++++++++++++++++ tests/jerry/es.next/string-prototype-split.js | 4 ++-- tests/jerry/es.next/symbol-split.js | 11 ++++++++++ tests/test262-es6-excludelist.xml | 2 +- 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tests/jerry/es.next/regexp-prototype-split.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c index eaada42904..89f2534900 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c @@ -795,10 +795,13 @@ ecma_builtin_string_prototype_object_split (ecma_value_t this_value, /**< this a uint32_t limit = UINT32_MAX; if (!ecma_is_value_undefined (limit_value)) { - if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (limit_value, &limit))) + /* ECMA-262 v11, 21.1.3.20 6 */ + ecma_number_t num; + if (ECMA_IS_VALUE_ERROR (ecma_get_number (limit_value, &num))) { goto cleanup_string; } + limit = ecma_number_to_uint32 (num); } /* 12. */ diff --git a/jerry-core/ecma/operations/ecma-regexp-object.c b/jerry-core/ecma/operations/ecma-regexp-object.c index a6ee37a70f..7e8b35737f 100644 --- a/jerry-core/ecma/operations/ecma-regexp-object.c +++ b/jerry-core/ecma/operations/ecma-regexp-object.c @@ -2113,10 +2113,13 @@ ecma_regexp_split_helper (ecma_value_t this_arg, /**< this value */ uint32_t limit = UINT32_MAX; if (!ecma_is_value_undefined (limit_arg)) { - if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (limit_arg, &limit))) + /* ECMA-262 v11, 21.2.5.13 13 */ + ecma_number_t num; + if (ECMA_IS_VALUE_ERROR (ecma_get_number (limit_arg, &num))) { goto cleanup_splitter; } + limit = ecma_number_to_uint32 (num); } /* 15. */ @@ -2215,6 +2218,12 @@ ecma_regexp_split_helper (ecma_value_t this_arg, /**< this value */ goto cleanup_array; } + /* ECMA-262 v11, 21.2.5.11 19.d.ii */ + if (end_index > string_length) + { + end_index = string_length; + } + /* 24.f.iii. */ if (previous_index == end_index) { diff --git a/tests/jerry/es.next/regexp-prototype-split.js b/tests/jerry/es.next/regexp-prototype-split.js new file mode 100644 index 0000000000..d6ee29dfb5 --- /dev/null +++ b/tests/jerry/es.next/regexp-prototype-split.js @@ -0,0 +1,20 @@ +// 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. + + +result = /./[Symbol.split]('string', -13); +assert(result.length === 7); + +result = /./[Symbol.split]('string', 2); +assert(result.length === 2); diff --git a/tests/jerry/es.next/string-prototype-split.js b/tests/jerry/es.next/string-prototype-split.js index be23a55f98..43ee26bd73 100644 --- a/tests/jerry/es.next/string-prototype-split.js +++ b/tests/jerry/es.next/string-prototype-split.js @@ -13,11 +13,11 @@ // limitations under the License. var str = "foo//bar/baz//foo"; -res = str.split("a", Infinity); +res = str.split("a", -1); assert (res.length === 3); assert (res[0] === "foo//b"); assert (res[1] === "r/b"); assert (res[2] === "z//foo"); -res = str.split(/\/\//, -1); +res = str.split(/\/\//, Infinity); assert (res.length === 0); diff --git a/tests/jerry/es.next/symbol-split.js b/tests/jerry/es.next/symbol-split.js index 0c17e21ec6..67ec8d4f74 100644 --- a/tests/jerry/es.next/symbol-split.js +++ b/tests/jerry/es.next/symbol-split.js @@ -107,3 +107,14 @@ try { } catch (e) { assert (e === "abrupt capture"); } + +Object.defineProperty(RegExp.prototype, "exec", { value: function (str) { + this.lastIndex = 10; + return { }; +}}); + +var result = split.call ({flags: "g"}, "string"); + +assert(result.length === 2) +assert(result[0] === "") +assert(result[1] === "") diff --git a/tests/test262-es6-excludelist.xml b/tests/test262-es6-excludelist.xml index 54d5030090..d793b94dde 100644 --- a/tests/test262-es6-excludelist.xml +++ b/tests/test262-es6-excludelist.xml @@ -86,6 +86,7 @@ + Test is outdated: ES11, 21.2.5.13 13 @@ -96,7 +97,6 @@ -