Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ function mangleUnsupportedSyntax(text) {
// placeholders during preprocess phase, and back after all the other ops.
// 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');
}
text = text.replaceAll('await createWasm()', 'EMSCRIPTEN$AWAIT(createWasm())');
text = text.replaceAll('await run()', 'EMSCRIPTEN$AWAIT(run())');
text = text.replaceAll('await instantiatePromise', 'EMSCRIPTEN$AWAIT(instantiatePromise)');
Expand Down Expand Up @@ -102,6 +99,15 @@ export function preprocess(filename) {
let text = readFile(filename);
// Remove windows line endings, if any
text = text.replace(/\r\n/g, '\n');
if (EXPORT_ES6 && USE_CLOSURE_COMPILER) {
// Replace `await import` with a placeholder during preprocessing, and restore
// it after other transforms. This must be done in `preprocess` (rather than
// `mangleUnsupportedSyntax`) to avoid accidentally rewriting `await import`
// inside `nodePthreadDetection()` and `nodeWWDetection()` macro expansions.

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.

I think we need even more explanation here. Why we not what to rewrite those, and how do moving the re-writing prevent that? Are they not run through preprocess?

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.

I found a better fix for this, PTAL (the PR description has been updated accordingly).

// See also: `fix_js_mangling` in link.py.
// FIXME: Remove after https://github.com/google/closure-compiler/issues/3835 is fixed.
text = text.replaceAll('await import', 'EMSCRIPTEN$AWAIT$IMPORT');
}

const IGNORE = 0;
const SHOW = 1;
Expand Down
13 changes: 13 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,19 @@ 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'])
src = read_file('hello_world.mjs')
self.assertContained('new URL("hello_world.wasm",import.meta.url)', src)
self.assertContained('(await import("node:worker_threads")).workerData==="em-pthread"', src)

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.

Are these assertions needed? i.e. won't the program fail if we the get the mangling wrong?

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.

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
2 changes: 1 addition & 1 deletion tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,7 @@ def phase_source_transforms(options):

# Unmangle previously mangled `await import` and `await` references in
# both main code and libraries.
# See also: `mangleUnsupportedSyntax` in parseTools.mjs.
# See also: `mangleUnsupportedSyntax` and `preprocess` in parseTools.mjs.
def fix_js_mangling(js_file):
# Mangling only takes place under closure in MODULARIZE mode.
if not settings.MODULARIZE or not settings.USE_CLOSURE_COMPILER:
Expand Down
Loading