Skip to content

Commit 73b5f28

Browse files
committed
Extend Jerry API & API documentation with regex flags
JerryScript-DCO-1.0-Signed-off-by: Virag Orkenyi [email protected]
1 parent 3b7409f commit 73b5f28

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

docs/02.API-REFERENCE.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,19 @@ RegExp object optional flags:
166166

167167
- JERRY_REGEXP_FLAG_GLOBAL - global match; find all matches rather than stopping after the first match
168168
- JERRY_REGEXP_FLAG_IGNORE_CASE - ignore case
169-
- JERRY_REGEXP_FLAG_MULTILINE - multiline; treat beginning and end characters (^ and $) as working over
170-
multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the
171-
very beginning or end of the whole input string)
169+
- JERRY_REGEXP_FLAG_MULTILINE - multiline; treat beginning and end characters (^ and $) as working
170+
over
171+
multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the
172+
very beginning or end of the whole input string)
173+
- JERRY_REGEXP_FLAG_STICKY - The sticky flag indicates that it matches only from the index indicated
174+
by the lastIndex property
175+
- JERRY_REGEXP_FLAG_UNICODE - The unicode flag enables various Unicode-related features
176+
- JERRY_REGEXP_FLAG_DOTALL -The dotall flag indicates that the dot special character (".") should
177+
additionally match the following line terminator ("newline") characters in a string;
178+
179+
180+
*Changed in version[[NEXT_RELEASE]]* : Added `JERRY_REGEXP_FLAG_STICKY`, `JERRY_REGEXP_FLAG_UNICODE` , `JERRY_REGEXP_FLAG_DOTALL` values.
172181

173-
*New in version 2.0*.
174182

175183
## jerry_parse_opts_t
176184

jerry-core/api/jerry.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ JERRY_STATIC_ASSERT ((int) ECMA_INIT_EMPTY == (int) JERRY_INIT_EMPTY
7171
#if ENABLED (JERRY_BUILTIN_REGEXP)
7272
JERRY_STATIC_ASSERT ((int) RE_FLAG_GLOBAL == (int) JERRY_REGEXP_FLAG_GLOBAL
7373
&& (int) RE_FLAG_MULTILINE == (int) JERRY_REGEXP_FLAG_MULTILINE
74-
&& (int) RE_FLAG_IGNORE_CASE == (int) JERRY_REGEXP_FLAG_IGNORE_CASE,
74+
&& (int) RE_FLAG_IGNORE_CASE == (int) JERRY_REGEXP_FLAG_IGNORE_CASE
75+
&& (int) RE_FLAG_STICKY== (int) JERRY_REGEXP_FLAG_STICKY
76+
&& (int) RE_FLAG_UNICODE == (int) JERRY_REGEXP_FLAG_UNICODE
77+
&& (int) RE_FLAG_DOTALL == (int) JERRY_REGEXP_FLAG_DOTALL,
7578
re_flags_t_must_be_equal_to_jerry_regexp_flags_t);
7679
#endif /* ENABLED (JERRY_BUILTIN_REGEXP) */
7780

jerry-core/include/jerryscript-core.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ typedef enum
135135
{
136136
JERRY_REGEXP_FLAG_GLOBAL = (1u << 1), /**< Globally scan string */
137137
JERRY_REGEXP_FLAG_IGNORE_CASE = (1u << 2), /**< Ignore case */
138-
JERRY_REGEXP_FLAG_MULTILINE = (1u << 3) /**< Multiline string scan */
138+
JERRY_REGEXP_FLAG_MULTILINE = (1u << 3), /**< Multiline string scan */
139+
JERRY_REGEXP_FLAG_STICKY = (1u << 4), /**< ECMAScript v11, 21.2.5.14 */
140+
JERRY_REGEXP_FLAG_UNICODE = (1u << 5), /**< ECMAScript v11, 21.2.5.17 */
141+
JERRY_REGEXP_FLAG_DOTALL = (1u << 6) /**< ECMAScript v11, 21.2.5.3 */
139142
} jerry_regexp_flags_t;
140143

141144
/**
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "jerryscript.h"
17+
18+
#include "test-common.h"
19+
20+
int
21+
main (void)
22+
{
23+
TEST_INIT ();
24+
jerry_init (JERRY_INIT_EMPTY);
25+
26+
if (!jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL))
27+
{
28+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "ES.next support is disabled\n");
29+
jerry_cleanup ();
30+
return 0;
31+
}
32+
33+
jerry_value_t global_obj_val = jerry_get_global_object ();
34+
35+
jerry_char_t pattern2[] = "\\u{61}.\\u{62}";
36+
uint16_t flags = JERRY_REGEXP_FLAG_DOTALL | JERRY_REGEXP_FLAG_UNICODE | JERRY_REGEXP_FLAG_STICKY;
37+
jerry_value_t regex_obj = jerry_create_regexp (pattern2, flags);
38+
TEST_ASSERT (jerry_value_is_object (regex_obj));
39+
40+
const jerry_char_t func_resource[] = "unknown";
41+
const jerry_char_t func_arg_list[] = "regex";
42+
const jerry_char_t func_src2[] = "return [regex.exec('a\\nb'), regex.dotAll, regex.sticky, regex.unicode ];";
43+
jerry_value_t func_val = jerry_parse_function (func_resource,
44+
sizeof (func_resource) - 1,
45+
func_arg_list,
46+
sizeof (func_arg_list) - 1,
47+
func_src2,
48+
sizeof (func_src2) - 1,
49+
JERRY_PARSE_NO_OPTS);
50+
51+
jerry_value_t res = jerry_call_function (func_val, global_obj_val, &regex_obj, 1);
52+
jerry_value_t regex_res = jerry_get_property_by_index (res, 0);
53+
jerry_value_t regex_res_str = jerry_get_property_by_index (regex_res, 0);
54+
jerry_value_t is_dotall = jerry_get_property_by_index (res, 1);
55+
jerry_value_t is_sticky = jerry_get_property_by_index (res, 2);
56+
jerry_value_t is_unicode = jerry_get_property_by_index (res, 3);
57+
58+
jerry_size_t str_size = jerry_get_string_size (regex_res_str);
59+
JERRY_VLA (jerry_char_t, res_buff, str_size);
60+
jerry_size_t res_size = jerry_string_to_char_buffer (regex_res_str, res_buff, str_size);
61+
62+
const char expected_result[] = "a\nb";
63+
TEST_ASSERT (res_size == (sizeof (expected_result) - 1));
64+
TEST_ASSERT (strncmp (expected_result, (const char *) res_buff, res_size) == 0);
65+
TEST_ASSERT (jerry_get_boolean_value (is_dotall));
66+
TEST_ASSERT (jerry_get_boolean_value (is_sticky));
67+
TEST_ASSERT (jerry_get_boolean_value (is_unicode));
68+
69+
jerry_release_value (regex_obj);
70+
jerry_release_value (res);
71+
jerry_release_value (func_val);
72+
jerry_release_value (regex_res);
73+
jerry_release_value (regex_res_str);
74+
jerry_release_value (is_dotall);
75+
jerry_release_value (is_sticky);
76+
jerry_release_value (is_unicode);
77+
jerry_release_value (global_obj_val);
78+
79+
jerry_cleanup ();
80+
return 0;
81+
} /* main */

0 commit comments

Comments
 (0)