Update @@search to conform to ES11 standard (#4003)

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai daniel.batyai@h-lab.eu
This commit is contained in:
Dániel Bátyai 2020-07-20 12:46:49 +02:00 committed by GitHub
parent b7fa4afb66
commit f6cde17bfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 19 deletions

View File

@ -1921,7 +1921,7 @@ ecma_regexp_search_helper (ecma_value_t regexp_arg, /**< regexp argument */
ecma_value_t result = ECMA_VALUE_ERROR;
/* 3-4. */
/* 3. */
ecma_string_t *const string_p = ecma_op_to_string (string_arg);
if (string_p == NULL)
{
@ -1930,7 +1930,7 @@ ecma_regexp_search_helper (ecma_value_t regexp_arg, /**< regexp argument */
ecma_object_t *const regexp_object_p = ecma_get_object_from_value (regexp_arg);
/* 5-6. */
/* 4. */
ecma_string_t *const last_index_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL);
const ecma_value_t prev_last_index = ecma_op_object_get (regexp_object_p, last_index_str_p);
if (ECMA_IS_VALUE_ERROR (prev_last_index))
@ -1938,35 +1938,56 @@ ecma_regexp_search_helper (ecma_value_t regexp_arg, /**< regexp argument */
goto cleanup_string;
}
/* 7-8. */
const ecma_value_t status = ecma_op_object_put (regexp_object_p, last_index_str_p, ecma_make_uint32_value (0), true);
if (ECMA_IS_VALUE_ERROR (status))
/* 5. */
if (prev_last_index != ecma_make_uint32_value (0))
{
ecma_free_value (prev_last_index);
goto cleanup_string;
const ecma_value_t status = ecma_op_object_put (regexp_object_p,
last_index_str_p,
ecma_make_uint32_value (0),
true);
if (ECMA_IS_VALUE_ERROR (status))
{
goto cleanup_prev_last_index;
}
JERRY_ASSERT (ecma_is_value_boolean (status));
}
JERRY_ASSERT (ecma_is_value_boolean (status));
/* 9-10. */
/* 6. */
const ecma_value_t match = ecma_op_regexp_exec (regexp_arg, string_p);
if (ECMA_IS_VALUE_ERROR (match))
{
ecma_free_value (prev_last_index);
goto cleanup_string;
goto cleanup_prev_last_index;
}
/* 11-12. */
result = ecma_op_object_put (regexp_object_p, last_index_str_p, prev_last_index, true);
ecma_free_value (prev_last_index);
if (ECMA_IS_VALUE_ERROR (result))
/* 7. */
const ecma_value_t current_last_index = ecma_op_object_get (regexp_object_p, last_index_str_p);
if (ECMA_IS_VALUE_ERROR (current_last_index))
{
ecma_free_value (match);
goto cleanup_string;
goto cleanup_prev_last_index;
}
/* 13-14. */
const bool same_value = ecma_op_same_value (prev_last_index, current_last_index);
ecma_free_value (current_last_index);
/* 8. */
if (!same_value)
{
result = ecma_op_object_put (regexp_object_p, last_index_str_p, prev_last_index, true);
if (ECMA_IS_VALUE_ERROR (result))
{
ecma_free_value (match);
goto cleanup_prev_last_index;
}
JERRY_ASSERT (ecma_is_value_boolean (result));
}
/* 9-10. */
if (ecma_is_value_null (match))
{
result = ecma_make_int32_value (-1);
@ -1978,6 +1999,9 @@ ecma_regexp_search_helper (ecma_value_t regexp_arg, /**< regexp argument */
ecma_deref_object (match_p);
}
cleanup_prev_last_index:
ecma_free_value (prev_last_index);
cleanup_string:
ecma_deref_ecma_string (string_p);
return result;

View File

@ -226,3 +226,70 @@ o = {
}
assert (RegExp.prototype[Symbol.search].call (o, "str") === 0);
var r = /a/;
r.lastIndex = 3.14;
var get_calls = [];
var set_calls = [];
var handler = {
get: function(o, k) {
get_calls.push(k);
if (k === "exec") {
return (str) => r.exec(str);
}
return r[k];
},
set: function(o, k, v) {
set_calls.push(k);
r[k] = v;
}
};
var p = new Proxy(r, handler);
assert (search.call(p, "bba") === 2);
assert (get_calls.join(",") === "lastIndex,exec,lastIndex");
assert (set_calls.join(",") === "lastIndex,lastIndex");
assert (r.lastIndex === 3.14);
var o = {
get lastIndex() {
Object.defineProperty(o, "lastIndex", {
get: function () { throw "abrupt get second lastIndex"; }
});
return 1;
},
set lastIndex(v) {},
exec: () => { return null; }
}
try {
search.call(o, "str");
assert (false);
} catch (e) {
assert (e === "abrupt get second lastIndex");
}
var index = 1;
var o = {
get lastIndex() {
return index++;
},
set lastIndex(v) {
Object.defineProperty(o, "lastIndex", {
set: function (v) { throw "abrupt set second lastIndex"; }
});
},
exec: () => { return null; }
}
try {
search.call(o, "str");
assert (false);
} catch (e) {
assert (e === "abrupt set second lastIndex");
}

View File

@ -378,4 +378,6 @@
<test id="built-ins/String/prototype/trim/15.5.4.20-3-6.js"><reason>Unicode 13: 0x180E is no longer whitespace character</reason></test>
<test id="built-ins/parseFloat/S15.1.2.3_A2_T10.js"><reason>Unicode 13: 0x180E is no longer whitespace character</reason></test>
<test id="built-ins/parseInt/S15.1.2.2_A2_T10.js"><reason>Unicode 13: 0x180E is no longer whitespace character</reason></test>
<test id="built-ins/RegExp/prototype/Symbol.search/set-lastindex-restore-err.js"><reason>lastIndex handling in Symbol.search has changed since ES6</reason></test>
<test id="built-ins/RegExp/prototype/Symbol.search/set-lastindex-restore.js"><reason>lastIndex handling in Symbol.search has changed since ES6</reason></test>
</excludeList>