Skip to content

Commit c07d48a

Browse files
authored
[esm-integration] Enable imported memory (#24130)
This is the first step to getting threads working.
1 parent e1be6ed commit c07d48a

File tree

6 files changed

+31
-13
lines changed

6 files changed

+31
-13
lines changed

src/postlibrary.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
function processModuleArgs()
66
#endif
77
{
8-
#if IMPORTED_MEMORY
8+
#if IMPORTED_MEMORY && !WASM_ESM_INTEGRATION
9+
// With WASM_ESM_INTEGRATION this has to happen at the top level and not
10+
// delayed until processModuleArgs.
911
initMemory();
1012
#endif
1113

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ if (typeof WebAssembly != 'object') {
4646

4747
// Wasm globals
4848

49-
#if !WASM_ESM_INTEGRATION
49+
#if !WASM_ESM_INTEGRATION || IMPORTED_MEMORY
5050
var wasmMemory;
5151
#endif
5252

src/runtime_init_memory.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function initMemory() {
2222
} else
2323
#endif
2424
{
25-
var INITIAL_MEMORY = {{{ makeModuleReceiveExpr('INITIAL_MEMORY', INITIAL_MEMORY) }}}
25+
var INITIAL_MEMORY = {{{ makeModuleReceiveExpr('INITIAL_MEMORY', INITIAL_MEMORY) }}};
2626

2727
#if ASSERTIONS
2828
assert(INITIAL_MEMORY >= {{{STACK_SIZE}}}, 'INITIAL_MEMORY should be larger than STACK_SIZE, was ' + INITIAL_MEMORY + '! (STACK_SIZE=' + {{{STACK_SIZE}}} + ')');
@@ -56,3 +56,6 @@ function initMemory() {
5656
updateMemoryViews();
5757
}
5858

59+
#if WASM_ESM_INTEGRATION
60+
initMemory();
61+
#endif

test/test_core.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9577,15 +9577,19 @@ def test_wasm_worker_malloc(self):
95779577
def test_wasm_worker_wait_async(self):
95789578
self.do_runf('atomic/test_wait_async.c', emcc_args=['-sWASM_WORKERS'])
95799579

9580+
@parameterized({
9581+
'': ([],),
9582+
'imported_memory': (['-sIMPORTED_MEMORY'],),
9583+
})
95809584
@esm_integration
9581-
def test_esm_integration_main(self):
9582-
self.do_runf('hello_world.c', 'hello, world!')
9585+
def test_esm_integration_main(self, args):
9586+
self.do_runf('hello_world.c', 'hello, world!', emcc_args=args)
95839587

95849588
@esm_integration
95859589
def test_esm_integration(self):
95869590
# TODO(sbc): WASM_ESM_INTEGRATION doesn't currently work with closure.
95879591
# self.maybe_closure()
9588-
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sEXPORTED_RUNTIME_METHODS=err', '-sEXPORTED_FUNCTIONS=_main,stringToNewUTF8', test_file('core/test_esm_integration.c')] + self.get_emcc_args())
9592+
self.run_process([EMCC, '-o', 'hello_world.mjs', '-sINCOMING_MODULE_JS_API=arguments', '-sEXPORTED_RUNTIME_METHODS=err', '-sEXPORTED_FUNCTIONS=_main,stringToNewUTF8', test_file('core/test_esm_integration.c')] + self.get_emcc_args())
95899593
create_file('runner.mjs', '''
95909594
import init, { err, stringToNewUTF8, _main, _foo } from "./hello_world.mjs";
95919595
await init({arguments: ['foo', 'bar']});

tools/emscripten.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,10 @@ def install_wrapper(sym):
964964
def create_receiving(function_exports):
965965
if settings.WASM_ESM_INTEGRATION:
966966
exports = [f'{f} as {asmjs_mangle(f)}' for f in function_exports]
967-
exports.append('memory as wasmMemory')
968-
exports.append('__indirect_function_table as wasmTable')
967+
if not settings.IMPORTED_MEMORY:
968+
exports.append('memory as wasmMemory')
969+
if not settings.RELOCATABLE:
970+
exports.append('__indirect_function_table as wasmTable')
969971
exports = ',\n '.join(exports)
970972
return f"import {{\n {exports}\n}} from './{settings.WASM_BINARY_FILE}';\n\n"
971973

tools/link.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,10 +794,21 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
794794
if settings.MODULARIZE and settings.MODULARIZE not in [1, 'instance']:
795795
exit_with_error(f'Invalid setting "{settings.MODULARIZE}" for MODULARIZE.')
796796

797+
def limit_incoming_module_api():
798+
if options.oformat == OFormat.HTML and options.shell_path == DEFAULT_SHELL_HTML:
799+
# Out default shell.html file has minimal set of INCOMING_MODULE_JS_API elements that it expects
800+
default_setting('INCOMING_MODULE_JS_API', 'canvas,monitorRunDependencies,onAbort,onExit,print,setStatus'.split(','))
801+
else:
802+
default_setting('INCOMING_MODULE_JS_API', [])
803+
797804
if settings.MODULARIZE == 'instance':
798805
diagnostics.warning('experimental', '-sMODULARIZE=instance is still experimental. Many features may not work or will change.')
799806
if options.oformat != OFormat.MJS:
800807
exit_with_error('emcc: MODULARIZE instance is only compatible with .mjs output files')
808+
limit_incoming_module_api()
809+
for s in ['wasmMemory', 'INITIAL_MEMORY']:
810+
if s in settings.INCOMING_MODULE_JS_API:
811+
exit_with_error(f'emcc: {s} cannot be in INCOMING_MODULE_JS_API in MODULARIZE=instance mode')
801812

802813
if options.oformat in (OFormat.WASM, OFormat.BARE):
803814
if options.emit_tsd:
@@ -965,11 +976,7 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
965976
# are needed in this mode.
966977
default_setting('AUTO_JS_LIBRARIES', 0)
967978
default_setting('ALLOW_UNIMPLEMENTED_SYSCALLS', 0)
968-
if options.oformat == OFormat.HTML and options.shell_path == DEFAULT_SHELL_HTML:
969-
# Out default shell.html file has minimal set of INCOMING_MODULE_JS_API elements that it expects
970-
default_setting('INCOMING_MODULE_JS_API', 'canvas,monitorRunDependencies,onAbort,onExit,print,setStatus'.split(','))
971-
else:
972-
default_setting('INCOMING_MODULE_JS_API', [])
979+
limit_incoming_module_api()
973980

974981
if 'noExitRuntime' in settings.INCOMING_MODULE_JS_API:
975982
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$noExitRuntime')

0 commit comments

Comments
 (0)