Skip to content

Commit

Permalink
Ensure that stdin/stdout/stderr are not mapped 1:1 under NODERAWFS (P…
Browse files Browse the repository at this point in the history
…R 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.
  • Loading branch information
kleisauke committed Jan 5, 2023
1 parent 9fd2fa5 commit cf6cdde
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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); },
Expand Down

0 comments on commit cf6cdde

Please sign in to comment.