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

[Old] Fix pthread worker options usage with Vite bundler #22834

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
27 changes: 4 additions & 23 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,25 +393,6 @@ var LibraryPThread = {
// Creates a new web Worker and places it in the unused worker pool to wait for its use.
allocateUnusedWorker() {
var worker;
var workerOptions = {
#if EXPORT_ES6
'type': 'module',
#endif
#if ENVIRONMENT_MAY_BE_NODE
// This is the way that we signal to the node worker that it is hosting
// a pthread.
'workerData': 'em-pthread',
#endif
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
// This is the way that we signal to the Web Worker that it is hosting
// a pthread.
#if ASSERTIONS
'name': 'em-pthread-' + PThread.nextWorkerID,
#else
'name': 'em-pthread',
#endif
#endif
};
#if EXPORT_ES6 && USE_ES6_IMPORT_META
// If we're using module output, use bundler-friendly pattern.
#if PTHREADS_DEBUG
Expand All @@ -426,14 +407,14 @@ var LibraryPThread = {
createScriptURL: (ignored) => new URL("{{{ TARGET_JS_NAME }}}", import.meta.url)
}
);
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
worker = new Worker(p.createScriptURL('ignored'), {{{ WORKER_OPTIONS }}});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I simpler way to do this would be to define a new macro here at the top of this file. e.g.

{{{
globalThis.WORKER_OPTIONS = {
   ...
};
null;
}}}

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much! I wasn't too sure how the templating works in these files 😅 . I'll update the PR with this approach when I get a chance.

} else
#endif
// We need to generate the URL with import.meta.url as the base URL of the JS file
// instead of just using new URL(import.meta.url) because bundler's only recognize
// the first case in their bundling step. The latter ends up producing an invalid
// URL to import from the server (e.g., for webpack the file:// path).
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), workerOptions);
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ WORKER_OPTIONS }}});
#else
var pthreadMainJs = _scriptName;
#if expectToReceiveOnModule('mainScriptUrlOrBlob')
Expand All @@ -453,10 +434,10 @@ var LibraryPThread = {
// Use Trusted Types compatible wrappers.
if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) {
var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => pthreadMainJs });
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
worker = new Worker(p.createScriptURL('ignored'), {{{ WORKER_OPTIONS }}});
} else
#endif
worker = new Worker(pthreadMainJs, workerOptions);
worker = new Worker(pthreadMainJs, {{{ WORKER_OPTIONS }}});
#endif // EXPORT_ES6 && USE_ES6_IMPORT_META
PThread.unusedWorkers.push(worker);
},
Expand Down
4 changes: 2 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def test_emcc_output_worker_mjs(self, args):
test_file('hello_world.c')] + args)
src = read_file('subdir/hello_world.mjs')
self.assertContained("new URL('hello_world.wasm', import.meta.url)", src)
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src)
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), { workerData: 'em-pthread' })", src)
self.assertContained('export default Module;', src)
self.assertContained('hello, world!', self.run_js('subdir/hello_world.mjs'))

Expand All @@ -381,7 +381,7 @@ def test_emcc_output_worker_mjs_single_file(self):
test_file('hello_world.c'), '-sSINGLE_FILE'])
src = read_file('hello_world.mjs')
self.assertNotContained("new URL('data:", src)
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src)
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), { workerData: 'em-pthread' })", src)
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))

def test_emcc_output_mjs_closure(self):
Expand Down
26 changes: 26 additions & 0 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,9 @@ def phase_final_emitting(options, state, target, wasm_target):

target_dir = os.path.dirname(os.path.abspath(target))

if settings.PTHREADS:
replace_worker_options_placeholders(final_js)
jamsinclair marked this conversation as resolved.
Show resolved Hide resolved

# Deploy the Wasm Worker bootstrap file as an output file (*.ww.js)
if settings.WASM_WORKERS == 1:
create_worker_file('src/wasm_worker.js', target_dir, settings.WASM_WORKER_FILE, options)
Expand Down Expand Up @@ -2685,6 +2688,29 @@ def worker_js_script(proxy_worker_filename):
return web_gl_client_src + '\n' + proxy_client_src


def replace_worker_options_placeholders():
global final_js
js = read_file(final_js)
options = []

if settings.EXPORT_ES6:
options.append("type: 'module'")
if settings.ENVIRONMENT_MAY_BE_NODE:
options.append("workerData: 'em-pthread'")
if settings.ENVIRONMENT_MAY_BE_WEB or settings.ENVIRONMENT_MAY_BE_WORKER:
if settings.ASSERTIONS:
options.append("name: 'em-pthread-' + PThread.nextWorkerID")
else:
options.append("name: 'em-pthread'")

worker_options_str = "{ " + ", ".join(options) + " }"

# Replace placeholders with a worker options object literal
# This is necessary to avoid issues with JS bundlers, like Vite.
js = do_replace(js, '<<< WORKER_OPTIONS >>>', worker_options_str)
write_file(final_js, js)


def find_library(lib, lib_dirs):
for lib_dir in lib_dirs:
path = os.path.join(lib_dir, lib)
Expand Down