Skip to content

[embind] Initialize embind on wasm workers. #24191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/libwasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ addToLibrary({
writeStackCookie();
#endif

#if EMBIND
// Embind must initialize itself on all threads, as it generates support JS.
__embind_initialize_bindings();
#endif

#if AUDIO_WORKLET
// Audio Worklets do not have postMessage()ing capabilities.
if (typeof AudioWorkletGlobalScope === 'undefined') {
Expand Down
34 changes: 34 additions & 0 deletions test/embind/test_embind_wasm_workers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <emscripten.h>
#include <emscripten/console.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <emscripten/wasm_worker.h>

using namespace emscripten;

int foo() {
return 42;
}

void do_exit() {
emscripten_out("do_exit");
emscripten_terminate_all_wasm_workers();
}

void run_in_worker() {
emscripten_out("Hello from Wasm Worker!");
int result = val::module_property("foo")().as<int>();
assert(result == 42);
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, do_exit);
}

int main() {
emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker(/*stackSize: */1024);
emscripten_wasm_worker_post_function_v(worker, run_in_worker);
emscripten_exit_with_live_runtime();
}

EMSCRIPTEN_BINDINGS(xxx) {
function("foo", &foo);
}
2 changes: 2 additions & 0 deletions test/embind/test_embind_wasm_workers.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello from Wasm Worker!
do_exit
4 changes: 4 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7588,6 +7588,10 @@ def test_embind_no_rtti_followed_by_rtti(self):
self.emcc_args += ['-lembind', '-fno-rtti', '-frtti']
self.do_run(src, '418\ndotest returned: 42\n')

@no_asan('ASan does not support WASM_WORKERS')
def test_embind_wasm_workers(self):
self.do_run_in_out_file_test('embind/test_embind_wasm_workers.cpp', emcc_args=['-lembind', '-sWASM_WORKERS'])

@parameterized({
'': ('DEFAULT', False),
'all': ('ALL', False),
Expand Down
5 changes: 2 additions & 3 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,6 @@ def setup_pthreads():
'_emscripten_thread_crashed',
]

if settings.EMBIND:
settings.REQUIRED_EXPORTS.append('_embind_initialize_bindings')

if settings.MAIN_MODULE:
settings.REQUIRED_EXPORTS += [
'_emscripten_dlsync_self',
Expand Down Expand Up @@ -1381,6 +1378,8 @@ def limit_incoming_module_api():
# Workaround for embind+LTO issue:
# https://github.com/emscripten-core/emscripten/issues/21653
settings.REQUIRED_EXPORTS.append('__getTypeName')
if settings.PTHREADS or settings.WASM_WORKERS:
settings.REQUIRED_EXPORTS.append('_embind_initialize_bindings')

if options.emit_tsd:
settings.EMIT_TSD = True
Expand Down