@@ -29,6 +29,13 @@ const MAX_PTR = Number((2n ** 64n) - 1n);
2929#else
3030const MAX_PTR = ( 2 ** 32 ) - 1
3131#endif
32+
33+ #if WASM_ESM_INTEGRATION
34+ const pthreadWorkerScript = TARGET_BASENAME + '.pthread.mjs' ;
35+ #else
36+ const pthreadWorkerScript = TARGET_JS_NAME ;
37+ #endif
38+
3239// Use a macro to avoid duplicating pthread worker options.
3340// We cannot use a normal JS variable since the vite bundler requires that worker
3441// options be inline.
@@ -282,6 +289,7 @@ var LibraryPThread = {
282289 message = `Pthread ${ ptrToString ( worker . pthread_ptr ) } sent an error!` ;
283290 }
284291#endif
292+ console . error ( e . stack ) ;
285293 err ( `${ message } ${ e . filename } :${ e . lineno } : ${ e . message } ` ) ;
286294 throw e ;
287295 } ;
@@ -295,7 +303,9 @@ var LibraryPThread = {
295303
296304#if ASSERTIONS
297305 assert ( wasmMemory instanceof WebAssembly . Memory , 'WebAssembly memory should have been loaded by now!' ) ;
306+ #if ! WASM_ESM_INTEGRATION
298307 assert ( wasmModule instanceof WebAssembly . Module , 'WebAssembly Module should have been loaded by now!' ) ;
308+ #endif
299309#endif
300310
301311 // When running on a pthread, none of the incoming parameters on the module
@@ -333,7 +343,9 @@ var LibraryPThread = {
333343#else // WASM2JS
334344 wasmMemory,
335345#endif // WASM2JS
346+ #if ! WASM_ESM_INTEGRATION
336347 wasmModule,
348+ #endif
337349#if LOAD_SOURCE_MAP
338350 wasmSourceMap,
339351#endif
@@ -391,25 +403,25 @@ var LibraryPThread = {
391403#if TRUSTED_TYPES
392404 // Use Trusted Types compatible wrappers.
393405 if ( typeof trustedTypes != 'undefined' && trustedTypes . createPolicy ) {
394- var p = trustedTypes . createPolicy ( 'emscripten#workerPolicy1' , { createScriptURL : ( ignored ) => import . meta. url } ) ;
406+ var p = trustedTypes . createPolicy ( 'emscripten#workerPolicy1' , { createScriptURL : ( ignored ) => new URL ( '{{{ pthreadWorkerScript }}}' , import . meta. url ) ) ;
395407 worker = new Worker ( p . createScriptURL ( 'ignored' ) , { { { pthreadWorkerOptions } } } ) ;
396408 } else
397409#endif
398410#if expectToReceiveOnModule ( 'mainScriptUrlOrBlob' )
399- if ( Module [ 'mainScriptUrlOrBlob' ] ) {
400- var pthreadMainJs = Module [ 'mainScriptUrlOrBlob' ] ;
401- if ( typeof pthreadMainJs != 'string' ) {
402- pthreadMainJs = URL . createObjectURL ( pthreadMainJs ) ;
403- }
404- worker = new Worker ( pthreadMainJs , { { { pthreadWorkerOptions } } } ) ;
405- } else
411+ if ( Module [ 'mainScriptUrlOrBlob' ] ) {
412+ var pthreadMainJs = Module [ 'mainScriptUrlOrBlob' ] ;
413+ if ( typeof pthreadMainJs != 'string' ) {
414+ pthreadMainJs = URL . createObjectURL ( pthreadMainJs ) ;
415+ }
416+ worker = new Worker ( pthreadMainJs , { { { pthreadWorkerOptions } } } ) ;
417+ } else
406418#endif
407419 // We need to generate the URL with import.meta.url as the base URL of the JS file
408420 // instead of just using new URL(import.meta.url) because bundler's only recognize
409421 // the first case in their bundling step. The latter ends up producing an invalid
410422 // URL to import from the server (e.g., for webpack the file:// path).
411423 // See https://github.com/webpack/webpack/issues/12638
412- worker = new Worker ( new URL ( '{{{ TARGET_JS_NAME }}}' , import . meta. url ) , { { { pthreadWorkerOptions } } } ) ;
424+ worker = new Worker ( new URL ( '{{{ pthreadWorkerScript }}}' , import . meta. url ) , { { { pthreadWorkerOptions } } } ) ;
413425#else // EXPORT_ES6
414426 var pthreadMainJs = _scriptName ;
415427#if expectToReceiveOnModule ( 'mainScriptUrlOrBlob' )
@@ -601,10 +613,10 @@ var LibraryPThread = {
601613
602614 worker . pthread_ptr = threadParams . pthread_ptr ;
603615 var msg = {
604- cmd : 'run' ,
605- start_routine : threadParams . startRoutine ,
606- arg : threadParams . arg ,
607- pthread_ptr : threadParams . pthread_ptr ,
616+ cmd : 'run' ,
617+ start_routine : threadParams . startRoutine ,
618+ arg : threadParams . arg ,
619+ pthread_ptr : threadParams . pthread_ptr ,
608620 } ;
609621#if OFFSCREENCANVAS_SUPPORT
610622 // Note that we do not need to quote these names because they are only used
0 commit comments