Skip to content

Commit 0c943c6

Browse files
committed
Enable createWasm to use async/await with closure
I really which we had a better solution here, but this makes the use of async/await independent of closure usage. This is a workaround for #23158, but it also happens to fix #23687 which stems from the same issue. Fixes: #23687
1 parent a5af114 commit 0c943c6

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

src/closure-externs/closure-externs.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// Special placeholder for `import.meta` and `await import`.
1515
var EMSCRIPTEN$IMPORT$META;
1616
var EMSCRIPTEN$AWAIT$IMPORT;
17+
var EMSCRIPTEN$AWAIT;
1718

1819
// Don't minify createRequire
1920
var createRequire;

test/test_other.py

+3
Original file line numberDiff line numberDiff line change
@@ -7170,6 +7170,9 @@ class Descriptor {
71707170
self.run_process([EMXX, 'src.cpp', '-O2', '-sEXPORT_ALL'])
71717171
self.assertExists('a.out.js')
71727172

7173+
def test_modularize_legacy(self):
7174+
self.do_runf('hello_world.c', emcc_args=['-sMODULARIZE', '-sLEGACY_VM_SUPPORT'])
7175+
71737176
def test_emmake_emconfigure(self):
71747177
def check(what, args, fail=True, expect=''):
71757178
args = [what] + args

tools/emscripten.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,7 @@ def create_sending(metadata, library_symbols):
872872

873873

874874
def can_use_await():
875-
# In MODULARIZE mode we can use `await` since the factory function itself
876-
# is marked as `async` and the generated code all lives inside that factory
877-
# function.
878-
# However, because closure does not see this (it runs only on the inner code),
879-
# it sees this as a top-level-await, which it does not yet support.
880-
# FIXME(https://github.com/emscripten-core/emscripten/issues/23158)
881-
return settings.MODULARIZE and not settings.USE_CLOSURE_COMPILER
875+
return settings.MODULARIZE
882876

883877

884878
def make_export_wrappers(function_exports):
@@ -988,7 +982,10 @@ def create_module(receiving, metadata, global_exports, library_symbols):
988982
if settings.WASM_ASYNC_COMPILATION:
989983
if can_use_await():
990984
# In modularize mode the generated code is within a factory function.
991-
module.append("var wasmExports = await createWasm();\n")
985+
# This magic string gets replaced by `await createWasm`. It needed to allow
986+
# closure and acorn to process the module without seeing this as a top-level
987+
# await.
988+
module.append("var wasmExports = EMSCRIPTEN$AWAIT(createWasm());\n")
992989
else:
993990
module.append("var wasmExports;\ncreateWasm();\n")
994991
else:

tools/link.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2089,13 +2089,15 @@ def phase_source_transforms(options):
20892089
# both main code and libraries.
20902090
# See also: `preprocess` in parseTools.js.
20912091
def fix_es6_import_statements(js_file):
2092-
if not settings.EXPORT_ES6:
2092+
if not settings.MODULARIZE:
20932093
return
20942094

20952095
src = read_file(js_file)
20962096
write_file(js_file, src
20972097
.replace('EMSCRIPTEN$IMPORT$META', 'import.meta')
2098-
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import'))
2098+
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import')
2099+
.replace('EMSCRIPTEN$AWAIT(createWasm())', 'await createWasm()')
2100+
.replace('EMSCRIPTEN$AWAIT(', 'await ('))
20992101
save_intermediate('es6-module')
21002102

21012103

0 commit comments

Comments
 (0)