@@ -29,6 +29,30 @@ globalThis.MAX_PTR = Number((2n ** 64n) - 1n);
2929#else
3030globalThis . 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+ #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
3458var 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 } ,
0 commit comments