Skip to content

Commit 6fc5d1c

Browse files
mamekateinoigakukun
authored andcommitted
Postpone writing to stdout/stderr until a newline arrives
1 parent e4468e0 commit 6fc5d1c

File tree

1 file changed

+10
-1
lines changed
  • packages/npm-packages/ruby-wasm-wasi/src

1 file changed

+10
-1
lines changed

packages/npm-packages/ruby-wasm-wasi/src/browser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const DefaultRubyVM = async (
1616

1717
if (options.consolePrint) {
1818
const originalWriteSync = wasmFs.fs.writeSync.bind(wasmFs.fs);
19+
const stdOutErrBuffers = { 1: "", 2: "" };
1920
wasmFs.fs.writeSync = function () {
2021
let fd: number = arguments[0];
2122
let text: string;
@@ -29,7 +30,15 @@ export const DefaultRubyVM = async (
2930
1: (line: string) => console.log(line),
3031
2: (line: string) => console.warn(line),
3132
};
32-
if (handlers[fd]) handlers[fd](text);
33+
if (handlers[fd]) {
34+
text = stdOutErrBuffers[fd] + text;
35+
let i = text.lastIndexOf("\n");
36+
if (i >= 0) {
37+
handlers[fd](text.substring(0, i + 1));
38+
text = text.substring(i + 1);
39+
}
40+
stdOutErrBuffers[fd] = text;
41+
}
3342
return originalWriteSync(...arguments);
3443
};
3544
}

0 commit comments

Comments
 (0)