Skip to content

Commit 0ec0351

Browse files
authored
Fix DECLARE_ASM_MODULE_EXPORTS=0 for mangled main function (#18895)
With `DECLARE_ASM_MODULE_EXPORTS` the list of wasm exports is statically generated at build time. This means that mangling in the compiler takes care of converted `__main_argc_argv` to `main`. e.g. something like the following is statically generated: ``` var main = Module['main] = asm['__main_argc_argv']; ``` However, with `DECLARE_ASM_MODULE_EXPORTS=0` the exports are dynamically propagated from the wasm exports to the Module object, so we need to apply the same demangling at runtime as we do at build time. The build time version of this logic is here: https://github.com/emscripten-core/emscripten/blob/c4693feae0e366cf7741e9e1725fda5539605d4d/tools/shared.py#L656-L657
1 parent 5548668 commit 0ec0351

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

src/library.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,9 @@ mergeInto(LibraryManager.library, {
36123612

36133613
$asmjsMangle: function(x) {
36143614
var unmangledSymbols = {{{ buildStringArray(WASM_SYSTEM_EXPORTS) }}};
3615+
if (x == '__main_argc_argv') {
3616+
x = 'main';
3617+
}
36153618
return x.indexOf('dynCall_') == 0 || unmangledSymbols.includes(x) ? x : '_' + x;
36163619
},
36173620

test/declare_asm_module_exports.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
#include <emscripten.h>
3+
#include <stdio.h>
4+
5+
int EMSCRIPTEN_KEEPALIVE cFunction(void) { return 1; }
6+
7+
EM_JS(int, jsFunction, (), {
8+
return _cFunction();
9+
});
10+
11+
// Intentional use of __main_argc_argv
12+
int main(int argc, char* argv[]) {
13+
printf("cFunction: %d\n", cFunction());
14+
printf("jsFunction: %d\n", jsFunction());
15+
assert(jsFunction() == 1);
16+
return 0;
17+
}

test/declare_asm_module_exports.cpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/test_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5061,14 +5061,14 @@ def test_closure_in_web_only_target_environment_webgl(self):
50615061
def test_no_declare_asm_module_exports_asmjs(self, args):
50625062
# TODO(sbc): Fix closure warnings with MODULARIZE + WASM=0
50635063
self.ldflags.append('-Wno-error=closure')
5064-
self.btest(test_file('declare_asm_module_exports.cpp'), '1', args=['-sDECLARE_ASM_MODULE_EXPORTS=0', '-sENVIRONMENT=web', '-O3', '--closure=1', '-sWASM=0'] + args)
5064+
self.btest_exit(test_file('declare_asm_module_exports.c'), args=['-sDECLARE_ASM_MODULE_EXPORTS=0', '-sENVIRONMENT=web', '-O3', '--closure=1', '-sWASM=0'] + args)
50655065

50665066
@parameterized({
50675067
'': (1,),
50685068
'2': (2,),
50695069
})
50705070
def test_no_declare_asm_module_exports_wasm_minimal_runtime(self, mode):
5071-
self.btest(test_file('declare_asm_module_exports.cpp'), '1', args=['-sDECLARE_ASM_MODULE_EXPORTS=0', '-sENVIRONMENT=web', '-O3', '--closure=1', f'-sMINIMAL_RUNTIME={mode}'])
5071+
self.btest_exit(test_file('declare_asm_module_exports.c'), args=['-sDECLARE_ASM_MODULE_EXPORTS=0', '-sENVIRONMENT=web', '-O3', '--closure=1', f'-sMINIMAL_RUNTIME={mode}'])
50725072

50735073
# Tests that the different code paths in src/shell_minimal_runtime.html all work ok.
50745074
def test_minimal_runtime_loader_shell(self):

test/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8792,7 +8792,7 @@ def test_no_declare_asm_module_exports(self):
87928792
self.set_setting('DECLARE_ASM_MODULE_EXPORTS', 0)
87938793
self.set_setting('WASM_ASYNC_COMPILATION', 0)
87948794
self.maybe_closure()
8795-
self.do_runf(test_file('declare_asm_module_exports.cpp'), 'jsFunction: 1')
8795+
self.do_runf(test_file('declare_asm_module_exports.c'), 'jsFunction: 1')
87968796
js = read_file('declare_asm_module_exports.js')
87978797
occurances = js.count('cFunction')
87988798
if self.is_optimizing() and '-g' not in self.emcc_args:
@@ -8814,7 +8814,7 @@ def test_minimal_runtime_no_declare_asm_module_exports(self):
88148814
self.maybe_closure()
88158815
self.set_setting('MINIMAL_RUNTIME')
88168816
self.emcc_args += ['--pre-js', test_file('minimal_runtime_exit_handling.js')]
8817-
self.do_runf(test_file('declare_asm_module_exports.cpp'), 'jsFunction: 1')
8817+
self.do_runf(test_file('declare_asm_module_exports.c'), 'jsFunction: 1')
88188818

88198819
# Tests that -sMINIMAL_RUNTIME works well in different build modes
88208820
@no_wasmfs('https://github.com/emscripten-core/emscripten/issues/16816')

0 commit comments

Comments
 (0)