Implement Symbol.matchAll (#4082)

The following methods were implemented:
- String.prototype.matchAll based on ECMA-262 v11, 21.1.3.12
- RegExp.prototype[@@matchAll] based on ECMA-262 v11, 21.2.5.8
- RegExp String Iterator Object based on 21.2.7

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam
2021-01-18 18:08:35 +01:00
committed by GitHub
parent 1d42d17ab6
commit 6ec4455111
24 changed files with 965 additions and 83 deletions
@@ -0,0 +1,190 @@
// 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.
assert(RegExp.prototype[Symbol.matchAll].length === 1);
var desc = Object.getOwnPropertyDescriptor(RegExp.prototype[Symbol.matchAll], "length");
assert(!desc.enumerable);
assert(!desc.writable);
assert(desc.configurable);
assert(RegExp.prototype[Symbol.matchAll].name === "[Symbol.matchAll]");
var desc = Object.getOwnPropertyDescriptor(RegExp.prototype[Symbol.matchAll], "name");
assert(!desc.enumerable);
assert(!desc.writable);
assert(desc.configurable);
// test basic functionality
var re = /[0-9]+/g;
var str = '2016-01-02';
var result = re[Symbol.matchAll](str);
assert(Array.from(result, x => x[0]).toString() === "2016,01,02");
class MyRegExp extends RegExp {
[Symbol.matchAll](str) {
const result = RegExp.prototype[Symbol.matchAll].call(this, str);
if (!result) {
return null;
}
return Array.from(result);
}
}
var regexp = new MyRegExp('-[0-9]+', 'g');
var result = re[Symbol.matchAll]("2016-01-02|2019-03-07");
assert(Array.from(result, x => x[0]).toString() === "2016,01,02,2019,03,07");
var counter = 0;
var callArgs;
var regexp = /\d/u;
regexp.constructor = {
[Symbol.species]: function(){
counter++;
callArgs = arguments;
return /\w/g;
}
};
var str = 'a*b';
var result = regexp[Symbol.matchAll](str);
assert(counter === 1);
assert(callArgs.length === 2);
assert(callArgs[0] === regexp);
assert(callArgs[1] === 'u');
assert(Array.from(result, x => x[0]).toString() === "a");
// test when flags throws error
var regexp = /a/g;
Object.defineProperty(regexp, 'flags', { get () { throw 42; }});
try {
regexp[Symbol.matchAll]("foo");
assert(false);
} catch (e) {
assert(e === 42);
}
// test when flags value is symbol
var regexp = /a/g;
var sym = Symbol("foo")
Object.defineProperty(regexp, 'flags', { value: sym });
try {
regexp[Symbol.matchAll]("foo");
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// test when match Symbol throws error
var regexp = /[A-Z]/g;
Object.defineProperty (regexp, Symbol.match, { get () { throw 42; }});
try {
regexp[Symbol.matchAll]("foo");
assert(false);
} catch (e) {
assert(e === 42);
}
// test when can't create RegExp
var obj = {
toString() {
throw 42;
}
};
try {
RegExp.prototype[Symbol.matchAll].call(obj, '');
assert(false);
} catch (e) {
assert(e === 42);
}
// test when constructor throws error
var regexp = /./;
Object.defineProperty(regexp, 'constructor', {
get(){
throw 42;
}
});
try {
regexp[Symbol.matchAll]("foo");
assert(false);
} catch (e) {
assert(e === 42);
}
// test when global flag throws error
var regexp = /[A-Z]/;
Object.defineProperty(regexp, 'global', { get() { throw 42; }});
try {
regexp[Symbol.matchAll]('');
assert(false);
} catch (e) {
assert(e === 42);
}
// test when lastIndex throws error
var regexp = /[A-Z]/;
regexp.lastIndex = {
valueOf() {
throw 42;
}
};
try {
regexp[Symbol.matchAll]("foo");
assert(false);
} catch (e) {
assert(e === 42);
}
// test when argument is not an object
try {
RegExp.prototype[Symbol.matchAll].call(null, '');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
RegExp.prototype[Symbol.matchAll].call(true, '');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
RegExp.prototype[Symbol.matchAll].call('', '');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
RegExp.prototype[Symbol.matchAll].call(Symbol(), '');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
RegExp.prototype[Symbol.matchAll].call(1, '');
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
@@ -0,0 +1,177 @@
// 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.
assert(String.prototype.matchAll.length === 1);
var desc = Object.getOwnPropertyDescriptor(String.prototype.matchAll, "length");
assert(!desc.enumerable);
assert(!desc.writable);
assert(desc.configurable);
assert(String.prototype.matchAll.name === "matchAll");
var desc = Object.getOwnPropertyDescriptor(String.prototype.matchAll, "name");
assert(!desc.enumerable);
assert(!desc.writable);
assert(desc.configurable);
// test basic functionality
var re = /[0-9]+/g;
var str = '2016-01-02|2019-03-07';
var result = str.matchAll(re);
var result_array = Array.from(result, x => x[0]);
assert(result_array.toString() === "2016,01,02,2019,03,07");
var counter = 0;
var obj = {};
RegExp.prototype[Symbol.matchAll] = function() {
counter++;
return obj;
};
assert('a'.matchAll(null) === obj);
assert(counter === 1);
assert(''.matchAll(undefined) === obj);
assert(counter === 2) ;
var obj = {};
var retval = {};
var counter = 0;
var thisVal, args;
obj[Symbol.matchAll] = function () {
counter++;
thisVal = this;
args = arguments;
return retval;
};
var str = ''
assert(str.matchAll(obj) === retval);
assert(counter === 1);
assert(thisVal === obj);
assert(args !== undefined);
assert(args.length === 1);
assert(args[0] === str);
// throw error when flag is not global
try {
"foo".matchAll(/a/);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
"foo".matchAll(/a/i);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
"foo".matchAll(/a/m);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
"foo".matchAll(/a/u);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
"foo".matchAll(/a/u);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
try {
"foo".matchAll(/a/y);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
var regexp = /a/;
Object.defineProperty(regexp, 'flags', {
value: 'muyi'
});
try {
"foo".matchAll(regexp);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// test when flags value is undefined
var regexp = /a/g;
Object.defineProperty(regexp, 'flags', { value: undefined });
try {
"foo".matchAll(regexp)
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// test when match Symbol throws error
var regexp = /[A-Z]/g;
Object.defineProperty (regexp, Symbol.match, { get () { throw 42; }});
try {
"foo".matchAll(regexp);
assert(false);
} catch (e) {
assert(e === 42);
}
// test when flags throws error
var regexp = /a/g;
Object.defineProperty(regexp, 'flags', { get () { throw 42; }});
try {
"foo".matchAll(regexp);
assert(false);
} catch (e) {
assert(e === 42);
}
// test when flags value is symbol
var regexp = /a/g;
var sym = Symbol("foo")
Object.defineProperty(regexp, 'flags', { value: sym });
try {
"foo".matchAll(regexp);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
// test when matchAll Symbol throws error
var regexp = /[A-Z]/g;
Object.defineProperty (regexp, Symbol.matchAll, { get () { throw 42; }});
try {
"foo".matchAll(regexp);
assert(false);
} catch (e) {
assert(e === 42);
}
-55
View File
@@ -109,62 +109,8 @@
<test id="built-ins/Promise/race/resolve-non-callable.js"><reason>Test expects incorrect call order</reason></test>
<test id="built-ins/Proxy/preventExtensions/trap-is-undefined-target-is-proxy.js"><reason></reason></test>
<test id="built-ins/Proxy/setPrototypeOf/toboolean-trap-result-false.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/isregexp-called-once.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/isregexp-this-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/length.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/name.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/prop-desc.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/regexpcreate-this-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-get-constructor-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-get-species-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-is-undefined.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-species-is-null-or-undefined.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-species-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-constructor.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-regexp-get-global-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/species-regexp-get-unicode-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/string-tostring-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/string-tostring.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/this-lastindex-cached.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/this-tolength-lastindex-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/this-tostring-flags-throws.js"><reason></reason></test>
<test id="built-ins/RegExp/prototype/Symbol.matchAll/this-tostring-flags.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/Symbol.toStringTag.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/ancestry.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-call-throws.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-get-throws.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-throws.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-tostring-throws.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-tostring.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-not-callable.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/length.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/name.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/next-iteration-global.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/next-iteration.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/next-missing-internal-slots.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/prop-desc.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/regexp-tolength-lastindex-throws.js"><reason></reason></test>
<test id="built-ins/RegExpStringIteratorPrototype/next/this-is-not-object-throws.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/length.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/name.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/prop-desc.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-get-matchAll-throws.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-is-null.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-is-undefined-or-null-invokes-matchAll.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-is-undefined.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-matchAll-invocation.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-matchAll-is-undefined-or-null.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-matchAll-not-callable.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-matchAll-throws.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-prototype-get-matchAll-throws.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-prototype-has-no-matchAll.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-prototype-matchAll-invocation.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/regexp-prototype-matchAll-throws.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/this-val-non-obj-coercible.js"><reason></reason></test>
<test id="built-ins/String/prototype/matchAll/toString-this-val.js"><reason></reason></test>
<test id="built-ins/String/prototype/normalize/form-is-not-valid-throws.js"><reason></reason></test>
<test id="built-ins/String/prototype/normalize/length.js"><reason></reason></test>
<test id="built-ins/String/prototype/normalize/name.js"><reason></reason></test>
@@ -178,7 +124,6 @@
<test id="built-ins/String/prototype/toLocaleLowerCase/special_casing_conditional.js"><reason></reason></test>
<test id="built-ins/String/prototype/toLowerCase/Final_Sigma_U180E.js"><reason></reason></test>
<test id="built-ins/String/prototype/toLowerCase/special_casing_conditional.js"><reason></reason></test>
<test id="built-ins/Symbol/matchAll/prop-desc.js"><reason></reason></test>
<test id="built-ins/ThrowTypeError/name.js"><reason></reason></test>
<test id="built-ins/TypedArray/from/arylk-get-length-error.js"><reason></reason></test>
<test id="built-ins/TypedArray/from/arylk-to-length-error.js"><reason></reason></test>
+5 -3
View File
@@ -232,6 +232,7 @@ main (void)
" [Symbol.toPrimitive]: 10,"
" [Symbol.toStringTag]: 11,"
" [Symbol.unscopables]: 12,"
" [Symbol.matchAll]: 13,"
"})";
const char *symbols[] =
@@ -248,6 +249,7 @@ main (void)
"toPrimitive",
"toStringTag",
"unscopables",
"matchAll",
};
jerry_value_t obj = jerry_eval (obj_src, sizeof (obj_src) - 1, JERRY_PARSE_NO_OPTS);
@@ -262,7 +264,7 @@ main (void)
uint32_t prop_index = 0;
for (jerry_well_known_symbol_t id = JERRY_SYMBOL_ASYNC_ITERATOR;
id <= JERRY_SYMBOL_UNSCOPABLES;
id <= JERRY_SYMBOL_MATCH_ALL;
id++, expected++, prop_index++)
{
jerry_value_t well_known_symbol = jerry_get_well_known_symbol (id);
@@ -316,7 +318,7 @@ main (void)
prop_index = 0;
for (jerry_well_known_symbol_t id = JERRY_SYMBOL_ASYNC_ITERATOR;
id <= JERRY_SYMBOL_UNSCOPABLES;
id <= JERRY_SYMBOL_MATCH_ALL;
id++, expected++, prop_index++)
{
jerry_value_t well_known_symbol = jerry_get_well_known_symbol (id);
@@ -330,7 +332,7 @@ main (void)
jerry_release_value (well_known_symbol);
}
jerry_well_known_symbol_t invalid_symbol = (jerry_well_known_symbol_t) (JERRY_SYMBOL_UNSCOPABLES + 1);
jerry_well_known_symbol_t invalid_symbol = (jerry_well_known_symbol_t) (JERRY_SYMBOL_MATCH_ALL + 1);
jerry_value_t invalid_well_known_symbol = jerry_get_well_known_symbol (invalid_symbol);
TEST_ASSERT (jerry_value_is_undefined (invalid_well_known_symbol));
jerry_release_value (invalid_well_known_symbol);