Skip to content

Commit

Permalink
Fix pthread compatibility on Deno
Browse files Browse the repository at this point in the history
`importScripts()` is always `undefined` on Deno.
  • Loading branch information
kleisauke committed Oct 23, 2024
1 parent 999305f commit 67924ad
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/library_pthread_stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var LibraryPThreadStub = {

emscripten_is_main_browser_thread: () =>
#if MINIMAL_RUNTIME
typeof importScripts == 'undefined'
typeof WorkerGlobalScope == 'undefined'
#else
!ENVIRONMENT_IS_WORKER
#endif
Expand Down
11 changes: 3 additions & 8 deletions src/runtime_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ if (ENVIRONMENT_IS_PTHREAD) {

Object.assign(globalThis, {
self: global,
// Dummy importScripts. The presence of this global is used
// to detect that we are running on a Worker.
// TODO(sbc): Find another way?
importScripts: () => {
#if ASSERTIONS
assert(false, 'dummy importScripts called');
#endif
},
// The presence of this global is used to detect
// that we are running on a Worker.
WorkerGlobalScope: global,
postMessage: (msg) => parentPort.postMessage(msg),
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var ENVIRONMENT_IS_AUDIO_WORKLET = typeof AudioWorkletGlobalScope !== 'undefined
var ENVIRONMENT_IS_WEB = {{{ ENVIRONMENT === 'web' }}};
#if PTHREADS && ENVIRONMENT_MAY_BE_NODE
// node+pthreads always supports workers; detect which we are at runtime
var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function';
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
#else
var ENVIRONMENT_IS_WORKER = {{{ ENVIRONMENT === 'worker' }}};
#endif
Expand All @@ -96,7 +96,7 @@ var ENVIRONMENT_IS_SHELL = {{{ ENVIRONMENT === 'shell' }}};
#else // ENVIRONMENT
// Attempt to auto-detect the environment
var ENVIRONMENT_IS_WEB = typeof window == 'object';
var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function';
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
// N.b. Electron.js environment is simultaneously a NODE-environment, but
// also a web environment.
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
Expand Down Expand Up @@ -337,7 +337,7 @@ if (ENVIRONMENT_IS_DENO) { // Deno
if (ENVIRONMENT_IS_SHELL) {

#if ENVIRONMENT && ASSERTIONS
if ((typeof process == 'object' && typeof require === 'function') || typeof window == 'object' || typeof importScripts == 'function') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
if ((typeof process == 'object' && typeof require === 'function') || typeof window == 'object' || typeof WorkerGlobalScope != 'undefined') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
#endif

#if ENVIRONMENT_MAY_BE_SHELL
Expand Down Expand Up @@ -423,7 +423,7 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
}

#if ENVIRONMENT && ASSERTIONS
if (!(typeof window == 'object' || typeof importScripts == 'function')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
if (!(typeof window == 'object' || typeof WorkerGlobalScope != 'undefined')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
#endif

#if PTHREADS && ENVIRONMENT_MAY_BE_NODE
Expand Down
2 changes: 1 addition & 1 deletion src/shell_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function ready() {
#if PTHREADS
// MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments
// coincide.
var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function';
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
var ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && self.name?.startsWith('em-pthread');

#if ENVIRONMENT_MAY_BE_NODE
Expand Down

0 comments on commit 67924ad

Please sign in to comment.