@@ -36,6 +36,7 @@ Possible compile time enabled feature types:
3636 - JERRY_FEATURE_SNAPSHOT_SAVE - saving snapshot files
3737 - JERRY_FEATURE_SNAPSHOT_EXEC - executing snapshot files
3838 - JERRY_FEATURE_DEBUGGER - debugging
39+ - JERRY_FEATURE_VM_EXEC_STOP - stopping ECMAScript execution
3940
4041## jerry_char_t
4142
@@ -234,6 +235,28 @@ typedef bool (*jerry_object_property_foreach_t) (const jerry_value_t property_na
234235 void *user_data_p);
235236```
236237
238+ ## jerry_vm_exec_stop_callback_t
239+
240+ **Summary**
241+
242+ Callback which tells whether the ECMAScript execution should be stopped.
243+ If it returns with undefined value the ECMAScript execution continues.
244+ Otherwise the result is thrown by the engine (if the error flag is not
245+ set for the returned value the engine automatically sets it). The
246+ callback function might be called again even if it threw an error.
247+ In this case the function must throw the same error again.
248+
249+ **Prototype**
250+
251+ ```c
252+ typedef jerry_value_t (*jerry_vm_exec_stop_callback_t) (void *user_p);
253+ ```
254+
255+ **See also**
256+
257+ - [jerry_set_vm_exec_stop_callback](#jerry_set_vm_exec_stop_callback)
258+
259+
237260# General engine functions
238261
239262## jerry_init
@@ -3896,3 +3919,81 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p,
38963919- [jerry_init](#jerry_init)
38973920- [jerry_cleanup](#jerry_cleanup)
38983921- [jerry_register_magic_strings](#jerry_register_magic_strings)
3922+
3923+
3924+ # Miscellaneous functions
3925+
3926+ ## jerry_set_vm_exec_stop_callback
3927+
3928+ **Summary**
3929+
3930+ When JERRY_FEATURE_VM_EXEC_STOP is enabled a callback function can be
3931+ specified by this function. This callback is periodically called when
3932+ JerryScript executes an ECMAScript program.
3933+
3934+ If the callback returns with undefined value the ECMAScript execution
3935+ continues. Otherwise the result is thrown by the engine (if the error
3936+ flag is not set for the returned value the engine automatically sets
3937+ it). The callback function might be called again even if it threw
3938+ an error. In this case the function must throw the same error again.
3939+
3940+ To reduce the CPU overhead of constantly checking the termination
3941+ condition the callback is called when a backward jump is executed
3942+ or an exception is caught. Setting the `frequency` to a greater
3943+ than `1` value reduces this overhead further. If its value is N
3944+ only every Nth event (backward jump, etc.) trigger the next check.
3945+
3946+
3947+ **Prototype**
3948+
3949+ ```c
3950+ void
3951+ jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb,
3952+ void *user_p,
3953+ uint32_t frequency);
3954+ ```
3955+
3956+ - `stop_cb` - periodically called callback (passing NULL disables this feature)
3957+ - `user_p` - user pointer passed to the `stop_cb` function
3958+ - `frequency` - frequency of calling the `stop_cb` function
3959+
3960+ **Example**
3961+
3962+ ```c
3963+ static jerry_value_t
3964+ vm_exec_stop_callback (void *user_p)
3965+ {
3966+ static int countdown = 10;
3967+
3968+ while (countdown > 0)
3969+ {
3970+ countdown--;
3971+ return jerry_create_undefined ();
3972+ }
3973+
3974+ // The error flag is added automatically.
3975+ return jerry_create_string ((const jerry_char_t *) "Abort script");
3976+ }
3977+
3978+ {
3979+ jerry_init (JERRY_INIT_EMPTY);
3980+
3981+ jerry_set_vm_exec_stop_callback (vm_exec_stop_callback, &countdown, 16);
3982+
3983+ // Inifinte loop.
3984+ const char *src_p = "while(true) {}";
3985+
3986+ jerry_value_t src = jerry_parse ((jerry_char_t *) src_p, strlen (src_p), false);
3987+ jerry_release_value (jerry_run (src));
3988+ jerry_release_value (src);
3989+ jerry_cleanup ();
3990+ }
3991+ ```
3992+
3993+ **See also**
3994+
3995+ - [jerry_init](#jerry_init)
3996+ - [jerry_cleanup](#jerry_cleanup)
3997+ - [jerry_parse](#jerry_parse)
3998+ - [jerry_run](#jerry_run)
3999+ - [jerry_vm_exec_stop_callback_t](#jerry_vm_exec_stop_callback_t)
0 commit comments