Skip to content

Commit 85cb0b7

Browse files
committed
Fix for vite bundling with pthreads enabled
Replaces emscripten-core#23607 Fixes: emscripten-core#22394
1 parent bb7fd25 commit 85cb0b7

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/lib/libpthread.js

+29-23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ globalThis.MAX_PTR = Number((2n ** 64n) - 1n);
2929
#else
3030
globalThis.MAX_PTR = (2 ** 32) - 1
3131
#endif
32+
// Use a helper function to avoid duplicating pthread worker options.
33+
// We cannot use normal JS variable since the vite bundler requires that worker
34+
// options be inline.
35+
// See https://github.com/emscripten-core/emscripten/issues/22394
36+
globalThis.pthreadWorkerOptions = () => {
37+
return `{
38+
#if EXPORT_ES6
39+
'type': 'module',
40+
#endif
41+
#if ENVIRONMENT_MAY_BE_NODE
42+
// This is the way that we signal to the node worker that it is hosting
43+
// a pthread.
44+
'workerData': 'em-pthread',
45+
#endif
46+
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
47+
// This is the way that we signal to the Web Worker that it is hosting
48+
// a pthread.
49+
#if ASSERTIONS
50+
'name': 'em-pthread-' + PThread.nextWorkerID,
51+
#else
52+
'name': 'em-pthread',
53+
#endif
54+
#endif
55+
}`
56+
}
3257
}}}
3358

3459
var LibraryPThread = {
@@ -394,25 +419,6 @@ var LibraryPThread = {
394419
// Creates a new web Worker and places it in the unused worker pool to wait for its use.
395420
allocateUnusedWorker() {
396421
var worker;
397-
var workerOptions = {
398-
#if EXPORT_ES6
399-
'type': 'module',
400-
#endif
401-
#if ENVIRONMENT_MAY_BE_NODE
402-
// This is the way that we signal to the node worker that it is hosting
403-
// a pthread.
404-
'workerData': 'em-pthread',
405-
#endif
406-
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
407-
// This is the way that we signal to the Web Worker that it is hosting
408-
// a pthread.
409-
#if ASSERTIONS
410-
'name': 'em-pthread-' + PThread.nextWorkerID,
411-
#else
412-
'name': 'em-pthread',
413-
#endif
414-
#endif
415-
};
416422
#if EXPORT_ES6 && USE_ES6_IMPORT_META
417423
// If we're using module output, use bundler-friendly pattern.
418424
#if PTHREADS_DEBUG
@@ -427,14 +433,14 @@ var LibraryPThread = {
427433
createScriptURL: (ignored) => new URL("{{{ TARGET_JS_NAME }}}", import.meta.url)
428434
}
429435
);
430-
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
436+
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions() }}});
431437
} else
432438
#endif
433439
// We need to generate the URL with import.meta.url as the base URL of the JS file
434440
// instead of just using new URL(import.meta.url) because bundler's only recognize
435441
// the first case in their bundling step. The latter ends up producing an invalid
436442
// URL to import from the server (e.g., for webpack the file:// path).
437-
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), workerOptions);
443+
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions() }}});
438444
#else
439445
var pthreadMainJs = _scriptName;
440446
#if expectToReceiveOnModule('mainScriptUrlOrBlob')
@@ -454,10 +460,10 @@ var LibraryPThread = {
454460
// Use Trusted Types compatible wrappers.
455461
if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) {
456462
var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => pthreadMainJs });
457-
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
463+
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions() }}});
458464
} else
459465
#endif
460-
worker = new Worker(pthreadMainJs, workerOptions);
466+
worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions() }}});
461467
#endif // EXPORT_ES6 && USE_ES6_IMPORT_META
462468
PThread.unusedWorkers.push(worker);
463469
},

test/test_browser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5534,10 +5534,11 @@ def test_webpack(self, es6):
55345534
shutil.copy('webpack/src/hello.wasm', 'webpack/dist/')
55355535
self.run_browser('webpack/dist/index.html', '/report_result?exit:0')
55365536

5537+
@also_with_threads
55375538
def test_vite(self):
55385539
shutil.copytree(test_file('vite'), 'vite')
55395540
with common.chdir('vite'):
5540-
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-o', 'hello.mjs'])
5541+
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', 'hello.mjs'])
55415542
self.run_process(shared.get_npm_cmd('vite') + ['build'])
55425543
self.run_browser('vite/dist/index.html', '/report_result?exit:0')
55435544

0 commit comments

Comments
 (0)