Skip to content
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
4 changes: 0 additions & 4 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
* The closure_compiler() method in tools/shared.py refers to this file when calling closure.
*/

// Special placeholder for `await import` and `await`.
var EMSCRIPTEN$AWAIT$IMPORT;
var EMSCRIPTEN$AWAIT;

// Don't minify createRequire
var createRequire;

Expand Down
3 changes: 3 additions & 0 deletions src/closure-externs/modularize-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ var moduleArg;
* @suppress {duplicate, undefinedVars}
*/
var Module;

// Special placeholder for `await`.
var EMSCRIPTEN$AWAIT;
5 changes: 4 additions & 1 deletion src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function mangleUnsupportedSyntax(text) {
// See also: `fix_js_mangling` in link.py.
// FIXME: Remove after https://github.com/google/closure-compiler/issues/3835 is fixed.
if (EXPORT_ES6) {
text = text.replaceAll('await import', 'EMSCRIPTEN$AWAIT$IMPORT');
// Use a low-precedence `||` pattern so Closure doesn't strip parentheses.
// High-precedence placeholders trick Closure into optimizing `(PLACEHOLDER).y`
// into `PLACEHOLDER.y`, breaking execution order once swapped back to `await`.
text = text.replaceAll('await import', 'EMSCRIPTEN$AWAIT||import');
}
text = text.replaceAll('await createWasm()', 'EMSCRIPTEN$AWAIT(createWasm())');
text = text.replaceAll('await run()', 'EMSCRIPTEN$AWAIT(run())');
Expand Down
10 changes: 10 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ def test_esm_worker_single_file(self):
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), {", src)
self.assertContained('Hello, world!', self.run_js('hello_world.mjs'))

def test_esm_worker_closure(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_esm_pthread_closure?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an extend of test_esm_worker, just like test_esm_worker_single_file.

self.run_process([EMCC, '-o', 'hello_world.mjs',
'-sEXIT_RUNTIME', '-sPROXY_TO_PTHREAD', '-pthread', '-O2',
test_file('hello_world.c'), '--closure=1'])
create_file('run.mjs', '''
import Module from './hello_world.mjs';
await Module();
''')
self.assertContained('Hello, world!', self.run_js('run.mjs'))

def test_esm_closure(self):
self.run_process([EMCC, '-o', 'hello_world.mjs',
'--extern-post-js', test_file('modularize_post_js.js'),
Expand Down
13 changes: 7 additions & 6 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,8 +2129,8 @@ def phase_source_transforms(options):
save_intermediate('transformed')


# Unmangle previously mangled `await import` and `await` references in
# both main code and libraries.
# Unmangle previously mangled `await` references in both
# main code and libraries.
# See also: `mangleUnsupportedSyntax` in parseTools.mjs.
def fix_js_mangling(js_file):
# Mangling only takes place under closure in MODULARIZE mode.
Expand All @@ -2140,13 +2140,14 @@ def fix_js_mangling(js_file):
src = read_file(js_file)

if settings.EXPORT_ES6:
# Also remove the line containing `export{};`, which is inserted by
# Safely matches 'EMSCRIPTEN$AWAIT' with any amount of surrounding
# whitespace around '||'
src = re.sub(r'EMSCRIPTEN\$AWAIT\s*\|\|\s*', 'await ', src)
# Remove the line containing `export{};`, which is inserted by
# Closure to mark the file as an ES6 module.
# https://github.com/google/closure-compiler/issues/4084#issuecomment-1505056519
# https://github.com/google/closure-compiler/blob/v20260401/src/com/google/javascript/jscomp/ConvertChunksToESModules.java#L111-L113
src = src \
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import') \
.replace('export{};\n', '')
src = src.replace('export{};\n', '')

src = src.replace('EMSCRIPTEN$AWAIT(', 'await (')

Expand Down