emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Expose Embindings in wasm worker

Open lindell opened this issue 6 months ago • 3 comments
trafficstars

Hello.

I have a use-case where I need to run some EM_JS code in a WASM_WORKER. The function executed will be asynchronous (listen to a message) and needs to call back into WASM again. When testing this in the main thread, Module in the scope of EM_JS exist and does contain all bindings, but they do no exist when run in the Worker context.

EM_JS(void, log_from_console, (), {
  self.addEventListener("message", (msg) => {
     Module.myMethod(msg.data.someData);
   });
});

Can an option to enable the bindings in the WASM_WORKER Module instance be created? Or that the binding are exported by default.

lindell avatar Apr 25 '25 14:04 lindell

It could be that embind simply does not work today (or rather have never been tested) inside of wasm workers.

@brendandahl @juj would know more, but I don't see any embind + wasm_workers tests in the test suite (at least not at first glance).

I don't think this is specific to EM_JS, but rather that embind just has not been initialized in the worker.

sbc100 avatar Apr 25 '25 15:04 sbc100

I don't think we have anyone using embind+wasm workers. We just need to call __embind_initialize_bindings like we do for pthreads. I can call that in _wasmWorkerInitializeRuntime, or @sbc100 is there any shared initialization code for pthreads and wasm workers that could call it?

brendandahl avatar Apr 25 '25 17:04 brendandahl

_wasmWorkerInitializeRuntime should work.

sbc100 avatar Apr 25 '25 17:04 sbc100