• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
main(void)21 main (void)
22 {
23   TEST_INIT ();
24   jerry_init (JERRY_INIT_EMPTY);
25 
26   jerry_value_t global_obj_val = jerry_get_global_object ();
27 
28   jerry_char_t pattern[] = "[^.]+";
29   uint16_t flags = JERRY_REGEXP_FLAG_GLOBAL | JERRY_REGEXP_FLAG_MULTILINE;
30   jerry_value_t regex_obj = jerry_create_regexp (pattern, flags);
31   TEST_ASSERT (jerry_value_is_object (regex_obj));
32 
33   const jerry_char_t func_resource[] = "unknown";
34   const jerry_char_t func_arg_list[] = "regex";
35   const jerry_char_t func_src[] = "return [regex.exec('something.domain.com'), regex.multiline, regex.global];";
36   jerry_value_t func_val = jerry_parse_function (func_resource,
37                                                  sizeof (func_resource) - 1,
38                                                  func_arg_list,
39                                                  sizeof (func_arg_list) - 1,
40                                                  func_src,
41                                                  sizeof (func_src) - 1,
42                                                  JERRY_PARSE_NO_OPTS);
43 
44   jerry_value_t res = jerry_call_function (func_val, global_obj_val, &regex_obj, 1);
45   jerry_value_t regex_res = jerry_get_property_by_index (res, 0);
46   jerry_value_t regex_res_str = jerry_get_property_by_index (regex_res, 0);
47   jerry_value_t is_multiline = jerry_get_property_by_index (res, 1);
48   jerry_value_t is_global = jerry_get_property_by_index (res, 2);
49 
50   jerry_size_t str_size = jerry_get_string_size (regex_res_str);
51   JERRY_VLA (jerry_char_t, res_buff, str_size);
52   jerry_size_t res_size = jerry_string_to_char_buffer (regex_res_str, res_buff, str_size);
53 
54   const char expected_result[] = "something";
55   TEST_ASSERT (res_size == (sizeof (expected_result) - 1));
56   TEST_ASSERT (strncmp (expected_result, (const char *) res_buff, res_size) == 0);
57   TEST_ASSERT (jerry_get_boolean_value (is_multiline));
58   TEST_ASSERT (jerry_get_boolean_value (is_global));
59 
60   jerry_release_value (regex_obj);
61   jerry_release_value (res);
62   jerry_release_value (func_val);
63   jerry_release_value (regex_res);
64   jerry_release_value (regex_res_str);
65   jerry_release_value (is_multiline);
66   jerry_release_value (is_global);
67   jerry_release_value (global_obj_val);
68 
69   jerry_cleanup ();
70   return 0;
71 } /* main */
72