Skip to content

Commit

Permalink
Test reading from stdin in shell environments (#21926)
Browse files Browse the repository at this point in the history
Also refactor FS_stdin_getChar so that it only includes the shell
code when needed.

I verified that this test runs under spidermonkey and v8.
  • Loading branch information
sbc100 committed May 9, 2024
1 parent 7c7ea15 commit 2a08958
Show file tree
Hide file tree
Showing 20 changed files with 33 additions and 29 deletions.
19 changes: 12 additions & 7 deletions src/library_fs_shared.js
Expand Up @@ -136,33 +136,38 @@ addToLibrary({
try {
bytesRead = fs.readSync(fd, buf, 0, BUFSIZE);
} catch(e) {
// Cross-platform differences: on Windows, reading EOF throws an exception, but on other OSes,
// reading EOF returns 0. Uniformize behavior by treating the EOF exception to return 0.
// Cross-platform differences: on Windows, reading EOF throws an
// exception, but on other OSes, reading EOF returns 0. Uniformize
// behavior by treating the EOF exception to return 0.
if (e.toString().includes('EOF')) bytesRead = 0;
else throw e;
}

if (bytesRead > 0) {
result = buf.slice(0, bytesRead).toString('utf-8');
} else {
result = null;
}
} else
#endif
#if ENVIRONMENT_MAY_BE_WEB
if (typeof window != 'undefined' &&
typeof window.prompt == 'function') {
// Browser.
result = window.prompt('Input: '); // returns null on cancel
if (result !== null) {
result += '\n';
}
} else if (typeof readline == 'function') {
} else
#endif
#if ENVIRONMENT_MAY_BE_SHELL
if (typeof readline == 'function') {
// Command line.
result = readline();
if (result !== null) {
if (result) {
result += '\n';
}
}
} else
#endif
{}
if (!result) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.gzsize
@@ -1 +1 @@
9840
9828
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
@@ -1 +1 @@
24247
24180
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.gzsize
@@ -1 +1 @@
9823
9811
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
@@ -1 +1 @@
24215
24148
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.gzsize
@@ -1 +1 @@
10931
10915
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
@@ -1 +1 @@
28164
28097
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.gzsize
@@ -1 +1 @@
9807
9794
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
@@ -1 +1 @@
24140
24073
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.gzsize
@@ -1 +1 @@
10932
10915
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
@@ -1 +1 @@
28164
28097
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.gzsize
@@ -1 +1 @@
9840
9828
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
@@ -1 +1 @@
24247
24180
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_wasmfs.gzsize
@@ -1 +1 @@
5210
5194
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_wasmfs.jssize
@@ -1 +1 @@
12062
11995
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.gzsize
@@ -1 +1 @@
7802
7790
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.jssize
@@ -1 +1 @@
19271
19204
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_wasmfs.gzsize
@@ -1 +1 @@
3133
3110
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_wasmfs.jssize
@@ -1 +1 @@
6812
6745
7 changes: 3 additions & 4 deletions test/test_other.py
Expand Up @@ -1649,19 +1649,18 @@ def test_export_all_and_exported_functions(self):
@also_with_wasmfs
@crossplatform
def test_stdin(self, args):
create_file('in.txt', 'abcdef\nghijkl')
create_file('in.txt', 'abcdef\nghijkl\n')
self.set_setting('ENVIRONMENT', 'node,shell')
self.emcc(test_file('module/test_stdin.c'), args=args, output_filename='out.js')

for engine in config.JS_ENGINES:
if engine == config.V8_ENGINE:
continue # no stdin support in v8 shell
engine[0] = os.path.normpath(engine[0])
print(engine, file=sys.stderr)
# work around a bug in python's subprocess module
# (we'd use self.run_js() normally)
delete_file('out.txt')
cmd = jsrun.make_command(os.path.normpath('out.js'), engine)
cmd = shared.shlex_join(cmd)
print(cmd, file=sys.stderr)
if WINDOWS:
os.system(f'type "in.txt" | {cmd} >out.txt')
else: # posix
Expand Down

0 comments on commit 2a08958

Please sign in to comment.