jerryscript-project / jerryscript

Ultra-lightweight JavaScript engine for the Internet of Things.
https://jerryscript.net
Apache License 2.0
6.95k stars 672 forks source link

Is it possible to implement worker in JerryScript? #4864

Open Sherry0901 opened 2 years ago

Sherry0901 commented 2 years ago

Is it possible to implement worker(like web worker) in JerryScript? if it's available, where can I get some examples or guides?

rerobika commented 2 years ago

Hi, please check the following example: https://github.com/jerryscript-project/jerryscript/blob/master/docs/02.API-REFERENCE.md#jerry_context_alloc

Sherry0901 commented 2 years ago

thanks! I see, so JerryScript can create multi-threads. But I am still confused about the details. How can I implement postMessage,addEventListener,removeEventListener...in JerryScript? @rerobika

rerobika commented 2 years ago

I see, so JerryScript can create multi-threads.

Nope, JerryScript cannot create threads. The linked example only shows that the engine is able to work in a multi threaded context. Please note that those independent contexts cannot pass value to each other.

If I were you I'd definitely use Promises or async functions to build and own event queue to handle the listener events. However, it's still single threaded, but the jobs can communicate with each other.

Sherry0901 commented 2 years ago

Thanks for your advice! I'll try it.