Skip to content

Commit f35e85c

Browse files
committed
Implement RegExp.prototype.test and RegExp.prototype.toString. Add new test cases to RegExp.
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
1 parent 5ecd250 commit f35e85c

File tree

5 files changed

+199
-10
lines changed

5 files changed

+199
-10
lines changed

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ecma-conversion.h"
1919
#include "ecma-globals.h"
2020
#include "ecma-helpers.h"
21+
#include "ecma-objects.h"
2122
#include "ecma-try-catch-macro.h"
2223

2324
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_REGEXP_BUILTIN
@@ -86,6 +87,133 @@ ecma_builtin_regexp_prototype_exec (ecma_value_t this_arg, /**< this argument */
8687
return ret_value;
8788
} /* ecma_builtin_regexp_prototype_exec */
8889

90+
/**
91+
* @}
92+
* @}
93+
* @}
94+
*/
95+
96+
/**
97+
* The RegExp.prototype object's 'test' routine
98+
*
99+
* See also:
100+
* ECMA-262 v5, 15.10.6.3
101+
*
102+
* @return completion value
103+
* Returned value must be freed with ecma_free_completion_value.
104+
*/
105+
static ecma_completion_value_t
106+
ecma_builtin_regexp_prototype_test (ecma_value_t this_arg, /**< this argument */
107+
ecma_value_t arg) /**< routine's argument */
108+
{
109+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
110+
111+
ECMA_TRY_CATCH (match_value,
112+
ecma_builtin_regexp_prototype_exec (this_arg, arg),
113+
ret_value);
114+
if (ecma_is_value_undefined (match_value))
115+
{
116+
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE);
117+
}
118+
else
119+
{
120+
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
121+
}
122+
ECMA_FINALIZE (match_value);
123+
124+
return ret_value;
125+
} /* ecma_builtin_regexp_prototype_test */
126+
127+
/**
128+
* @}
129+
* @}
130+
* @}
131+
*/
132+
133+
/**
134+
* The RegExp.prototype object's 'toString' routine
135+
*
136+
* See also:
137+
* ECMA-262 v5, 15.10.6.4
138+
*
139+
* @return completion value
140+
* Returned value must be freed with ecma_free_completion_value.
141+
*/
142+
static ecma_completion_value_t
143+
ecma_builtin_regexp_prototype_to_string (ecma_value_t this_arg) /**< this argument */
144+
{
145+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
146+
147+
ECMA_TRY_CATCH (obj_this,
148+
ecma_op_to_object (this_arg),
149+
ret_value);
150+
151+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
152+
153+
/* Get RegExp source from the source property */
154+
ecma_string_t *magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_SOURCE);
155+
ecma_property_t* source_prop = ecma_op_object_get_property (obj_p, magic_string_p);
156+
ecma_deref_ecma_string (magic_string_p);
157+
158+
ecma_string_t *src_sep_str_p = ecma_new_ecma_string ((ecma_char_t *) "/");
159+
ecma_string_t *source_str_p = ecma_get_string_from_value (source_prop->u.named_data_property.value);
160+
ecma_string_t *output_str_p = ecma_concat_ecma_strings (src_sep_str_p, ecma_copy_or_ref_ecma_string (source_str_p));
161+
ecma_deref_ecma_string (source_str_p);
162+
163+
ecma_string_t *concat_p = ecma_concat_ecma_strings (output_str_p, src_sep_str_p);
164+
ecma_deref_ecma_string (src_sep_str_p);
165+
ecma_deref_ecma_string (output_str_p);
166+
output_str_p = concat_p;
167+
168+
/* Check the global flag */
169+
magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_GLOBAL);
170+
ecma_property_t* global_prop = ecma_op_object_get_property (obj_p, magic_string_p);
171+
ecma_deref_ecma_string (magic_string_p);
172+
173+
if (ecma_is_value_true (global_prop->u.named_data_property.value))
174+
{
175+
ecma_string_t *g_flag_str_p = ecma_new_ecma_string ((ecma_char_t *) "g");
176+
concat_p = ecma_concat_ecma_strings (output_str_p, g_flag_str_p);
177+
ecma_deref_ecma_string (output_str_p);
178+
ecma_deref_ecma_string (g_flag_str_p);
179+
output_str_p = concat_p;
180+
}
181+
182+
/* Check the ignoreCase flag */
183+
magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_IGNORECASE);
184+
ecma_property_t* ignorecase_prop = ecma_op_object_get_property (obj_p, magic_string_p);
185+
ecma_deref_ecma_string (magic_string_p);
186+
187+
if (ecma_is_value_true (ignorecase_prop->u.named_data_property.value))
188+
{
189+
ecma_string_t *ic_flag_str_p = ecma_new_ecma_string ((ecma_char_t *) "i");
190+
concat_p = ecma_concat_ecma_strings (output_str_p, ic_flag_str_p);
191+
ecma_deref_ecma_string (output_str_p);
192+
ecma_deref_ecma_string (ic_flag_str_p);
193+
output_str_p = concat_p;
194+
}
195+
196+
/* Check the global flag */
197+
magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_MULTILINE);
198+
ecma_property_t* multiline_prop = ecma_op_object_get_property (obj_p, magic_string_p);
199+
ecma_deref_ecma_string (magic_string_p);
200+
201+
if (ecma_is_value_true (multiline_prop->u.named_data_property.value))
202+
{
203+
ecma_string_t *m_flag_str_p = ecma_new_ecma_string ((ecma_char_t *) "m");
204+
concat_p = ecma_concat_ecma_strings (output_str_p, m_flag_str_p);
205+
ecma_deref_ecma_string (output_str_p);
206+
ecma_deref_ecma_string (m_flag_str_p);
207+
output_str_p = concat_p;
208+
}
209+
210+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
211+
212+
ECMA_FINALIZE (obj_this);
213+
214+
return ret_value;
215+
} /* ecma_builtin_regexp_prototype_to_string */
216+
89217
/**
90218
* @}
91219
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.inc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ OBJECT_VALUE (ECMA_MAGIC_STRING_CONSTRUCTOR,
4040
ECMA_PROPERTY_CONFIGURABLE)
4141

4242
ROUTINE (ECMA_MAGIC_STRING_EXEC, ecma_builtin_regexp_prototype_exec, 1, 1)
43+
ROUTINE (ECMA_MAGIC_STRING_TEST, ecma_builtin_regexp_prototype_test, 1, 1)
44+
ROUTINE (ECMA_MAGIC_STRING_TO_STRING_UL, ecma_builtin_regexp_prototype_to_string, 0, 0)
4345

4446
#undef OBJECT_ID
4547
#undef SIMPLE_VALUE
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var r;
17+
18+
r = new RegExp ("[abc]*").exec("aaabbcccabcacbacabacbacab");
19+
assert (r == "aaabbcccabcacbacabacbacab");
20+
21+
r = new RegExp ("[abc]*").exec("aaabbcccabdcacb");
22+
assert (r == "aaabbcccab");
23+
24+
r = new RegExp ("[abc]*").exec("defghjklmnopqrstuvwxyz");
25+
assert (r == "");
26+
27+
r = new RegExp ("[a-z]*").exec("abcdefghjklmnopqrstuvwxyz");
28+
assert (r == "abcdefghjklmnopqrstuvwxyz");
29+
30+
r = new RegExp ("[A-Z]*").exec("abcdefghjklmnopqrstuvwxyz");
31+
assert (r == "");
32+
33+
// FIXME: Add more tescase when Unicode support is finished!

tests/jerry/regexp_construct.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ try {
5353
// assert (r2.ignoreCase == true);
5454
// assert (r2.multiline == true);
5555

56-
// r = /a/gim;
57-
// assert (r.source == "a");
58-
// assert (r.global == false);
59-
// assert (r.ignoreCase == false);
60-
// assert (r.multiline == false);
56+
r = /a/;
57+
assert (r.source == "a");
58+
assert (r.global == false);
59+
assert (r.ignoreCase == false);
60+
assert (r.multiline == false);
6161

62-
// r = /a/;
63-
// assert (r.source == "a");
64-
// assert (r.global == true);
65-
// assert (r.ignoreCase == true);
66-
// assert (r.multiline == true);
62+
r = /a/gim;
63+
assert (r.source == "a");
64+
assert (r.global == true);
65+
assert (r.ignoreCase == true);
66+
assert (r.multiline == true);

tests/jerry/regexp_routines.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var r;
17+
18+
r = new RegExp ("a");
19+
assert (r.exec ("a") == "a");
20+
assert (r.exec ("b") == undefined);
21+
22+
assert (r.test ("a") == true);
23+
assert (r.test ("b") == false);
24+
25+
r = new RegExp ("a", "mig");
26+
assert (r.toString () == "/a/gim");

0 commit comments

Comments
 (0)