From 6eb821698d1e73723eeee5a0a4e0ef318f890ee7 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 28 Feb 2025 12:15:34 +0100 Subject: [PATCH 1/3] Add adjustWasmImports incoming Module setting This provides a way to add symbols to the wasmImports dynamically. This doesn't allow anything new compared to `instantiateWasm` except that it doesn't interfere with sourcemaps or ASAN and it doesn't require reproducing the somewhat tricky behavior in the standard `instantiateWasm` function. As an alternative, we could expose something like `instantiateWasmDefault` to the user, and then they could do this like: ```js Module['instantiateWasm'] = (imports, successCallback) => { adjustWasmImports(imports); instantiateWasmDefault(imports, successCallback); } ``` --- src/preamble.js | 6 ++++++ src/settings.js | 2 +- test/other/test_adjust_wasm_imports.c | 13 +++++++++++++ test/test_other.py | 10 ++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/other/test_adjust_wasm_imports.c diff --git a/src/preamble.js b/src/preamble.js index 4d516dab9d9a1..2bcbaf73cbde6 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -977,6 +977,12 @@ function getWasmImports() { var info = getWasmImports(); +#if expectToReceiveOnModule('adjustWasmImports') + if (Module['adjustWasmImports']) { + Module['adjustWasmImports'](info); + } +#endif + #if expectToReceiveOnModule('instantiateWasm') // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback // to manually instantiate the Wasm module themselves. This allows pages to diff --git a/src/settings.js b/src/settings.js index 533a797aad347..52d56bfb34c95 100644 --- a/src/settings.js +++ b/src/settings.js @@ -999,7 +999,7 @@ var INCOMING_MODULE_JS_API = [ 'SDL_numSimultaneouslyQueuedBuffers', 'INITIAL_MEMORY', 'wasmMemory', 'arguments', 'buffer', 'canvas', 'doNotCaptureKeyboard', 'dynamicLibraries', 'elementPointerLock', 'extraStackTrace', 'forcedAspectRatio', - 'instantiateWasm', 'keyboardListeningElement', 'freePreloadedMediaOnUse', + 'instantiateWasm', 'adjustWasmImports', 'keyboardListeningElement', 'freePreloadedMediaOnUse', 'loadSplitModule', 'locateFile', 'logReadFiles', 'mainScriptUrlOrBlob', 'mem', 'monitorRunDependencies', 'noExitRuntime', 'noInitialRun', 'onAbort', 'onCustomMessage', 'onExit', 'onFree', 'onFullScreen', 'onMalloc', diff --git a/test/other/test_adjust_wasm_imports.c b/test/other/test_adjust_wasm_imports.c new file mode 100644 index 0000000000000..0cff6461bc6de --- /dev/null +++ b/test/other/test_adjust_wasm_imports.c @@ -0,0 +1,13 @@ +#include "assert.h" +#include "stdio.h" + +int __attribute__((import_module("env"), import_name("js_fun"))) js_func(int x, int y); + +int main() { + int x = 7; + int y = 9; + printf("Calling js_func(%d, %d);\n", x, y); + int res = js_func(x, y); + assert(res == 16); + return 0; +} diff --git a/test/test_other.py b/test/test_other.py index dac24ea5ead4f..9b2ac746df1fc 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -15690,6 +15690,16 @@ def test_instantiate_wasm(self): }''') self.do_runf('test_manual_wasm_instantiate.c', emcc_args=['--pre-js=pre.js']) + @also_with_modularize + def test_adjust_wasm_imports(self): + create_file('pre.js', ''' + Module['adjustWasmImports'] = (imports) => { + imports.env.js_fun = (x, y) => x + y; + }; + ''') + self.emcc_args.remove('-Werror') + self.do_runf('other/test_adjust_wasm_imports.c', emcc_args=['--pre-js=pre.js', '-sERROR_ON_UNDEFINED_SYMBOLS=0']) + def test_late_module_api_assignment(self): # When sync instantiation is used (or when async/await is used in MODULARIZE mode) certain # Module properties cannot be assigned in `--post-js` code because its too late by the time From 8817a2b005efbea60dceb73c12d6547868caf669 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 28 Feb 2025 19:47:40 +0100 Subject: [PATCH 2/3] Move out of default incoming_js_api into extra_incoming_js_api --- emcc.py | 3 ++- src/settings.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/emcc.py b/emcc.py index 9c160a39a6443..06e606601a63c 100644 --- a/emcc.py +++ b/emcc.py @@ -73,7 +73,8 @@ # These symbol names are allowed in INCOMING_MODULE_JS_API but are not part of the # default set. EXTRA_INCOMING_JS_API = [ - 'fetchSettings' + 'fetchSettings', + 'adjustWasmImports', ] SIMD_INTEL_FEATURE_TOWER = ['-msse', '-msse2', '-msse3', '-mssse3', '-msse4.1', '-msse4.2', '-msse4', '-mavx', '-mavx2'] diff --git a/src/settings.js b/src/settings.js index 52d56bfb34c95..533a797aad347 100644 --- a/src/settings.js +++ b/src/settings.js @@ -999,7 +999,7 @@ var INCOMING_MODULE_JS_API = [ 'SDL_numSimultaneouslyQueuedBuffers', 'INITIAL_MEMORY', 'wasmMemory', 'arguments', 'buffer', 'canvas', 'doNotCaptureKeyboard', 'dynamicLibraries', 'elementPointerLock', 'extraStackTrace', 'forcedAspectRatio', - 'instantiateWasm', 'adjustWasmImports', 'keyboardListeningElement', 'freePreloadedMediaOnUse', + 'instantiateWasm', 'keyboardListeningElement', 'freePreloadedMediaOnUse', 'loadSplitModule', 'locateFile', 'logReadFiles', 'mainScriptUrlOrBlob', 'mem', 'monitorRunDependencies', 'noExitRuntime', 'noInitialRun', 'onAbort', 'onCustomMessage', 'onExit', 'onFree', 'onFullScreen', 'onMalloc', From 4a4e1ee58ccddc1e38f55617cdf6949a9b77f239 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 28 Feb 2025 20:00:38 +0100 Subject: [PATCH 3/3] Fix test --- test/test_other.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_other.py b/test/test_other.py index 9b2ac746df1fc..c0ac08690a376 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -15698,6 +15698,7 @@ def test_adjust_wasm_imports(self): }; ''') self.emcc_args.remove('-Werror') + self.set_setting('INCOMING_MODULE_JS_API', 'adjustWasmImports') self.do_runf('other/test_adjust_wasm_imports.c', emcc_args=['--pre-js=pre.js', '-sERROR_ON_UNDEFINED_SYMBOLS=0']) def test_late_module_api_assignment(self):