Skip to content

Commit 89aca9c

Browse files
authored
Fix for vite bundling with pthreads enabled (#23707)
Replaces #23607 Fixes: #22394
1 parent f66c67c commit 89aca9c

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/lib/libpthread.js

+28-23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ globalThis.MAX_PTR = Number((2n ** 64n) - 1n);
2929
#else
3030
globalThis.MAX_PTR = (2 ** 32) - 1
3131
#endif
32+
// Use a macro to avoid duplicating pthread worker options.
33+
// We cannot use a 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+
#if EXPORT_ES6
38+
'type': 'module',
39+
#endif
40+
#if ENVIRONMENT_MAY_BE_NODE
41+
// This is the way that we signal to the node worker that it is hosting
42+
// a pthread.
43+
'workerData': 'em-pthread',
44+
#endif
45+
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
46+
// This is the way that we signal to the Web Worker that it is hosting
47+
// a pthread.
48+
#if ASSERTIONS
49+
'name': 'em-pthread-' + PThread.nextWorkerID,
50+
#else
51+
'name': 'em-pthread',
52+
#endif
53+
#endif
54+
}`;
55+
null
3256
}}}
3357

3458
var LibraryPThread = {
@@ -394,25 +418,6 @@ var LibraryPThread = {
394418
// Creates a new web Worker and places it in the unused worker pool to wait for its use.
395419
allocateUnusedWorker() {
396420
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-
};
416421
#if EXPORT_ES6 && USE_ES6_IMPORT_META
417422
// If we're using module output, use bundler-friendly pattern.
418423
#if PTHREADS_DEBUG
@@ -427,14 +432,14 @@ var LibraryPThread = {
427432
createScriptURL: (ignored) => new URL("{{{ TARGET_JS_NAME }}}", import.meta.url)
428433
}
429434
);
430-
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
435+
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}});
431436
} else
432437
#endif
433438
// We need to generate the URL with import.meta.url as the base URL of the JS file
434439
// instead of just using new URL(import.meta.url) because bundler's only recognize
435440
// the first case in their bundling step. The latter ends up producing an invalid
436441
// 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);
442+
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}});
438443
#else
439444
var pthreadMainJs = _scriptName;
440445
#if expectToReceiveOnModule('mainScriptUrlOrBlob')
@@ -454,10 +459,10 @@ var LibraryPThread = {
454459
// Use Trusted Types compatible wrappers.
455460
if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) {
456461
var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => pthreadMainJs });
457-
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
462+
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}});
458463
} else
459464
#endif
460-
worker = new Worker(pthreadMainJs, workerOptions);
465+
worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions }}});
461466
#endif // EXPORT_ES6 && USE_ES6_IMPORT_META
462467
PThread.unusedWorkers.push(worker);
463468
},

test/test_browser.py

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

5536+
@also_with_threads
55365537
def test_vite(self):
55375538
copytree(test_file('vite'), '.')
5538-
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-o', 'hello.mjs'])
5539+
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', 'hello.mjs'])
55395540
self.run_process(shared.get_npm_cmd('vite') + ['build'])
55405541
self.run_browser('dist/index.html', '/report_result?exit:0')
55415542

test/test_other.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def test_emcc_output_worker_mjs(self, args):
380380
test_file('hello_world.c')] + args)
381381
src = read_file('subdir/hello_world.mjs')
382382
self.assertContained("new URL('hello_world.wasm', import.meta.url)", src)
383-
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src)
383+
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), {", src)
384384
self.assertContained('export default Module;', src)
385385
self.assertContained('hello, world!', self.run_js('subdir/hello_world.mjs'))
386386

@@ -391,7 +391,7 @@ def test_emcc_output_worker_mjs_single_file(self):
391391
test_file('hello_world.c'), '-sSINGLE_FILE'])
392392
src = read_file('hello_world.mjs')
393393
self.assertNotContained("new URL('data:", src)
394-
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src)
394+
self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), {", src)
395395
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))
396396

397397
def test_emcc_output_mjs_closure(self):

0 commit comments

Comments
 (0)