• 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 "config.h"
17 #include "jerryscript.h"
18 
19 #include "test-common.h"
20 
21 static jerry_value_t
callback_func(const jerry_value_t function_obj,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_count)22 callback_func (const jerry_value_t function_obj,
23                const jerry_value_t this_val,
24                const jerry_value_t args_p[],
25                const jerry_length_t args_count)
26 {
27   JERRY_UNUSED (function_obj);
28   JERRY_UNUSED (this_val);
29   JERRY_UNUSED (args_p);
30   JERRY_UNUSED (args_count);
31 
32   jerry_value_t value = jerry_create_string ((jerry_char_t *) "Abort run!");
33   value = jerry_create_abort_from_value (value, true);
34   return value;
35 } /* callback_func */
36 
37 int
main(void)38 main (void)
39 {
40   TEST_INIT ();
41 
42   jerry_init (JERRY_INIT_EMPTY);
43 
44   jerry_value_t global = jerry_get_global_object ();
45   jerry_value_t callback_name = jerry_create_string ((jerry_char_t *) "callback");
46   jerry_value_t func = jerry_create_external_function (callback_func);
47   jerry_value_t res = jerry_set_property (global, callback_name, func);
48   TEST_ASSERT (!jerry_value_is_error (res));
49 
50   jerry_release_value (res);
51   jerry_release_value (func);
52   jerry_release_value (callback_name);
53   jerry_release_value (global);
54 
55   const jerry_char_t inf_loop_code_src1[] = TEST_STRING_LITERAL (
56     "while(true) {\n"
57     "  with ({}) {\n"
58     "    try {\n"
59     "      callback();\n"
60     "    } catch (e) {\n"
61     "    } finally {\n"
62     "    }\n"
63     "  }\n"
64     "}"
65   );
66 
67   jerry_value_t parsed_code_val = jerry_parse (NULL,
68                                                0,
69                                                inf_loop_code_src1,
70                                                sizeof (inf_loop_code_src1) - 1,
71                                                JERRY_PARSE_NO_OPTS);
72 
73   TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
74   res = jerry_run (parsed_code_val);
75 
76   TEST_ASSERT (jerry_value_is_abort (res));
77 
78   jerry_release_value (res);
79   jerry_release_value (parsed_code_val);
80 
81   const jerry_char_t inf_loop_code_src2[] = TEST_STRING_LITERAL (
82     "function f() {"
83     "  while(true) {\n"
84     "    with ({}) {\n"
85     "      try {\n"
86     "        callback();\n"
87     "      } catch (e) {\n"
88     "      } finally {\n"
89     "      }\n"
90     "    }\n"
91     "  }"
92     "}\n"
93     "function g() {\n"
94     "  for (a in { x:5 })\n"
95     "    f();\n"
96     "}\n"
97     "\n"
98     "with({})\n"
99     " f();\n"
100   );
101 
102   parsed_code_val = jerry_parse (NULL,
103                                  0,
104                                  inf_loop_code_src2,
105                                  sizeof (inf_loop_code_src2) - 1,
106                                  JERRY_PARSE_NO_OPTS);
107 
108   TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
109   res = jerry_run (parsed_code_val);
110 
111   TEST_ASSERT (jerry_value_is_abort (res));
112 
113   jerry_release_value (res);
114   jerry_release_value (parsed_code_val);
115 
116   /* Test flag overwrites. */
117   jerry_value_t value = jerry_create_string ((jerry_char_t *) "Error description");
118   TEST_ASSERT (!jerry_value_is_abort (value));
119   TEST_ASSERT (!jerry_value_is_error (value));
120 
121   value = jerry_create_abort_from_value (value, true);
122   TEST_ASSERT (jerry_value_is_abort (value));
123   TEST_ASSERT (jerry_value_is_error (value));
124 
125   value = jerry_create_error_from_value (value, true);
126   TEST_ASSERT (!jerry_value_is_abort (value));
127   TEST_ASSERT (jerry_value_is_error (value));
128 
129   value = jerry_create_abort_from_value (value, true);
130   TEST_ASSERT (jerry_value_is_abort (value));
131   TEST_ASSERT (jerry_value_is_error (value));
132 
133   jerry_release_value (value);
134 
135   jerry_cleanup ();
136   return 0;
137 } /* main */
138