Skip to content

Commit 547dcc7

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 02b5a05 commit 547dcc7

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
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+
1431
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2995
1+
2980

test/test_other.py

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

7101+
def test_modularize_legacy(self):
7102+
self.do_runf('hello_world.c', emcc_args=['-sMODULARIZE', '-sLEGACY_VM_SUPPORT'])
7103+
71017104
def test_emmake_emconfigure(self):
71027105
def check(what, args, fail=True, expect=''):
71037106
args = [what] + args

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -2065,13 +2065,15 @@ def phase_source_transforms(options):
20652065
# both main code and libraries.
20662066
# See also: `preprocess` in parseTools.js.
20672067
def fix_es6_import_statements(js_file):
2068-
if not settings.EXPORT_ES6 or not settings.USE_ES6_IMPORT_META:
2068+
if not settings.MODULARIZE:
20692069
return
20702070

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 createWasm()')
2076+
.replace('EMSCRIPTEN$AWAIT(', 'await ('))
20752077
save_intermediate('es6-module')
20762078

20772079

0 commit comments

Comments
 (0)