Skip to content

Commit 00c5c7d

Browse files
authored
Fix exporting of native symbol aliases (e.g. wasmTable) (#25539)
This was broken by #25376.
1 parent fbf6b6d commit 00c5c7d

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/modules.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ function exportSymbol(name) {
433433
function exportRuntimeSymbols() {
434434
// optionally export something.
435435
function shouldExport(name) {
436+
// Native exports are not available to be exported initially. Instead,
437+
// they get exported later in `assignWasmExports`.
438+
if (nativeAliases[name]) {
439+
return false;
440+
}
436441
// If requested to be exported, export it.
437442
if (EXPORTED_RUNTIME_METHODS.has(name)) {
438443
// Unless we are in MODULARIZE=instance mode then HEAP objects are

test/test_other.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4948,6 +4948,35 @@ def test_jslib_aliases(self, args):
49484948
'''
49494949
self.do_runf('main.c', expected, cflags=['--js-library', 'foo.js'] + args)
49504950

4951+
@parameterized({
4952+
'': ([],),
4953+
'closure': (['--closure=1'],),
4954+
})
4955+
def test_jslib_export_alias(self, args):
4956+
create_file('lib.js', '''
4957+
addToLibrary({
4958+
$foo: 'main',
4959+
$bar: '__indirect_function_table',
4960+
$baz: 'memory',
4961+
});
4962+
''')
4963+
create_file('extern_pre.js', r'''
4964+
Module = {
4965+
onRuntimeInitialized: () => {
4966+
const assert = require('assert');
4967+
console.log("onRuntimeInitialized");
4968+
console.log('foo:', typeof Module.foo)
4969+
console.log('bar:', typeof Module.bar)
4970+
console.log('baz:', typeof Module.baz)
4971+
assert(Module.foo instanceof Function);
4972+
assert(Module.bar instanceof WebAssembly.Table);
4973+
assert(Module.baz instanceof WebAssembly.Memory);
4974+
console.log('done');
4975+
}
4976+
};
4977+
''')
4978+
self.do_runf('hello_world.c', 'done\n', cflags=['--js-library=lib.js', '--extern-pre-js=extern_pre.js', '-sEXPORTED_RUNTIME_METHODS=foo,bar,baz'] + args)
4979+
49514980
def test_postjs_errors(self):
49524981
create_file('post.js', '#preprocess\n#error This is an error')
49534982
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--post-js', 'post.js'])

tools/emscripten.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ def create_receiving(function_exports, other_exports, library_symbols, aliases):
980980
assert alias in exports, f'expected alias target ({alias}) to be exported'
981981
alias_inverse_map.setdefault(alias, []).append(sym)
982982

983+
do_module_exports = (settings.MODULARIZE or not settings.MINIMAL_RUNTIME) and settings.MODULARIZE != 'instance'
983984
receiving.append('\nfunction assignWasmExports(wasmExports) {')
984985
for sym, sig in exports.items():
985986
is_function = sig is not None
@@ -988,11 +989,13 @@ def create_receiving(function_exports, other_exports, library_symbols, aliases):
988989
if generate_dyncall_assignment and is_function and sym.startswith('dynCall_'):
989990
sig_str = sym.replace('dynCall_', '')
990991
assignment += f" = dynCalls['{sig_str}']"
991-
if (settings.MODULARIZE or not settings.MINIMAL_RUNTIME) and should_export(mangled) and settings.MODULARIZE != 'instance':
992+
if do_module_exports and should_export(mangled):
992993
assignment += f" = Module['{mangled}']"
993994
if sym in alias_inverse_map:
994995
for target in alias_inverse_map[sym]:
995996
assignment += f" = {target}"
997+
if do_module_exports and target in settings.EXPORTED_RUNTIME_METHODS:
998+
assignment += f" = Module['{target}']"
996999
if is_function and install_debug_wrapper(sym):
9971000
nargs = len(sig.params)
9981001
receiving.append(f" {assignment} = createExportWrapper('{sym}', {nargs});")

0 commit comments

Comments
 (0)