Skip to content

Commit 3d8d5d1

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 3d8d5d1

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
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

+8-11
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
444444
pre = None
445445

446446
receiving = create_receiving(function_exports)
447+
receiving += create_global_exports(global_exports)
447448

448-
module = create_module(receiving, metadata, global_exports, forwarded_json['librarySymbols'])
449+
module = create_module(receiving, metadata, forwarded_json['librarySymbols'])
449450

450451
metadata.library_definitions = forwarded_json['libraryDefinitions']
451452

@@ -877,13 +878,7 @@ def create_sending(metadata, library_symbols):
877878

878879

879880
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
881+
return settings.MODULARIZE
887882

888883

889884
def make_export_wrappers(function_exports):
@@ -973,8 +968,7 @@ def create_receiving(function_exports):
973968
return '\n'.join(receiving) + '\n'
974969

975970

976-
def create_module(receiving, metadata, global_exports, library_symbols):
977-
receiving += create_global_exports(global_exports)
971+
def create_module(receiving, metadata, library_symbols):
978972
module = []
979973

980974
sending = create_sending(metadata, library_symbols)
@@ -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)