Skip to content

Commit 81054ef

Browse files
authored
[embind] Initialize embind on wasm workers. (#24191)
Fixes #24189
1 parent 9b715a9 commit 81054ef

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

src/lib/libwasm_worker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ addToLibrary({
135135
writeStackCookie();
136136
#endif
137137

138+
#if EMBIND
139+
// Embind must initialize itself on all threads, as it generates support JS.
140+
__embind_initialize_bindings();
141+
#endif
142+
138143
#if AUDIO_WORKLET
139144
// Audio Worklets do not have postMessage()ing capabilities.
140145
if (typeof AudioWorkletGlobalScope === 'undefined') {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include <emscripten.h>
3+
#include <emscripten/console.h>
4+
#include <emscripten/bind.h>
5+
#include <emscripten/val.h>
6+
#include <emscripten/wasm_worker.h>
7+
8+
using namespace emscripten;
9+
10+
int foo() {
11+
return 42;
12+
}
13+
14+
void do_exit() {
15+
emscripten_out("do_exit");
16+
emscripten_terminate_all_wasm_workers();
17+
}
18+
19+
void run_in_worker() {
20+
emscripten_out("Hello from Wasm Worker!");
21+
int result = val::module_property("foo")().as<int>();
22+
assert(result == 42);
23+
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT, do_exit);
24+
}
25+
26+
int main() {
27+
emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker(/*stackSize: */1024);
28+
emscripten_wasm_worker_post_function_v(worker, run_in_worker);
29+
emscripten_exit_with_live_runtime();
30+
}
31+
32+
EMSCRIPTEN_BINDINGS(xxx) {
33+
function("foo", &foo);
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello from Wasm Worker!
2+
do_exit

test/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7588,6 +7588,10 @@ def test_embind_no_rtti_followed_by_rtti(self):
75887588
self.emcc_args += ['-lembind', '-fno-rtti', '-frtti']
75897589
self.do_run(src, '418\ndotest returned: 42\n')
75907590

7591+
@no_asan('ASan does not support WASM_WORKERS')
7592+
def test_embind_wasm_workers(self):
7593+
self.do_run_in_out_file_test('embind/test_embind_wasm_workers.cpp', emcc_args=['-lembind', '-sWASM_WORKERS'])
7594+
75917595
@parameterized({
75927596
'': ('DEFAULT', False),
75937597
'all': ('ALL', False),

tools/link.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,6 @@ def setup_pthreads():
516516
'_emscripten_thread_crashed',
517517
]
518518

519-
if settings.EMBIND:
520-
settings.REQUIRED_EXPORTS.append('_embind_initialize_bindings')
521-
522519
if settings.MAIN_MODULE:
523520
settings.REQUIRED_EXPORTS += [
524521
'_emscripten_dlsync_self',
@@ -1381,6 +1378,8 @@ def limit_incoming_module_api():
13811378
# Workaround for embind+LTO issue:
13821379
# https://github.com/emscripten-core/emscripten/issues/21653
13831380
settings.REQUIRED_EXPORTS.append('__getTypeName')
1381+
if settings.PTHREADS or settings.WASM_WORKERS:
1382+
settings.REQUIRED_EXPORTS.append('_embind_initialize_bindings')
13841383

13851384
if options.emit_tsd:
13861385
settings.EMIT_TSD = True

0 commit comments

Comments
 (0)