Skip to content

Enable createWasm to use async/await with closure and babel #23704

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

Merged
merged 1 commit into from
Mar 24, 2025
Merged
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
1 change: 1 addition & 0 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1437
1429
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2982
2967
3 changes: 3 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down