diff --git a/src/library_fs.js b/src/library_fs.js index ba44faf3de707..d3f70f83f45ad 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -150,8 +150,8 @@ FS.staticInit(); this.node_ops = {}; this.stream_ops = {}; this.rdev = rdev; - this.readMode = 292/*{{{ cDefs.S_IRUGO }}}*/ | 73/*{{{ cDefs.S_IXUGO }}}*/; - this.writeMode = 146/*{{{ cDefs.S_IWUGO }}}*/; + this.readMode = {{{ cDefs.S_IRUGO }}} | {{{ cDefs.S_IXUGO }}}; + this.writeMode = {{{ cDefs.S_IWUGO }}}; } get read() { return (this.mode & this.readMode) === this.readMode; diff --git a/src/library_noderawfs.js b/src/library_noderawfs.js index b43f196f67e5a..1871e35a7e787 100644 --- a/src/library_noderawfs.js +++ b/src/library_noderawfs.js @@ -15,11 +15,7 @@ addToLibrary({ // 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); - + init() { var _wrapNodeError = function(func) { return function(...args) { try { @@ -39,6 +35,9 @@ addToLibrary({ /** @suppress {partialAlias} */ FS[_key] = _wrapNodeError(NODERAWFS[_key]); } + + // Setup the stdin, stdout and stderr devices + FS.createStandardStreams(); }, lookup(parent, name) { #if ASSERTIONS @@ -55,6 +54,13 @@ addToLibrary({ var mode = NODEFS.getMode(path); return { path, node: { id: st.ino, mode, node_ops: NODERAWFS, path }}; }, + createStandardStreams() { + // FIXME: tty is set to true to appease isatty(), the underlying ioctl syscalls still needs to be implemented, see issue #22264. + FS.createStream({ nfd: 0, position: 0, path: '', flags: 0, tty: true, seekable: false }, 0); + for (var i = 1; i < 3; i++) { + FS.createStream({ nfd: i, position: 0, path: '', flags: {{{ cDefs.O_TRUNC | cDefs.O_CREAT | cDefs.O_WRONLY }}}, tty: true, seekable: false }, i); + } + }, // generic function for all node creation cwd() { return process.cwd(); }, chdir(...args) { process.chdir(...args); }, diff --git a/test/other/test_fflush_fs_exit.out b/test/other/test_fflush_fs_exit.out index 2b61dcbf7e816..347b8f14a5e19 100644 --- a/test/other/test_fflush_fs_exit.out +++ b/test/other/test_fflush_fs_exit.out @@ -1,2 +1,2 @@ Hey1 -Hey2 +Hey2 \ No newline at end of file diff --git a/test/test_other.py b/test/test_other.py index ff2dd5cff65e9..8b58e2ae1474f 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -1675,7 +1675,6 @@ def test_stdin(self, args): os.system(f'cat in.txt | {cmd} > out.txt') self.assertContained('abcdef\nghijkl\neof', read_file('out.txt')) - @also_with_noderawfs @crossplatform def test_module_stdin(self): self.set_setting('FORCE_FILESYSTEM') @@ -1687,18 +1686,36 @@ def test_module_stdin(self): self.emcc_args += ['--pre-js', 'pre.js'] self.do_runf('module/test_stdin.c', 'hello, world!') - @also_with_noderawfs @crossplatform def test_module_stdout_stderr(self): self.set_setting('FORCE_FILESYSTEM') create_file('pre.js', ''' +let stdout = []; +let stderr = []; + +Module['stdout'] = (char) => stdout.push(char); +Module['stderr'] = (char) => stderr.push(char); +Module['postRun'] = () => { + assert(stderr.length === 0, 'stderr should be empty. \\n' + + 'stderr: \\n' + stderr); + assert(UTF8ArrayToString(stdout, 0).startsWith('hello, world!'), 'stdout should start with the famous greeting. \\n' + + 'stdout: \\n' + stdout); +} +''') + self.emcc_args += ['--pre-js', 'pre.js'] + self.do_runf('hello_world.c') + + @crossplatform + def test_module_print_printerr(self): + self.set_setting('FORCE_FILESYSTEM') + create_file('pre.js', ''' let stdout = ''; let stderr = ''; Module['print'] = (text) => stdout += text; Module['printErr'] = (text) => stderr += text; Module['postRun'] = () => { - assert(stderr == '', 'stderr should be empty. \\n' + + assert(stderr === '', 'stderr should be empty. \\n' + 'stderr: \\n' + stderr); assert(stdout.startsWith('hello, world!'), 'stdout should start with the famous greeting. \\n' + 'stdout: \\n' + stdout); @@ -9716,7 +9733,7 @@ def test_ioctl(self): # ioctl requires filesystem self.do_other_test('test_ioctl.c', emcc_args=['-sFORCE_FILESYSTEM']) - @also_with_noderawfs + # @also_with_noderawfs # NODERAWFS needs to implement the ioctl syscalls, see issue #22264. def test_ioctl_termios(self): # ioctl requires filesystem self.do_other_test('test_ioctl_termios.c', emcc_args=['-sFORCE_FILESYSTEM']) @@ -9733,6 +9750,7 @@ def test_fflush_fs(self): # fflush with the full filesystem will flush from libc, but not the JS logging, which awaits a newline self.do_other_test('test_fflush_fs.cpp', emcc_args=['-sFORCE_FILESYSTEM']) + @also_with_noderawfs def test_fflush_fs_exit(self): # on exit, we can send out a newline as no more code will run self.do_other_test('test_fflush_fs_exit.cpp', emcc_args=['-sFORCE_FILESYSTEM', '-sEXIT_RUNTIME'])