From 2a88d528c3add92536f1e74221f5c05ee4dd202a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Mon, 3 Feb 2020 11:53:34 +0200 Subject: [PATCH 1/2] Remove ENVIRONMENT_HAS_NODE as redundant. --- src/compiler.js | 2 +- src/library_nodefs.js | 4 ++-- src/library_noderawfs.js | 2 +- src/library_pthread.js | 4 ++-- src/preamble.js | 4 ++-- src/runtime_init_memory.js | 2 +- src/shell.js | 18 ++++++------------ 7 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/compiler.js b/src/compiler.js index 3babfb7b99e70..a2d47449e8426 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -199,7 +199,7 @@ ENVIRONMENT_MAY_BE_SHELL = !ENVIRONMENT || ENVIRONMENTS.indexOf('shell') >= 0; // The worker case also includes Node.js workers when pthreads are // enabled and Node.js is one of the supported environments for the build to // run on. Node.js workers are detected as a combination of -// ENVIRONMENT_IS_WORKER and ENVIRONMENT_HAS_NODE. +// ENVIRONMENT_IS_WORKER and ENVIRONMENT_IS_NODE. ENVIRONMENT_MAY_BE_WORKER = !ENVIRONMENT || ENVIRONMENTS.indexOf('worker') >= 0 || (ENVIRONMENT_MAY_BE_NODE && USE_PTHREADS); diff --git a/src/library_nodefs.js b/src/library_nodefs.js index 84f77e719f488..780c15ecf3053 100644 --- a/src/library_nodefs.js +++ b/src/library_nodefs.js @@ -5,7 +5,7 @@ mergeInto(LibraryManager.library, { $NODEFS__deps: ['$FS', '$PATH', '$ERRNO_CODES'], - $NODEFS__postset: 'if (ENVIRONMENT_HAS_NODE) { var fs = require("fs"); var NODEJS_PATH = require("path"); NODEFS.staticInit(); }', + $NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { var fs = require("fs"); var NODEJS_PATH = require("path"); NODEFS.staticInit(); }', $NODEFS: { isWindows: false, staticInit: function() { @@ -38,7 +38,7 @@ mergeInto(LibraryManager.library, { return ERRNO_CODES[code]; }, mount: function (mount) { - assert(ENVIRONMENT_HAS_NODE); + assert(ENVIRONMENT_IS_NODE); return NODEFS.createNode(null, '/', NODEFS.getMode(mount.opts.root), 0); }, createNode: function (parent, name, mode, dev) { diff --git a/src/library_noderawfs.js b/src/library_noderawfs.js index a3f65cc5729ee..b89ab4cb68e88 100644 --- a/src/library_noderawfs.js +++ b/src/library_noderawfs.js @@ -5,7 +5,7 @@ mergeInto(LibraryManager.library, { $NODERAWFS__deps: ['$ERRNO_CODES', '$FS', '$NODEFS'], - $NODERAWFS__postset: 'if (ENVIRONMENT_HAS_NODE) {' + + $NODERAWFS__postset: 'if (ENVIRONMENT_IS_NODE) {' + 'var _wrapNodeError = function(func) { return function() { try { return func.apply(this, arguments) } catch (e) { if (!e.code) throw e; throw new FS.ErrnoError(ERRNO_CODES[e.code]); } } };' + 'var VFS = Object.assign({}, FS);' + 'for (var _key in NODERAWFS) FS[_key] = _wrapNodeError(NODERAWFS[_key]);' + diff --git a/src/library_pthread.js b/src/library_pthread.js index b920e442da2ad..765ed6d48902f 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -359,7 +359,7 @@ var LibraryPThread = { }; #if ENVIRONMENT_MAY_BE_NODE - if (ENVIRONMENT_HAS_NODE) { + if (ENVIRONMENT_IS_NODE) { worker.on('message', function(data) { worker.onmessage({ data: data }); }); @@ -910,7 +910,7 @@ var LibraryPThread = { else PThread.threadExit(status); #if WASM_BACKEND // pthread_exit is marked noReturn, so we must not return from it. - if (ENVIRONMENT_HAS_NODE) { + if (ENVIRONMENT_IS_NODE) { // exit the pthread properly on node, as a normal JS exception will halt // the entire application. process.exit(status); diff --git a/src/preamble.js b/src/preamble.js index c5d3cee591ad5..0816c6913e2e8 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -1037,7 +1037,7 @@ function createWasm() { try { binary = getBinary(); #if NODE_CODE_CACHING - if (ENVIRONMENT_HAS_NODE) { + if (ENVIRONMENT_IS_NODE) { var v8 = require('v8'); // Include the V8 version in the cache name, so that we don't try to // load cached code from another version, which fails silently (it seems @@ -1062,7 +1062,7 @@ err(module); if (!module) { module = new WebAssembly.Module(binary); } - if (ENVIRONMENT_HAS_NODE) { + if (ENVIRONMENT_IS_NODE) { if (!hasCached) { #if RUNTIME_LOGGING err('NODE_CODE_CACHING: saving module'); diff --git a/src/runtime_init_memory.js b/src/runtime_init_memory.js index dfbcd92c46d2a..db46e847c10f5 100644 --- a/src/runtime_init_memory.js +++ b/src/runtime_init_memory.js @@ -33,7 +33,7 @@ if (ENVIRONMENT_IS_PTHREAD) { #if USE_PTHREADS if (!(wasmMemory.buffer instanceof SharedArrayBuffer)) { err('requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag'); - if (ENVIRONMENT_HAS_NODE) { + if (ENVIRONMENT_IS_NODE) { console.log('(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and also use a recent version)'); } throw Error('bad memory'); diff --git a/src/shell.js b/src/shell.js index 8630c8a46e087..65cdeb7af15d4 100644 --- a/src/shell.js +++ b/src/shell.js @@ -60,23 +60,17 @@ var quit_ = function(status, toThrow) { var ENVIRONMENT_IS_WEB = {{{ ENVIRONMENT === 'web' }}}; var ENVIRONMENT_IS_WORKER = {{{ ENVIRONMENT === 'worker' }}}; var ENVIRONMENT_IS_NODE = {{{ ENVIRONMENT === 'node' }}}; -var ENVIRONMENT_HAS_NODE = ENVIRONMENT_IS_NODE; var ENVIRONMENT_IS_SHELL = {{{ ENVIRONMENT === 'shell' }}}; #else // ENVIRONMENT var ENVIRONMENT_IS_WEB = false; var ENVIRONMENT_IS_WORKER = false; var ENVIRONMENT_IS_NODE = false; -var ENVIRONMENT_HAS_NODE = false; var ENVIRONMENT_IS_SHELL = false; ENVIRONMENT_IS_WEB = typeof window === 'object'; ENVIRONMENT_IS_WORKER = typeof importScripts === 'function'; -// A web environment like Electron.js can have Node enabled, so we must -// distinguish between Node-enabled environments and Node environments per se. -// This will allow the former to do things like mount NODEFS. -// Extended check using process.versions fixes issue #8816. -// (Also makes redundant the original check that 'require' is a function.) -ENVIRONMENT_HAS_NODE = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string'; -ENVIRONMENT_IS_NODE = ENVIRONMENT_HAS_NODE && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER; +// N.b. Electron.js environment is simultaneously a NODE-environment, but +// also a web environment. +ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string'; ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER; #endif // ENVIRONMENT @@ -259,7 +253,7 @@ if (ENVIRONMENT_IS_SHELL) { // Note that this includes Node.js workers when relevant (pthreads is enabled). // Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and -// ENVIRONMENT_HAS_NODE. +// ENVIRONMENT_IS_NODE. #if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled @@ -293,7 +287,7 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { // Differentiate the Web Worker from the Node Worker case, as reading must // be done differently. #if USE_PTHREADS - if (ENVIRONMENT_HAS_NODE) { + if (ENVIRONMENT_IS_NODE) { #include "node_shell_read.js" @@ -315,7 +309,7 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { } #if ENVIRONMENT_MAY_BE_NODE && USE_PTHREADS -if (ENVIRONMENT_HAS_NODE) { +if (ENVIRONMENT_IS_NODE) { // Polyfill the performance object, which emscripten pthreads support // depends on for good timing. if (typeof performance === 'undefined') { From 5decc7028f22142b87cec134ba6e39b976d22094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Mon, 3 Feb 2020 14:31:52 +0200 Subject: [PATCH 2/2] Populate _scriptDir and scriptDirectory for Node.js pthreads --- src/shell.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/shell.js b/src/shell.js index 65cdeb7af15d4..e3f06e0b87f66 100644 --- a/src/shell.js +++ b/src/shell.js @@ -92,10 +92,10 @@ var _scriptDir = import.meta.url; #else var _scriptDir = (typeof document !== 'undefined' && document.currentScript) ? document.currentScript.src : undefined; -if (ENVIRONMENT_IS_NODE) { - _scriptDir = __filename; -} else if (ENVIRONMENT_IS_WORKER) { +if (ENVIRONMENT_IS_WORKER) { _scriptDir = self.location.href; +} else if (ENVIRONMENT_IS_NODE) { + _scriptDir = __filename; } #endif #endif @@ -127,7 +127,11 @@ if (ENVIRONMENT_IS_NODE) { if (!(typeof process === 'object' && typeof require === '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?)'); #endif #endif - scriptDirectory = __dirname + '/'; + if (ENVIRONMENT_IS_WORKER) { + scriptDirectory = require('path').dirname(scriptDirectory) + '/'; + } else { + scriptDirectory = __dirname + '/'; + } #include "node_shell_read.js"