@@ -29,6 +29,31 @@ globalThis.MAX_PTR = Number((2n ** 64n) - 1n);
29
29
#else
30
30
globalThis . MAX_PTR = ( 2 ** 32 ) - 1
31
31
#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
+ }
32
57
} } }
33
58
34
59
var LibraryPThread = {
@@ -394,25 +419,6 @@ var LibraryPThread = {
394
419
// Creates a new web Worker and places it in the unused worker pool to wait for its use.
395
420
allocateUnusedWorker ( ) {
396
421
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
- } ;
416
422
#if EXPORT_ES6 && USE_ES6_IMPORT_META
417
423
// If we're using module output, use bundler-friendly pattern.
418
424
#if PTHREADS_DEBUG
@@ -427,14 +433,14 @@ var LibraryPThread = {
427
433
createScriptURL : ( ignored ) => new URL ( "{{{ TARGET_JS_NAME }}}" , import . meta. url )
428
434
}
429
435
) ;
430
- worker = new Worker ( p . createScriptURL ( 'ignored' ) , workerOptions ) ;
436
+ worker = new Worker ( p . createScriptURL ( 'ignored' ) , { { { pthreadWorkerOptions ( ) } } } ) ;
431
437
} else
432
438
#endif
433
439
// We need to generate the URL with import.meta.url as the base URL of the JS file
434
440
// instead of just using new URL(import.meta.url) because bundler's only recognize
435
441
// the first case in their bundling step. The latter ends up producing an invalid
436
442
// 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 ( ) } } } ) ;
438
444
#else
439
445
var pthreadMainJs = _scriptName ;
440
446
#if expectToReceiveOnModule ( 'mainScriptUrlOrBlob' )
@@ -454,10 +460,10 @@ var LibraryPThread = {
454
460
// Use Trusted Types compatible wrappers.
455
461
if ( typeof trustedTypes != 'undefined' && trustedTypes . createPolicy ) {
456
462
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 ( ) } } } ) ;
458
464
} else
459
465
#endif
460
- worker = new Worker ( pthreadMainJs , workerOptions ) ;
466
+ worker = new Worker ( pthreadMainJs , { { { pthreadWorkerOptions ( ) } } } ) ;
461
467
#endif // EXPORT_ES6 && USE_ES6_IMPORT_META
462
468
PThread . unusedWorkers . push ( worker ) ;
463
469
} ,
0 commit comments