Skip to content

Commit

Permalink
[NODERAWFS] Revert #18163 (#22393)
Browse files Browse the repository at this point in the history
This reverts commit c614fa5.

---

Here's a standalone reproducer:

Before:
```console
$ curl -LO https://github.com/llvm/llvm-test-suite/raw/main/SingleSource/Benchmarks/Misc-C++/Large/ray.cpp
$ em++ -O3 -sEXIT_RUNTIME -sNODERAWFS ray.cpp -o ray.js
$ node ray.js > x.ppm
$ sha256sum x.ppm
acaa1db08ee30dafda03b41c77466b1435fe8ffdd4d88e5a5876e87dff126f58  x.ppm
$ vips copy x.ppm x.png
VipsImage: memory area too small --- should be 262144 bytes, you passed 79485
```

After:
```console
$ em++ -O3 -sEXIT_RUNTIME -sNODERAWFS ray.cpp -o ray.js
$ node ray.js > x.ppm
$ sha256sum x.ppm
cd6fa6fa75b4ba29077f7e4e531b96f17ff0b82f3c70df526c36f748d6a38c5c  x.ppm
$ vips copy x.ppm x.png
```
<details>
  <summary>x.png</summary>


![x](https://github.com/user-attachments/assets/832af0bc-17af-4919-9c3c-72f0683079b8)
</details>
  • Loading branch information
kleisauke authored Aug 20, 2024
1 parent 72cbe0c commit 659f01d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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); },
Expand Down
2 changes: 1 addition & 1 deletion test/other/test_fflush_fs_exit.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Hey1
Hey2
Hey2
26 changes: 22 additions & 4 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);
Expand Down Expand Up @@ -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'])
Expand All @@ -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'])
Expand Down

0 comments on commit 659f01d

Please sign in to comment.