Refactor String.prototype.replace (#3284)

This change brings the replace operation up to date with ES6 by
implementing support for the @@replace well-known symbol, while
also improving performance and memory usage.

Also fixes #3070.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2019-11-08 12:15:28 +01:00
committed by GitHub
parent f48f926a39
commit 923fd128b5
13 changed files with 1858 additions and 674 deletions
+20 -5
View File
@@ -14,6 +14,8 @@
assert ("abcabc".replace("bc", ":") === "a:abc");
assert ("hello".replace("", ":") === ":hello");
assert ("hello".replace("h", "") === "ello");
assert ("".replace("", "h") === "h");
assert ("xabcxabcx".replace (/abc/g, "[$&][$`][$']") === "x[abc][x][xabcx]x[abc][xabcx][x]x");
assert ("abc".replace (/a(b)c|d()/, "[$1][$01][$2][$02][$99][$123][$012]") === "[b][b][][][$99][b23][b2]");
@@ -110,10 +112,23 @@ try {
assert (e instanceof TypeError);
}
// The real "exec" never returns with a number.
Object.getPrototypeOf(/x/).exec = function () { return 1234; }
try {
"str".replace ({toString: function () {throw "abrupt search toString"}}, "");
assert (false);
} catch (e) {
assert (e === "abrupt search toString");
}
assert (/y/.exec("y") === 1234);
try {
"str".replace ("str", {toString: function () {throw "abrupt search toString"}});
assert (false);
} catch (e) {
assert (e === "abrupt search toString");
}
// Changing exec should not affect replace.
assert ("y".replace (/y/, "x") === "x");
try {
"str".replace ("str", function () {return {toString: function () {throw "abrupt replacer toString"}}});
assert (false);
} catch (e) {
assert (e === "abrupt replacer toString");
}