Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RegExp constructor when called with undefined arguments #492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions jerry-core/ecma/builtin-objects/ecma-builtin-regexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,10 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
}
}

if (arguments_list_len == 0 || ecma_is_value_undefined (arguments_list_p[0]))
if (ecma_is_value_object (pattern_value)
&& ecma_object_get_class_name (ecma_get_object_from_value (pattern_value)) == LIT_MAGIC_STRING_REGEXP_UL)
{
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
ret_value = ecma_op_create_regexp_object (magic_str_p, NULL);
ecma_deref_ecma_string (magic_str_p);
}
else if (ecma_is_value_object (pattern_value)
&& ecma_object_get_class_name (ecma_get_object_from_value (pattern_value)) == LIT_MAGIC_STRING_REGEXP_UL)
{
if (arguments_list_len == 1
|| (arguments_list_len > 1 && ecma_is_value_undefined (flags_value)))
if (ecma_is_value_undefined (flags_value))
{
ret_value = ecma_make_normal_completion_value (ecma_copy_value (pattern_value, true));
}
Expand All @@ -99,15 +92,32 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*
}
else
{
ECMA_TRY_CATCH (regexp_str_value,
ecma_op_to_string (pattern_value),
ret_value);
ecma_string_t *pattern_string_p = NULL;
ecma_string_t *flags_string_p = NULL;

ecma_string_t *pattern_string_p = ecma_get_string_from_value (regexp_str_value);
if (!ecma_is_value_undefined (pattern_value))
{
ECMA_TRY_CATCH (regexp_str_value,
ecma_op_to_string (pattern_value),
ret_value);

ecma_string_t *flags_string_p = NULL;
if (ecma_string_get_length (ecma_get_string_from_value (regexp_str_value)) == 0)
{
pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
}
else
{
pattern_string_p = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (regexp_str_value));
}

if (arguments_list_len > 1)
ECMA_FINALIZE (regexp_str_value);
}
else
{
pattern_string_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
}

if (ecma_is_completion_value_empty (ret_value) && !ecma_is_value_undefined (flags_value))
{
ECMA_TRY_CATCH (flags_str_value,
ecma_op_to_string (flags_value),
Expand All @@ -119,24 +129,18 @@ ecma_builtin_regexp_dispatch_construct (const ecma_value_t *arguments_list_p, /*

if (ecma_is_completion_value_empty (ret_value))
{
if (ecma_string_get_length (pattern_string_p) == 0)
{
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP);
ret_value = ecma_op_create_regexp_object (magic_str_p, flags_string_p);
ecma_deref_ecma_string (magic_str_p);
}
else
{
ret_value = ecma_op_create_regexp_object (pattern_string_p, flags_string_p);
}
ret_value = ecma_op_create_regexp_object (pattern_string_p, flags_string_p);
}

if (pattern_string_p != NULL)
{
ecma_deref_ecma_string (pattern_string_p);
}

if (flags_string_p != NULL)
{
ecma_deref_ecma_string (flags_string_p);
}

ECMA_FINALIZE (regexp_str_value);
}

return ret_value;
Expand Down
40 changes: 39 additions & 1 deletion tests/jerry/regexp-construct.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ assert (r.multiline == true);

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


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

assert (r1.source === r2.source);
assert (r2.source === r3.source);

r = new RegExp ("foo", undefined);
assert (r.source === "foo");
assert (r.global === false);
assert (r.ignoreCase === false);
assert (r.multiline === false);

r = new RegExp ("foo", void 0);
assert (r.source === "foo");
assert (r.global === false);
assert (r.ignoreCase === false);
assert (r.multiline === false);

try {
new RegExp (undefined, "ii");
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}

try {
new RegExp ("", "gg");
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}

try {
new RegExp (void 0, "mm");
assert (false);
} catch (e) {
assert (e instanceof SyntaxError);
}

r = new RegExp (undefined, undefined);
assert (r.source == "(?:)");
assert (r.global == false);
assert (r.ignoreCase == false);
assert (r.multiline == false);