Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions src/lib/libpthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,49 +413,44 @@ var LibraryPThread = {

// Creates a new web Worker and places it in the unused worker pool to wait for its use.
allocateUnusedWorker() {
var worker;
var workerUrl, workerSource;
#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
workerSource = import.meta.url;
#else
workerUrl = workerSource = _scriptName;
#endif
// 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);
// 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 ${pthreadMainJs}`);
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) => pthreadMainJs });
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}});
var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => workerSource });
workerUrl = p.createScriptURL('ignored');
}
#endif
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
// 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
#endif
worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions }}});
#endif // EXPORT_ES6
worker = new Worker(workerUrl, {{{ pthreadWorkerOptions }}});
#if ASSERTIONS
worker.workerID = PThread.nextWorkerID++;
#endif
Expand Down