diff --git a/src/closure-externs/closure-externs.js b/src/closure-externs/closure-externs.js index 00aa5a9a158d9..99f417710e6d5 100644 --- a/src/closure-externs/closure-externs.js +++ b/src/closure-externs/closure-externs.js @@ -14,6 +14,7 @@ // Special placeholder for `import.meta` and `await import`. var EMSCRIPTEN$IMPORT$META; var EMSCRIPTEN$AWAIT$IMPORT; +var EMSCRIPTEN$AWAIT; // Don't minify createRequire var createRequire; diff --git a/test/other/codesize/test_codesize_minimal_esm.gzsize b/test/other/codesize/test_codesize_minimal_esm.gzsize index 06415b870d0fc..2107c66fb6bb4 100644 --- a/test/other/codesize/test_codesize_minimal_esm.gzsize +++ b/test/other/codesize/test_codesize_minimal_esm.gzsize @@ -1 +1 @@ -1437 +1429 diff --git a/test/other/codesize/test_codesize_minimal_esm.jssize b/test/other/codesize/test_codesize_minimal_esm.jssize index 4448785bdb2ae..3a874affa19d0 100644 --- a/test/other/codesize/test_codesize_minimal_esm.jssize +++ b/test/other/codesize/test_codesize_minimal_esm.jssize @@ -1 +1 @@ -2982 +2967 diff --git a/test/test_other.py b/test/test_other.py index bfb906229f023..88e9772365a87 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -7170,6 +7170,9 @@ class Descriptor { self.run_process([EMXX, 'src.cpp', '-O2', '-sEXPORT_ALL']) self.assertExists('a.out.js') + def test_modularize_legacy(self): + self.do_runf('hello_world.c', emcc_args=['-sMODULARIZE', '-sLEGACY_VM_SUPPORT']) + def test_emmake_emconfigure(self): def check(what, args, fail=True, expect=''): args = [what] + args diff --git a/tools/emscripten.py b/tools/emscripten.py index d33b4c97cffb2..843de819d0e9a 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -872,13 +872,7 @@ def create_sending(metadata, library_symbols): def can_use_await(): - # In MODULARIZE mode we can use `await` since the factory function itself - # is marked as `async` and the generated code all lives inside that factory - # function. - # However, because closure does not see this (it runs only on the inner code), - # it sees this as a top-level-await, which it does not yet support. - # FIXME(https://github.com/emscripten-core/emscripten/issues/23158) - return settings.MODULARIZE and not settings.USE_CLOSURE_COMPILER + return settings.MODULARIZE def make_export_wrappers(function_exports): @@ -988,7 +982,10 @@ def create_module(receiving, metadata, global_exports, library_symbols): if settings.WASM_ASYNC_COMPILATION: if can_use_await(): # In modularize mode the generated code is within a factory function. - module.append("var wasmExports = await createWasm();\n") + # This magic string gets replaced by `await createWasm`. It needed to allow + # closure and acorn to process the module without seeing this as a top-level + # await. + module.append("var wasmExports = EMSCRIPTEN$AWAIT(createWasm());\n") else: module.append("var wasmExports;\ncreateWasm();\n") else: diff --git a/tools/link.py b/tools/link.py index eecc827331c64..35245969890c6 100644 --- a/tools/link.py +++ b/tools/link.py @@ -2086,13 +2086,15 @@ def phase_source_transforms(options): # both main code and libraries. # See also: `preprocess` in parseTools.js. def fix_es6_import_statements(js_file): - if not settings.EXPORT_ES6: + if not settings.MODULARIZE: return src = read_file(js_file) write_file(js_file, src .replace('EMSCRIPTEN$IMPORT$META', 'import.meta') - .replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import')) + .replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import') + .replace('EMSCRIPTEN$AWAIT(createWasm())', 'await createWasm()') + .replace('EMSCRIPTEN$AWAIT(', 'await (')) save_intermediate('es6-module')