Skip to content

Commit ace27ff

Browse files
committed
rubyのoutput処理を修正
1 parent 52abfd2 commit ace27ff

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

app/terminal/repl.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import { useEmbedContext } from "./embedContext";
1717
import { emptyMutex, langConstants, RuntimeLang, useRuntime } from "./runtime";
1818
import clsx from "clsx";
1919

20+
export type ReplOutputType = "stdout" | "stderr" | "error" | "return" | "trace" | "system";
2021
export interface ReplOutput {
21-
type: "stdout" | "stderr" | "error" | "return" | "trace" | "system"; // 出力の種類
22+
type: ReplOutputType; // 出力の種類
2223
message: string; // 出力メッセージ
2324
}
2425
export interface ReplCommand {

app/terminal/worker/ruby.worker.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { expose } from "comlink";
55
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/browser";
66
import type { RubyVM } from "@ruby/wasm-wasi/dist/vm";
77
import type { WorkerCapabilities } from "./runtime";
8-
import type { ReplOutput } from "../repl";
8+
import type { ReplOutput, ReplOutputType } from "../repl";
99

1010
import init_rb from "./ruby/init.rb?raw";
1111

@@ -21,29 +21,26 @@ declare global {
2121
self.stdout = {
2222
write(str: string) {
2323
stdoutBuffer += str;
24-
// If buffer contains newlines, flush complete lines immediately
25-
if (stdoutBuffer.includes("\n")) {
26-
const lines = stdoutBuffer.split("\n");
27-
for (let i = 0; i < lines.length - 1; i++) {
28-
currentOutputCallback?.({ type: "stdout", message: lines[i] });
29-
}
30-
stdoutBuffer = lines[lines.length - 1];
31-
}
24+
stdoutBuffer = handleBatchOutput(stdoutBuffer, "stdout");
3225
},
3326
};
3427
self.stderr = {
3528
write(str: string) {
3629
stderrBuffer += str;
37-
// If buffer contains newlines, flush complete lines immediately
38-
if (stderrBuffer.includes("\n")) {
39-
const lines = stderrBuffer.split("\n");
40-
for (let i = 0; i < lines.length - 1; i++) {
41-
currentOutputCallback?.({ type: "stderr", message: lines[i] });
42-
}
43-
stderrBuffer = lines[lines.length - 1];
44-
}
30+
stderrBuffer = handleBatchOutput(stderrBuffer, "stderr");
4531
},
4632
};
33+
function handleBatchOutput(buffer: string, type: ReplOutputType): string {
34+
// If buffer contains newlines, flush complete lines immediately
35+
if (buffer.includes("\n")) {
36+
const lines = buffer.split("\n");
37+
for (let i = 0; i < lines.length - 1; i++) {
38+
currentOutputCallback?.({ type, message: lines[i] });
39+
}
40+
return lines[lines.length - 1];
41+
}
42+
return buffer;
43+
}
4744

4845
async function init(/*_interruptBuffer?: Uint8Array*/): Promise<{
4946
capabilities: WorkerCapabilities;
@@ -122,6 +119,9 @@ async function runCode(
122119

123120
const resultStr = await result.callAsync("inspect");
124121

122+
// Flush any buffered output
123+
flushOutput();
124+
125125
// Add result to output if it's not nil and not empty
126126
onOutput({
127127
type: "return",
@@ -130,13 +130,14 @@ async function runCode(
130130
} catch (e) {
131131
console.log(e);
132132

133+
// Flush any buffered output
134+
flushOutput();
135+
133136
onOutput({
134137
type: "error",
135138
message: formatRubyError(e, false),
136139
});
137140
} finally {
138-
// Flush any buffered output
139-
flushOutput();
140141
currentOutputCallback = null;
141142
}
142143

@@ -175,16 +176,20 @@ async function runFile(
175176

176177
// Run the specified file
177178
rubyVM.eval(`load ${JSON.stringify(name)}`);
179+
180+
// Flush any buffered output
181+
flushOutput();
178182
} catch (e) {
179183
console.log(e);
180184

185+
// Flush any buffered output
186+
flushOutput();
187+
181188
onOutput({
182189
type: "error",
183190
message: formatRubyError(e, true),
184191
});
185192
} finally {
186-
// Flush any buffered output
187-
flushOutput();
188193
currentOutputCallback = null;
189194
}
190195

0 commit comments

Comments
 (0)