Skip to content

Commit e3f079f

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. Fixes: #23158
1 parent c50c33d commit e3f079f

File tree

5 files changed

+10
-11
lines changed

5 files changed

+10
-11
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;
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1436
1+
1445
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2995
1+
2990

tools/emscripten.py

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

878878

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

888882

889883
def make_export_wrappers(function_exports):
@@ -993,7 +987,10 @@ def create_module(receiving, metadata, global_exports, library_symbols):
993987
if settings.WASM_ASYNC_COMPILATION:
994988
if can_use_await():
995989
# In modularize mode the generated code is within a factory function.
996-
module.append("var wasmExports = await createWasm();\n")
990+
# This magic string gets replaced by `await createWasm`. It needed to allow
991+
# closure and acorn to process the module without seeing this as a top-level
992+
# await.
993+
module.append("var wasmExports = EMSCRIPTEN$AWAIT(createWasm());\n")
997994
else:
998995
module.append("var wasmExports;\ncreateWasm();\n")
999996
else:

tools/link.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,8 @@ def fix_es6_import_statements(js_file):
20712071
src = read_file(js_file)
20722072
write_file(js_file, src
20732073
.replace('EMSCRIPTEN$IMPORT$META', 'import.meta')
2074-
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import'))
2074+
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import')
2075+
.replace('EMSCRIPTEN$AWAIT(createWasm())', 'await creasteWasm()'))
20752076
save_intermediate('es6-module')
20762077

20772078

0 commit comments

Comments
 (0)