• 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
vm_exec_stop_callback(void * user_p)22 vm_exec_stop_callback (void *user_p)
23 {
24   int *int_p = (int *) user_p;
25 
26   if (*int_p > 0)
27   {
28     (*int_p)--;
29 
30     return jerry_create_undefined ();
31   }
32 
33   return jerry_create_string ((const jerry_char_t *) "Abort script");
34 } /* vm_exec_stop_callback */
35 
36 int
main(void)37 main (void)
38 {
39   TEST_INIT ();
40 
41   /* Test stopping an infinite loop. */
42   if (!jerry_is_feature_enabled (JERRY_FEATURE_VM_EXEC_STOP))
43   {
44     return 0;
45   }
46 
47   jerry_init (JERRY_INIT_EMPTY);
48 
49   int countdown = 6;
50   jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16);
51 
52   const jerry_char_t inf_loop_code_src1[] = "while(true) {}";
53   jerry_value_t parsed_code_val = jerry_parse (NULL,
54                                                0,
55                                                inf_loop_code_src1,
56                                                sizeof (inf_loop_code_src1) - 1,
57                                                JERRY_PARSE_NO_OPTS);
58 
59   TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
60   jerry_value_t res = jerry_run (parsed_code_val);
61   TEST_ASSERT (countdown == 0);
62 
63   TEST_ASSERT (jerry_value_is_error (res));
64 
65   jerry_release_value (res);
66   jerry_release_value (parsed_code_val);
67 
68   /* A more complex example. Although the callback error is captured
69    * by the catch block, it is automatically thrown again. */
70 
71   /* We keep the callback function, only the countdown is reset. */
72   countdown = 6;
73 
74   const jerry_char_t inf_loop_code_src2[] = TEST_STRING_LITERAL (
75     "function f() { while (true) ; }\n"
76     "try { f(); } catch(e) {}"
77   );
78 
79   parsed_code_val = jerry_parse (NULL,
80                                  0,
81                                  inf_loop_code_src2,
82                                  sizeof (inf_loop_code_src2) - 1,
83                                  JERRY_PARSE_NO_OPTS);
84 
85   TEST_ASSERT (!jerry_value_is_error (parsed_code_val));
86   res = jerry_run (parsed_code_val);
87   TEST_ASSERT (countdown == 0);
88 
89   /* The result must have an error flag which proves that
90    * the error is thrown again inside the catch block. */
91   TEST_ASSERT (jerry_value_is_error (res));
92 
93   jerry_release_value (res);
94   jerry_release_value (parsed_code_val);
95 
96   jerry_cleanup ();
97   return 0;
98 } /* main */
99