Skip to content

Commit a21a419

Browse files
authored
Improve RegExp.prototype.compile (#2971)
This patch fixes the return value of the routine also fixes several incorrect regression-tests due to the previously incorrect [[RegExpMatcher]] internal slot check. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent 2700e66 commit a21a419

6 files changed

Lines changed: 35 additions & 11 deletions

File tree

jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument
6464
ecma_value_t flags_arg) /**< flags */
6565
{
6666
if (!ecma_is_value_object (this_arg)
67-
|| !ecma_object_class_is (ecma_get_object_from_value (this_arg), LIT_MAGIC_STRING_REGEXP_UL))
67+
|| !ecma_object_class_is (ecma_get_object_from_value (this_arg), LIT_MAGIC_STRING_REGEXP_UL)
68+
/* The builtin RegExp.prototype object does not have [[RegExpMatcher]] internal slot */
69+
|| ecma_get_object_from_value (this_arg) == ecma_builtin_get (ECMA_BUILTIN_ID_REGEXP_PROTOTYPE))
6870
{
6971
return ecma_raise_type_error (ECMA_ERR_MSG ("Incomplete RegExp type"));
7072
}
@@ -149,7 +151,7 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument
149151
re_initialize_props (this_obj_p, pattern_string_p, flags);
150152
ecma_free_value (obj_this);
151153

152-
return ECMA_VALUE_UNDEFINED;
154+
return ecma_copy_value (this_arg);
153155
}
154156

155157
ecma_string_t *pattern_string_p = NULL;
@@ -213,7 +215,7 @@ ecma_builtin_regexp_prototype_compile (ecma_value_t this_arg, /**< this argument
213215
ecma_free_value (obj_this);
214216
ecma_deref_ecma_string (pattern_string_p);
215217

216-
return ECMA_VALUE_UNDEFINED;
218+
return ecma_copy_value (this_arg);
217219
} /* ecma_builtin_regexp_prototype_compile */
218220

219221
#endif /* ENABLED (JERRY_BUILTIN_ANNEXB) */

tests/jerry/regression-test-issue-612.js renamed to tests/jerry/es2015/regression-test-issue-612.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
var result = RegExp.prototype.compile([]);
16-
assert(result === undefined);
15+
try {
16+
RegExp.prototype.compile([]);
17+
assert(false);
18+
} catch (e) {
19+
assert(e instanceof TypeError);
20+
}

tests/jerry/regexp-routines.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ try {
9494
}
9595

9696
re2.lastIndex = 2;
97-
re2.compile("asd", "im");
97+
assert (re2.compile("asd", "im") === re2);
9898

9999
assert (re2 == "/asd/im");
100100
assert (re2.global === false);
@@ -103,7 +103,7 @@ assert (re2.multiline === true);
103103
assert (re2.source === "asd");
104104
assert (re2.lastIndex === 0);
105105

106-
re2.compile(re1);
106+
assert (re2.compile(re1) === re2);
107107
assert (re2.toString() === re1.toString());
108108
assert (re2.global === re1.global);
109109
assert (re2.ignoreCase === re1.ignoreCase);

tests/jerry/regression-test-issue-1065.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
new (new (new RegExp().constructor)().constructor)().constructor.prototype.toString()
16-
RegExp().constructor().constructor.prototype.compile(RegExp.prototype)
15+
new (new (new RegExp().constructor)().constructor)().constructor.prototype.toString();
16+
17+
try {
18+
RegExp().constructor().constructor.prototype.compile(RegExp.prototype);
19+
assert(false);
20+
} catch (e) {
21+
assert(e instanceof TypeError);
22+
}

tests/jerry/regression-test-issue-1080.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@
1414

1515
((new RegExp("}").constructor)("a", "g").constructor)(undefined).constructor.prototype.toString();
1616
new Date("2015-07-09T12:13:14.121+01:30").toISOString();
17-
RegExp(new RegExp("a", "g")).constructor("").constructor.prototype.compile(RegExp.prototype);
17+
18+
try {
19+
RegExp(new RegExp("a", "g")).constructor("").constructor.prototype.compile(RegExp.prototype);
20+
assert (false);
21+
} catch (e) {
22+
assert(e instanceof TypeError);
23+
}
24+
1825
new Date("2015-09-17").getUTCFullYear();

tests/jerry/regression-test-issue-783.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
RegExp.prototype.compile(RegExp.prototype);
15+
try {
16+
RegExp.prototype.compile(RegExp.prototype);
17+
assert(false);
18+
} catch (e) {
19+
assert (e instanceof TypeError);
20+
}

0 commit comments

Comments
 (0)