Skip to content

Commit 2462733

Browse files
authored
Avoid using Module.instantiateWasm for pthreads. (#22912)
This leave the Module.instantiateWasm API for users to overide as they please. Fixes: #22905
1 parent f5aeebd commit 2462733

File tree

5 files changed

+20
-30
lines changed

5 files changed

+20
-30
lines changed

src/preamble.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,6 @@ function getWasmImports() {
930930
// Create the wasm instance.
931931
// Receives the wasm imports, returns the exports.
932932
function createWasm() {
933-
var info = getWasmImports();
934933
// Load the wasm module and create an instance of using native support in the JS engine.
935934
// handle a generated wasm instance, receiving its exports and
936935
// performing other necessary setup
@@ -1054,6 +1053,8 @@ function createWasm() {
10541053
}
10551054
#endif // WASM_ASYNC_COMPILATION
10561055

1056+
var info = getWasmImports();
1057+
10571058
#if expectToReceiveOnModule('instantiateWasm')
10581059
// User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
10591060
// to manually instantiate the Wasm module themselves. This allows pages to
@@ -1076,6 +1077,20 @@ function createWasm() {
10761077
}
10771078
#endif
10781079

1080+
#if PTHREADS
1081+
if (ENVIRONMENT_IS_PTHREAD) {
1082+
return new Promise((resolve) => {
1083+
wasmModuleReceived = (module) => {
1084+
// Instantiate from the module posted from the main thread.
1085+
// We can just use sync instantiation in the worker.
1086+
var instance = new WebAssembly.Instance(module, getWasmImports());
1087+
receiveInstance(instance, module);
1088+
resolve();
1089+
};
1090+
});
1091+
}
1092+
#endif
1093+
10791094
wasmBinaryFile ??= findWasmBinary();
10801095

10811096
#if WASM_ASYNC_COMPILATION

src/runtime_pthread.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var workerID = 0;
1616

1717
if (ENVIRONMENT_IS_PTHREAD) {
1818
#if !MINIMAL_RUNTIME
19-
var wasmPromiseResolve;
19+
var wasmModuleReceived;
2020
#endif
2121

2222
#if ENVIRONMENT_MAY_BE_NODE
@@ -62,27 +62,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
6262
}
6363
self.alert = threadAlert;
6464

65-
#if !MINIMAL_RUNTIME
66-
Module['instantiateWasm'] = (info, receiveInstance) => {
67-
return new Promise((resolve) => {
68-
wasmPromiseResolve = (module) => {
69-
// Instantiate from the module posted from the main thread.
70-
// We can just use sync instantiation in the worker.
71-
var instance = new WebAssembly.Instance(module, getWasmImports());
72-
#if RELOCATABLE || MAIN_MODULE
73-
receiveInstance(instance, module);
74-
#else
75-
// TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193,
76-
// the above line no longer optimizes out down to the following line.
77-
// When the regression is fixed, we can remove this if/else.
78-
receiveInstance(instance);
79-
#endif
80-
resolve();
81-
};
82-
});
83-
}
84-
#endif
85-
8665
// Turn unhandled rejected promises into errors so that the main thread will be
8766
// notified about them.
8867
self.onunhandledrejection = (e) => { throw e.reason || e; };
@@ -164,7 +143,7 @@ if (ENVIRONMENT_IS_PTHREAD) {
164143
Module['wasm'] = msgData.wasmModule;
165144
loadModule();
166145
#else
167-
wasmPromiseResolve(msgData.wasmModule);
146+
wasmModuleReceived(msgData.wasmModule);
168147
#endif // MINIMAL_RUNTIME
169148
} else if (cmd === 'run') {
170149
#if ASSERTIONS
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4244
1+
4195
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8800
1+
8657

tools/link.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,6 @@ def setup_pthreads():
495495

496496
default_setting('DEFAULT_PTHREAD_STACK_SIZE', settings.STACK_SIZE)
497497

498-
if not settings.MINIMAL_RUNTIME and 'instantiateWasm' not in settings.INCOMING_MODULE_JS_API:
499-
# pthreads runtime depends on overriding instantiateWasm
500-
settings.INCOMING_MODULE_JS_API.append('instantiateWasm')
501-
502498
# Functions needs by runtime_pthread.js
503499
settings.REQUIRED_EXPORTS += [
504500
'_emscripten_thread_free_data',

0 commit comments

Comments
 (0)