Skip to content

Commit 39cf5aa

Browse files
committed
Fix RegExp constructor when called with undefined arguments
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 1193de8 commit 39cf5aa

File tree

2 files changed

+71
-29
lines changed

2 files changed

+71
-29
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-regexp.cpp

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,10 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
7878
}
7979
}
8080

81-
if (arguments_list_len == 0 || ecma_is_value_undefined (arguments_list_p[0]))
81+
if (ecma_is_value_object (pattern_value)
82+
&& ecma_object_get_class_name (ecma_get_object_from_value (pattern_value)) == LIT_MAGIC_STRING_REGEXP_UL)
8283
{
83-
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
84-
ret_value = ecma_op_create_regexp_object (magic_str_p, NULL);
85-
ecma_deref_ecma_string (magic_str_p);
86-
}
87-
else if (ecma_is_value_object (pattern_value)
88-
&& ecma_object_get_class_name (ecma_get_object_from_value (pattern_value)) == LIT_MAGIC_STRING_REGEXP_UL)
89-
{
90-
if (arguments_list_len == 1
91-
|| (arguments_list_len > 1 && ecma_is_value_undefined (flags_value)))
84+
if (ecma_is_value_undefined (flags_value))
9285
{
9386
ret_value = ecma_make_normal_completion_value (ecma_copy_value (pattern_value, true));
9487
}
@@ -99,15 +92,32 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
9992
}
10093
else
10194
{
102-
ECMA_TRY_CATCH (regexp_str_value,
103-
ecma_op_to_string (pattern_value),
104-
ret_value);
95+
ecma_string_t *pattern_string_p = NULL;
96+
ecma_string_t *flags_string_p = NULL;
10597

106-
ecma_string_t *pattern_string_p = ecma_get_string_from_value (regexp_str_value);
98+
if (!ecma_is_value_undefined (pattern_value))
99+
{
100+
ECMA_TRY_CATCH (regexp_str_value,
101+
ecma_op_to_string (pattern_value),
102+
ret_value);
107103

108-
ecma_string_t *flags_string_p = NULL;
104+
if (ecma_string_get_length (ecma_get_string_from_value (regexp_str_value)) == 0)
105+
{
106+
pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
107+
}
108+
else
109+
{
110+
pattern_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
111+
}
109112

110-
if (arguments_list_len > 1)
113+
ECMA_FINALIZE (regexp_str_value);
114+
}
115+
else
116+
{
117+
pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
118+
}
119+
120+
if (ecma_is_completion_value_empty (ret_value) && !ecma_is_value_undefined (flags_value))
111121
{
112122
ECMA_TRY_CATCH (flags_str_value,
113123
ecma_op_to_string (flags_value),
@@ -119,24 +129,18 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
119129

120130
if (ecma_is_completion_value_empty (ret_value))
121131
{
122-
if (ecma_string_get_length (pattern_string_p) == 0)
123-
{
124-
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
125-
ret_value = ecma_op_create_regexp_object (magic_str_p, flags_string_p);
126-
ecma_deref_ecma_string (magic_str_p);
127-
}
128-
else
129-
{
130-
ret_value = ecma_op_create_regexp_object (pattern_string_p, flags_string_p);
131-
}
132+
ret_value = ecma_op_create_regexp_object (pattern_string_p, flags_string_p);
133+
}
134+
135+
if (pattern_string_p != NULL)
136+
{
137+
ecma_deref_ecma_string (pattern_string_p);
132138
}
133139

134140
if (flags_string_p != NULL)
135141
{
136142
ecma_deref_ecma_string (flags_string_p);
137143
}
138-
139-
ECMA_FINALIZE (regexp_str_value);
140144
}
141145

142146
return ret_value;

tests/jerry/regexp-construct.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ assert (r.multiline == true);
8787

8888
assert(Object.prototype.toString.call(RegExp.prototype) === '[object RegExp]');
8989

90-
9190
/* The 'undefined' argument for the RegExp constructor should not be converted to string,
9291
* and it should behave just like when there is no argument.
9392
*/
@@ -98,3 +97,42 @@ r3 = new RegExp(foo)
9897

9998
assert (r1.source === r2.source);
10099
assert (r2.source === r3.source);
100+
101+
r = new RegExp ("foo", undefined);
102+
assert (r.source === "foo");
103+
assert (r.global === false);
104+
assert (r.ignoreCase === false);
105+
assert (r.multiline === false);
106+
107+
r = new RegExp ("foo", void 0);
108+
assert (r.source === "foo");
109+
assert (r.global === false);
110+
assert (r.ignoreCase === false);
111+
assert (r.multiline === false);
112+
113+
try {
114+
new RegExp (undefined, "ii");
115+
assert (false);
116+
} catch (e) {
117+
assert (e instanceof SyntaxError);
118+
}
119+
120+
try {
121+
new RegExp ("", "gg");
122+
assert (false);
123+
} catch (e) {
124+
assert (e instanceof SyntaxError);
125+
}
126+
127+
try {
128+
new RegExp (void 0, "mm");
129+
assert (false);
130+
} catch (e) {
131+
assert (e instanceof SyntaxError);
132+
}
133+
134+
r = new RegExp (undefined, undefined);
135+
assert (r.source == "(?:)");
136+
assert (r.global == false);
137+
assert (r.ignoreCase == false);
138+
assert (r.multiline == false);

0 commit comments

Comments
 (0)