-
Notifications
You must be signed in to change notification settings - Fork 688
Description
TL;DR: Is there any other way to implement a Promise job queue than what's implemented in the default port? If not, should it not be merged into jerry-core?
Source of my confusion:
- If Promises are available/enabled then those JS functions that are in
thens orcatches are to be executed after the main script completed. Until they get their turn, they are enqueued byjerry_port_jobqueue_enqueue. - Enqueued jobs must be executed, otherwise they keep references to their environment and memory will not be freed. (Just try to comment out
jerry_port_default_jobqueue_runin main-unix.c, the debug-built jerry tool will assert out at cleanup when running a script with Promises, because heap does not get emptied.) - However, there is no general API to execute the enqueued jobs. The default port provides
jerry_port_default_jobqueue_run, but it is specific to that port. - What's more, the default port implementation uses the engine's internal memory handling routines (
jmem_heap_alloc_block,jmem_heap_free_block), which is quite a no-no IMHO.
I've taken a quick look at another VM (JavaScriptCore) and command line tool (jsc), how it handles Promises and enqueued jobs. Without digging too deep, it seems to me that there the VM has an API call to execute such jobs (called microtasks, if I'm not mistaken).
(vm.drainMicrotasks(); at https://trac.webkit.org/browser/webkit/trunk/Source/JavaScriptCore/jsc.cpp#L3522)
Wouldn't it be better for us, too, to integrate Promise job handling into the engine. E.g., something like
jerry_run_enqueuedto run one enqueued job,jerry_run_all_enqueuedto run until the queue gets empty (replacement forjerry_port_default_jobqueue_runand analogous to JSC'svm.drainMicrotasks, I think).
Advantages:
- the whole API needed to run a Promise-enabled engine is port-independent (in addition to the above discussed,
jerry_initcan do the queue initialization instead ofjerry_port_default_jobqueue_init), - no API layering violation,
- less burden on port implementers.
However, I'm not sure whether there are any use cases for which this approach would be inappropriate.
Feedbacks are welcome.