Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pthreads] Fix worker.js in ES6 module environments #21041

Merged
merged 7 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works.

3.1.52 (in development)
-----------------------
- Building with `pthreads+EXPORT_ES6` will now emit the worker file as
`NAME.worker.mjs` rather than `.js`. This is a necessary breaking change to
resolve other `pthreads+EXPORT_ES6` issues in Node.js (because Node.js is
affected by the suffix in some cases). (#21041)
- Include paths added by ports (e.g. `-sUSE_SDL=2`) now use `-isystem` rather
then `-I`. This means that files in user-specified include directories will
now take precedence over port includes. (#21014)
Expand Down
7 changes: 6 additions & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ if (ENVIRONMENT_IS_NODE) {
require,
Module,
location: {
// __filename is undefined in ES6 modules
// __filename is undefined in ES6 modules, and import.meta.url only in ES6
// modules.
#if EXPORT_ES6
href: typeof __filename !== 'undefined' ? __filename : import.meta.url
#else
href: typeof __filename
RReverser marked this conversation as resolved.
Show resolved Hide resolved
#endif
},
Worker: nodeWorkerThreads.Worker,
importScripts: (f) => vm.runInThisContext(fs.readFileSync(f, 'utf8'), {filename: f}),
Expand Down
9 changes: 7 additions & 2 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ def do_split_module(wasm_file, options):
building.run_binaryen_command('wasm-split', wasm_file + '.orig', outfile=wasm_file, args=args)


def get_worker_js_suffix():
return '.worker.mjs' if settings.EXPORT_ES6 else '.worker.js'


def setup_pthreads(target):
if settings.RELOCATABLE:
# phtreads + dyanmic linking has certain limitations
Expand Down Expand Up @@ -569,7 +573,7 @@ def setup_pthreads(target):
building.user_requested_exports.update(worker_imports)

# set location of worker.js
settings.PTHREAD_WORKER_FILE = unsuffixed_basename(target) + '.worker.js'
settings.PTHREAD_WORKER_FILE = unsuffixed_basename(target) + get_worker_js_suffix()

if settings.MINIMAL_RUNTIME:
building.user_requested_exports.add('exit')
Expand Down Expand Up @@ -2011,6 +2015,7 @@ def fix_es6_import_statements(js_file):
.replace('EMSCRIPTEN$IMPORT$META', 'import.meta')
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import'))


def create_worker_file(input_file, target_dir, output_file):
output_file = os.path.join(target_dir, output_file)
input_file = utils.path_from_root(input_file)
Expand Down Expand Up @@ -2605,7 +2610,7 @@ def generate_worker_js(target, js_target, target_basename):
proxy_worker_filename = get_subresource_location(js_target)
else:
# compiler output goes in .worker.js file
move_file(js_target, shared.replace_suffix(js_target, '.worker.js'))
move_file(js_target, shared.replace_suffix(js_target, get_worker_js_suffix()))
worker_target_basename = target_basename + '.worker'
proxy_worker_filename = (settings.PROXY_TO_WORKER_FILENAME or worker_target_basename) + '.js'

Expand Down