Skip to content

Commit

Permalink
Ensure exports are correctly handled when instantiateWasm is present (e…
Browse files Browse the repository at this point in the history
…mscripten-core#18711)

Split out from emscripten-core#18705

I noticed while working on emscripten-core#18705 that the exports returned by
`instantiateWasm` (which is used in pthreads to load the wasm module)
ere not the same (potentially wrapped/modified) exports that were
created during `receiveInstance`.

I guess someone else must have noticed this and added the extra
call to `Asyncify.instrumentWasmExports`.  This should not be necessary
since `receiveInstance` already takes care of that.
  • Loading branch information
sbc100 authored Feb 13, 2023
1 parent 7bec7ec commit e04f291
Show file tree
Hide file tree
Showing 29 changed files with 37 additions and 41 deletions.
12 changes: 4 additions & 8 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ function createWasm() {
removeRunDependency('wasm-instantiate');
#endif // ~USE_PTHREADS

return exports;
}
// wait for the pthread pool (if any)
addRunDependency('wasm-instantiate');
Expand Down Expand Up @@ -1106,11 +1107,7 @@ function createWasm() {
wasmSourceMap = resetPrototype(WasmSourceMap, Module['wasmSourceMapData']);
#endif
try {
var exports = Module['instantiateWasm'](info, receiveInstance);
#if ASYNCIFY
exports = Asyncify.instrumentWasmExports(exports);
#endif
return exports;
return Module['instantiateWasm'](info, receiveInstance);
} catch(e) {
err('Module.instantiateWasm callback failed with error: ' + e);
#if MODULARIZE
Expand Down Expand Up @@ -1140,14 +1137,13 @@ function createWasm() {
#else
var result = instantiateSync(wasmBinaryFile, info);
#if USE_PTHREADS || MAIN_MODULE
receiveInstance(result[0], result[1]);
return receiveInstance(result[0], result[1]);
#else
// TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193,
// the above line no longer optimizes out down to the following line.
// When the regression is fixed, we can remove this if/else.
receiveInstance(result[0]);
return receiveInstance(result[0]);
#endif
return Module['asm']; // exports were assigned here
#endif
}

Expand Down
12 changes: 6 additions & 6 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ var dbg = threadPrintErr;
Module['instantiateWasm'] = (info, receiveInstance) => {
// Instantiate from the module posted from the main thread.
// We can just use sync instantiation in the worker.
var instance = new WebAssembly.Instance(Module['wasmModule'], info);
var module = Module['wasmModule'];
// We don't need the module anymore; new threads will be spawned from the main thread.
Module['wasmModule'] = null;
var instance = new WebAssembly.Instance(module, info);
#if RELOCATABLE || MAIN_MODULE
receiveInstance(instance, Module['wasmModule']);
return receiveInstance(instance, module);
#else
// TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193,
// the above line no longer optimizes out down to the following line.
// When the regression is fixed, we can remove this if/else.
receiveInstance(instance);
return receiveInstance(instance);
#endif
// We don't need the module anymore; new threads will be spawned from the main thread.
Module['wasmModule'] = null;
return instance.exports;
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26036
26053
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26000
26017
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30573
30590
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25715
25732
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30573
30590
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26036
26053
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23275
23304
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8679
8708
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6166
6183
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5992
6009
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5992
6009
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5951
5968
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27767
27776
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4666
4683
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5257
5274
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5346
5363
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6121
6138
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6464
6482
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5869
5887
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5793
5810
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5343
5360
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5346
5363
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5346
5363
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15757
15771
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
63151
63142
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
38439
38430
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62503
62494

0 comments on commit e04f291

Please sign in to comment.