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