@@ -35,7 +35,8 @@ Possible compile time enabled feature types:
3535 - JERRY_FEATURE_REGEXP_DUMP - regexp byte-code dumps
3636 - JERRY_FEATURE_SNAPSHOT_SAVE - saving snapshot files
3737 - JERRY_FEATURE_SNAPSHOT_EXEC - executing snapshot files
38- - JERRY_FEATURE_DEBUGGER - debugging
38+ - JERRY_FEATURE_DEBUGGER - debugger server is available
39+ - JERRY_FEATURE_VM_EXEC_STOP - stopping ECMAScript execution
3940
4041## jerry_char_t
4142
@@ -3896,3 +3897,76 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p,
38963897- [jerry_init](#jerry_init)
38973898- [jerry_cleanup](#jerry_cleanup)
38983899- [jerry_register_magic_strings](#jerry_register_magic_strings)
3900+
3901+
3902+ ## jerry_set_vm_exec_stop_callback
3903+
3904+ **Summary**
3905+
3906+ When JERRY_FEATURE_VM_EXEC_STOP is enabled a callback function can be
3907+ specified by this function. This callback is periodically called when
3908+ JerryScript executes an ECMASCript program.
3909+
3910+ If the callback returns with undefined value the ECMAScript execution
3911+ continues. Otherwise the result is thrown by the engine (if the error
3912+ flag is not set for the returned value the engine automatically sets
3913+ it). The callback function might be called again even if it thrown
3914+ an error. In this case the function must throw the same error again.
3915+
3916+ Frequently calling a callback in the main executor loop reduces
3917+ its performance. The `frequency` argument allows decreasing
3918+ this overhead. If its value is `N`, the callback is called only
3919+ every `N` times. The minimum value of `N` is `1` (`0` is converted
3920+ to `1` automatically).
3921+
3922+ **Prototype**
3923+
3924+ ```c
3925+ void
3926+ jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb,
3927+ void *user_p,
3928+ uint32_t frequency);
3929+ ```
3930+
3931+ - `stop_cb` - periodically called callback (passing NULL disables this feature)
3932+ - `user_p` - user pointer passed to the `stop_cb` function
3933+ - `frequency` - frequency of calling the `stop_cb` function
3934+
3935+ **Example**
3936+
3937+ ```c
3938+ static jerry_value_t
3939+ vm_exec_stop_callback (void *user_p)
3940+ {
3941+ static int countdown = 10;
3942+
3943+ while (countdown > 0)
3944+ {
3945+ countdown--;
3946+ return jerry_create_undefined();
3947+ }
3948+
3949+ // The error flag is added automatically.
3950+ return jerry_create_string((const jerry_char_t *) "Abort script");
3951+ }
3952+
3953+ {
3954+ jerry_init (JERRY_INIT_EMPTY);
3955+
3956+ jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16);
3957+
3958+ // Inifinte loop.
3959+ const char *src_p = "while(true) {}";
3960+
3961+ jerry_value_t src = jerry_parse ((jerry_char_t *) src_p, strlen (src_p), false);
3962+ jerry_release_value (jerry_run (src));
3963+ jerry_release_value (src);
3964+ }
3965+ ```
3966+
3967+ **See also**
3968+
3969+ - [jerry_init](#jerry_init)
3970+ - [jerry_cleanup](#jerry_cleanup)
3971+ - [jerry_parse](#jerry_parse)
3972+ - [jerry_run](#jerry_run)
0 commit comments