Skip to content

Commit 35244c8

Browse files
committed
consoleとinterruptを修正
1 parent bdfa2a1 commit 35244c8

File tree

2 files changed

+34
-40
lines changed

2 files changed

+34
-40
lines changed

app/terminal/javascript/runtime.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,22 @@ export function JavaScriptProvider({ children }: { children: ReactNode }) {
109109
};
110110
}, [initializeWorker]);
111111

112-
const interrupt = useCallback(async () => {
112+
const interrupt = useCallback(() => {
113113
// Since we can't interrupt JavaScript execution directly,
114114
// we terminate the worker and restart it, then restore state
115-
await mutex.current.runExclusive(async () => {
116-
// Reject all pending callbacks before terminating
117-
const error = "Worker interrupted";
118-
messageCallbacks.current.forEach(([, reject]) => reject(error));
119-
messageCallbacks.current.clear();
120-
121-
// Terminate the current worker
122-
workerRef.current?.terminate();
123-
124-
// Reset ready state
125-
setReady(false);
126-
115+
116+
// Reject all pending callbacks before terminating
117+
const error = "Worker interrupted";
118+
messageCallbacks.current.forEach(([, reject]) => reject(error));
119+
messageCallbacks.current.clear();
120+
121+
// Terminate the current worker
122+
workerRef.current?.terminate();
123+
124+
// Reset ready state
125+
setReady(false);
126+
127+
mutex.current.runExclusive(async () => {
127128
// Create a new worker and wait for it to be ready
128129
await initializeWorker();
129130

public/javascript.worker.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
let jsOutput = [];
33

44
// Helper function to capture console output
5-
function createConsoleProxy() {
6-
return {
7-
log: (...args) => {
8-
jsOutput.push({ type: "stdout", message: args.join(" ") });
9-
},
10-
error: (...args) => {
11-
jsOutput.push({ type: "stderr", message: args.join(" ") });
12-
},
13-
warn: (...args) => {
14-
jsOutput.push({ type: "stderr", message: args.join(" ") });
15-
},
16-
info: (...args) => {
17-
jsOutput.push({ type: "stdout", message: args.join(" ") });
18-
},
19-
};
20-
}
5+
const originalConsole = globalThis.console;
6+
globalThis.console = {
7+
log: (...args) => {
8+
jsOutput.push({ type: "stdout", message: args.join(" ") });
9+
},
10+
error: (...args) => {
11+
jsOutput.push({ type: "stderr", message: args.join(" ") });
12+
},
13+
warn: (...args) => {
14+
jsOutput.push({ type: "stderr", message: args.join(" ") });
15+
},
16+
info: (...args) => {
17+
jsOutput.push({ type: "stdout", message: args.join(" ") });
18+
},
19+
};
2120

2221
async function init(id) {
2322
// Initialize the worker
@@ -27,12 +26,9 @@ async function init(id) {
2726
async function runJavaScript(id, payload) {
2827
const { code } = payload;
2928
try {
30-
// Create a console proxy to capture output
31-
const console = createConsoleProxy();
32-
3329
// Execute code directly with eval in the worker global scope
3430
// This will preserve variables across calls
35-
const result = eval(code);
31+
const result = globalThis.eval(code);
3632

3733
if (result !== undefined) {
3834
jsOutput.push({
@@ -41,8 +37,7 @@ async function runJavaScript(id, payload) {
4137
});
4238
}
4339
} catch (e) {
44-
// Use self.console to avoid recursion with our console proxy
45-
self.console.log(e);
40+
originalConsole.log(e);
4641
if (e instanceof Error) {
4742
jsOutput.push({
4843
type: "error",
@@ -95,12 +90,10 @@ async function restoreState(id, payload) {
9590

9691
for (const command of commands) {
9792
try {
98-
const console = createConsoleProxy();
99-
eval(command);
93+
globalThis.eval(command);
10094
} catch (e) {
10195
// If restoration fails, we still continue with other commands
102-
// Use self.console to avoid recursion with our console proxy
103-
self.console.error("Failed to restore command:", command, e);
96+
originalConsole.error("Failed to restore command:", command, e);
10497
}
10598
}
10699

@@ -124,7 +117,7 @@ self.onmessage = async (event) => {
124117
await restoreState(id, payload);
125118
return;
126119
default:
127-
console.error(`Unknown message type: ${type}`);
120+
originalConsole.error(`Unknown message type: ${type}`);
128121
return;
129122
}
130123
};

0 commit comments

Comments
 (0)