Skip to content

Add adjustWasmImports incoming Module setting #23794

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 3 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
3 changes: 2 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
6 changes: 6 additions & 0 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/other/test_adjust_wasm_imports.c
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15690,6 +15690,17 @@ 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.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):
# 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
Expand Down
Loading