From 706841b4ac6b6df4ef32f2292e5bf467fff3751a Mon Sep 17 00:00:00 2001 From: Jonathan Gamble Date: Thu, 6 Mar 2025 22:32:48 -0600 Subject: [PATCH 1/6] respect mainScriptUrlOrBlob for esm targets --- src/lib/libpthread.js | 50 +++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index 9baac65e5f3f8..c14eca75609c8 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -413,49 +413,39 @@ var LibraryPThread = { // Creates a new web Worker and places it in the unused worker pool to wait for its use. allocateUnusedWorker() { - var worker; -#if EXPORT_ES6 - // If we're using module output, use bundler-friendly pattern. -#if PTHREADS_DEBUG - dbg(`Allocating a new web worker from ${import.meta.url}`); -#endif -#if TRUSTED_TYPES - // Use Trusted Types compatible wrappers. - if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) { - var p = trustedTypes.createPolicy('emscripten#workerPolicy1', { createScriptURL: (ignored) => import.meta.url }); - worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}}); - } else + var workerUrl, workerPolicyUrl; +#if expectToReceiveOnModule('mainScriptUrlOrBlob') + // We can't use makeModuleReceiveWithVar here since we want to also + // call URL.createObjectURL on the mainScriptUrlOrBlob. + workerPolicyUrl = workerUrl = Module['mainScriptUrlOrBlob']; + if (workerUrl && typeof workerUrl != 'string') { + workerPolicyUrl = workerUrl = URL.createObjectURL(workerUrl); + } #endif + if (!workerUrl) { +#if EXPORT_ES6 // We need to generate the URL with import.meta.url as the base URL of the JS file // instead of just using new URL(import.meta.url) because bundler's only recognize // the first case in their bundling step. The latter ends up producing an invalid // URL to import from the server (e.g., for webpack the file:// path). // See https://github.com/webpack/webpack/issues/12638 - worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}}); -#else // EXPORT_ES6 - var pthreadMainJs = _scriptName; -#if expectToReceiveOnModule('mainScriptUrlOrBlob') - // We can't use makeModuleReceiveWithVar here since we want to also - // call URL.createObjectURL on the mainScriptUrlOrBlob. - if (Module['mainScriptUrlOrBlob']) { - pthreadMainJs = Module['mainScriptUrlOrBlob']; - if (typeof pthreadMainJs != 'string') { - pthreadMainJs = URL.createObjectURL(pthreadMainJs); - } - } + workerUrl = new URL("{{{ TARGET_JS_NAME }}}", import.meta.url); + workerPolicyUrl = import.meta.url +#else + workerUrl = workerPolicyUrl = _scriptName; #endif + } #if PTHREADS_DEBUG - dbg(`Allocating a new web worker from ${pthreadMainJs}`); + dbg(`Allocating a new web worker from ${workerUrl}`); #endif #if TRUSTED_TYPES // Use Trusted Types compatible wrappers. if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) { - var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => pthreadMainJs }); - worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}}); - } else + var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => workerPolicyUrl }); + workerUrl = p.createScriptURL('ignored'); + } #endif - worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions }}}); -#endif // EXPORT_ES6 + var worker = new Worker(workerUrl, {{{ pthreadWorkerOptions }}}); #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; #endif From 62258d3607df1c1442d9b7f56713855bf4664247 Mon Sep 17 00:00:00 2001 From: Jonathan Gamble <101470903+schlawg@users.noreply.github.com> Date: Fri, 7 Mar 2025 12:42:35 -0600 Subject: [PATCH 2/6] make worker construction webpack-friendly --- src/lib/libpthread.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index c14eca75609c8..b60af68f7834d 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -436,7 +436,7 @@ var LibraryPThread = { #endif } #if PTHREADS_DEBUG - dbg(`Allocating a new web worker from ${workerUrl}`); + dbg(`Allocating a new web worker from ${workerUrl ?? workerPolicyUrl}`); #endif #if TRUSTED_TYPES // Use Trusted Types compatible wrappers. @@ -445,7 +445,10 @@ var LibraryPThread = { workerUrl = p.createScriptURL('ignored'); } #endif - var worker = new Worker(workerUrl, {{{ pthreadWorkerOptions }}}); + var options = {{{ pthreadWorkerOptions }}}; + var worker = workerUrl + ? new Worker(workerUrl, options) + : new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), options); #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; #endif From bf926a8b8ab94e2e4473877b8df5e25960a77609 Mon Sep 17 00:00:00 2001 From: Jonathan Gamble Date: Fri, 7 Mar 2025 13:05:48 -0600 Subject: [PATCH 3/6] workerUrl must be undefined for webpack constructor --- src/lib/libpthread.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index b60af68f7834d..3ccdb008af418 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -424,12 +424,6 @@ var LibraryPThread = { #endif if (!workerUrl) { #if EXPORT_ES6 - // We need to generate the URL with import.meta.url as the base URL of the JS file - // instead of just using new URL(import.meta.url) because bundler's only recognize - // the first case in their bundling step. The latter ends up producing an invalid - // URL to import from the server (e.g., for webpack the file:// path). - // See https://github.com/webpack/webpack/issues/12638 - workerUrl = new URL("{{{ TARGET_JS_NAME }}}", import.meta.url); workerPolicyUrl = import.meta.url #else workerUrl = workerPolicyUrl = _scriptName; @@ -448,6 +442,11 @@ var LibraryPThread = { var options = {{{ pthreadWorkerOptions }}}; var worker = workerUrl ? new Worker(workerUrl, options) + // We need to generate the URL with import.meta.url as the base URL of the JS file + // instead of just using new URL(import.meta.url) because bundler's only recognize + // the first case in their bundling step. The latter ends up producing an invalid + // URL to import from the server (e.g., for webpack the file:// path). + // See https://github.com/webpack/webpack/issues/12638 : new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), options); #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; From 4849ae01ead6f00dbfd48ce095f9f3122dd1c4e6 Mon Sep 17 00:00:00 2001 From: Jonathan Gamble Date: Fri, 7 Mar 2025 13:51:43 -0600 Subject: [PATCH 4/6] dont break node builds --- src/lib/libpthread.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index 3ccdb008af418..048f3807e680c 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -440,6 +440,7 @@ var LibraryPThread = { } #endif var options = {{{ pthreadWorkerOptions }}}; +#if EXPORT_ES6 var worker = workerUrl ? new Worker(workerUrl, options) // We need to generate the URL with import.meta.url as the base URL of the JS file @@ -448,6 +449,9 @@ var LibraryPThread = { // URL to import from the server (e.g., for webpack the file:// path). // See https://github.com/webpack/webpack/issues/12638 : new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), options); +#else + var worker = new Worker(workerUrl, options); +#endif #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; #endif From 2e083e27ff62b9d65948d2e96b7af431dd94bd0f Mon Sep 17 00:00:00 2001 From: Jonathan Gamble Date: Fri, 7 Mar 2025 21:30:07 -0600 Subject: [PATCH 5/6] streamline allocateUnusedWorker pre-processing flow --- src/lib/libpthread.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index 048f3807e680c..4ddbc2f6b6c9b 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -413,45 +413,44 @@ var LibraryPThread = { // Creates a new web Worker and places it in the unused worker pool to wait for its use. allocateUnusedWorker() { - var workerUrl, workerPolicyUrl; -#if expectToReceiveOnModule('mainScriptUrlOrBlob') - // We can't use makeModuleReceiveWithVar here since we want to also - // call URL.createObjectURL on the mainScriptUrlOrBlob. - workerPolicyUrl = workerUrl = Module['mainScriptUrlOrBlob']; - if (workerUrl && typeof workerUrl != 'string') { - workerPolicyUrl = workerUrl = URL.createObjectURL(workerUrl); - } -#endif - if (!workerUrl) { + var workerUrl, workerSource; #if EXPORT_ES6 - workerPolicyUrl = import.meta.url + workerSource = import.meta.url; #else - workerUrl = workerPolicyUrl = _scriptName; + workerUrl = workerSource = _scriptName; #endif +#if expectToReceiveOnModule('mainScriptUrlOrBlob') + if (Module['mainScriptUrlOrBlob']) { + // We can't use makeModuleReceiveWithVar here since we want to also + // call URL.createObjectURL on the mainScriptUrlOrBlob. + workerSource = workerUrl = Module['mainScriptUrlOrBlob']; + if (typeof workerUrl != 'string') { + workerSource = workerUrl = URL.createObjectURL(workerUrl); + } } +#endif #if PTHREADS_DEBUG - dbg(`Allocating a new web worker from ${workerUrl ?? workerPolicyUrl}`); + dbg(`Allocating a new web worker from ${workerSource}`); #endif #if TRUSTED_TYPES // Use Trusted Types compatible wrappers. if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) { - var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => workerPolicyUrl }); + var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => workerSource }); workerUrl = p.createScriptURL('ignored'); } #endif - var options = {{{ pthreadWorkerOptions }}}; + var worker, workerOptions = {{{ pthreadWorkerOptions }}}; #if EXPORT_ES6 - var worker = workerUrl - ? new Worker(workerUrl, options) + if (!workerUrl) { // We need to generate the URL with import.meta.url as the base URL of the JS file // instead of just using new URL(import.meta.url) because bundler's only recognize // the first case in their bundling step. The latter ends up producing an invalid // URL to import from the server (e.g., for webpack the file:// path). // See https://github.com/webpack/webpack/issues/12638 - : new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), options); -#else - var worker = new Worker(workerUrl, options); + worker = new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), workerOptions); + } else #endif + worker = new Worker(workerUrl, workerOptions); #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; #endif From d055cc889d9aa9bc5c7bb1615814ee3b84248b05 Mon Sep 17 00:00:00 2001 From: Jonathan Gamble Date: Fri, 7 Mar 2025 22:41:42 -0600 Subject: [PATCH 6/6] remove worker options var to appease vite --- src/lib/libpthread.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index 4ddbc2f6b6c9b..d767e1dc953bb 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -439,7 +439,7 @@ var LibraryPThread = { workerUrl = p.createScriptURL('ignored'); } #endif - var worker, workerOptions = {{{ pthreadWorkerOptions }}}; + var worker; #if EXPORT_ES6 if (!workerUrl) { // We need to generate the URL with import.meta.url as the base URL of the JS file @@ -447,10 +447,10 @@ var LibraryPThread = { // the first case in their bundling step. The latter ends up producing an invalid // URL to import from the server (e.g., for webpack the file:// path). // See https://github.com/webpack/webpack/issues/12638 - worker = new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), workerOptions); + worker = new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), {{{ pthreadWorkerOptions }}}); } else #endif - worker = new Worker(workerUrl, workerOptions); + worker = new Worker(workerUrl, {{{ pthreadWorkerOptions }}}); #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; #endif