From cf6cdde22382590929698e570f26afce0d0c0a1f Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Tue, 8 Nov 2022 19:42:56 +0100 Subject: [PATCH] Ensure that stdin/stdout/stderr are not mapped 1:1 under NODERAWFS (PR 18163) Previously, when linking with -sNODERAWFS, the stdin, stdout and stderr streams were respectively attached to file descriptors 0, 1 and 2 of the running Node process. This implicitly means that overriding the print, printErr, stdin, stdout and stderr handlers on the incoming module wasn't possible. This commit ensures that stdin/stdout/stderr uses the library_tty.js implementation for its printing, which leverages the console object, whenever the above handlers weren't overriden. This matches what is done when filesystem support isn't included. --- src/library_noderawfs.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/library_noderawfs.js b/src/library_noderawfs.js index 0bc91928be4e..6a53f446c697 100644 --- a/src/library_noderawfs.js +++ b/src/library_noderawfs.js @@ -7,7 +7,19 @@ mergeInto(LibraryManager.library, { $NODERAWFS__deps: ['$ERRNO_CODES', '$FS', '$NODEFS', '$mmapAlloc'], $NODERAWFS__postset: ` - if (ENVIRONMENT_IS_NODE) { + if (!ENVIRONMENT_IS_NODE) { + throw new Error("NODERAWFS is currently only supported on Node.js environment.") + } + // Use this to reference our in-memory filesystem + var VFS = Object.assign({}, FS); + // Override the init function with our own + FS.init = NODERAWFS.init;`, + $NODERAWFS: { + init: (input, output, error) => { + // Call the original FS.init, this will setup the + // stdin, stdout and stderr devices + VFS.init(input, output, error); + var _wrapNodeError = function(func) { return function() { try { @@ -20,14 +32,13 @@ mergeInto(LibraryManager.library, { } } }; - var VFS = Object.assign({}, FS); + + // Wrap the whole in-memory filesystem API with + // our Node.js based functions for (var _key in NODERAWFS) { FS[_key] = _wrapNodeError(NODERAWFS[_key]); } - } else { - throw new Error("NODERAWFS is currently only supported on Node.js environment.") - }`, - $NODERAWFS: { + }, lookup: function(parent, name) { #if ASSERTIONS assert(parent) @@ -44,12 +55,6 @@ mergeInto(LibraryManager.library, { var mode = NODEFS.getMode(path); return { path: path, node: { id: st.ino, mode: mode, node_ops: NODERAWFS, path: path }}; }, - createStandardStreams: function() { - FS.streams[0] = FS.createStream({ nfd: 0, position: 0, path: '', flags: 0, tty: true, seekable: false }, 0, 0); - for (var i = 1; i < 3; i++) { - FS.streams[i] = FS.createStream({ nfd: i, position: 0, path: '', flags: 577, tty: true, seekable: false }, i, i); - } - }, // generic function for all node creation cwd: function() { return process.cwd(); }, chdir: function() { process.chdir.apply(void 0, arguments); },