emscripten-core / emscripten-core/emscripten
Access js Worker object after wasm_worker creation
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
## Background
We want to transfer a readable stream object (`postMessage(stream, {transfer: [stream]})`) to a wasm worker to be able to read the stream from the worker thread.
We intend to start a `message` listener through EM_JS like the following example, with the addition of checking that it is the right message and not an internal Emscripten one:
```js
addEventListener("message", async (event) => {
const reader = event.data.readableStream.getReader();
while (true) {
const frame = await reader.read();
if (frame.done) {
break;
}
wasmModule.insertFrame(frame.data);
}
});
```
But to post the message that we can listen to, you would need to access the `Worker` object in JS.
## Possible solutions
1. Be able to return `emscripten_wasm_worker_t` or similar in C++ and get get a `Worker` returned in JS.
2. Have a public lookup method on the module where the `id` of the Worker (`emscripten_wasm_worker_t`) can be used to fetch the corresponding Worker object.
Alternative solutions may exist, and any method of accessing the Worker object is acceptable.
Contributor guide
Assessment
This issue has not been assessed yet.