Skip to content

Commit 426df51

Browse files
Copilotna-trium-144
andcommitted
Simplify RuntimeContext: make runCommand required, remove IPyodideContext, refactor useWandbox with curried functions
Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent c08977b commit 426df51

File tree

3 files changed

+55
-64
lines changed

3 files changed

+55
-64
lines changed

app/terminal/python/pyodide.tsx

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@ import { Mutex, MutexInterface } from "async-mutex";
1414
import { useEmbedContext } from "../embedContext";
1515
import { RuntimeContext } from "../runtime";
1616

17-
interface IPyodideContext extends RuntimeContext {
18-
runPython: (code: string) => Promise<ReplOutput[]>;
19-
runCommand: (command: string) => Promise<ReplOutput[]>; // Alias for runPython for consistency
20-
runFile: (name: string) => Promise<ReplOutput[]>;
21-
}
22-
23-
const PyodideContext = createContext<IPyodideContext>(null!);
17+
const PyodideContext = createContext<RuntimeContext>(null!);
2418

25-
export function usePyodide() {
19+
export function usePyodide(): RuntimeContext {
2620
const context = useContext(PyodideContext);
2721
if (!context) {
2822
throw new Error("usePyodide must be used within a PyodideProvider");
@@ -125,10 +119,10 @@ export function PyodideProvider({ children }: { children: ReactNode }) {
125119
}
126120
}, []);
127121

128-
const runPython = useCallback(
122+
const runCommand = useCallback(
129123
async (code: string): Promise<ReplOutput[]> => {
130124
if (!mutex.current.isLocked()) {
131-
throw new Error("mutex of PyodideContext must be locked for runPython");
125+
throw new Error("mutex of PyodideContext must be locked for runCommand");
132126
}
133127
if (!workerRef.current || !ready) {
134128
return [{ type: "error", message: "Pyodide is not ready yet." }];
@@ -150,29 +144,6 @@ export function PyodideProvider({ children }: { children: ReactNode }) {
150144
[ready, writeFile]
151145
);
152146

153-
const runFile = useCallback(
154-
async (name: string): Promise<ReplOutput[]> => {
155-
if (!workerRef.current || !ready) {
156-
return [{ type: "error", message: "Pyodide is not ready yet." }];
157-
}
158-
if (interruptBuffer.current) {
159-
interruptBuffer.current[0] = 0;
160-
}
161-
return mutex.current.runExclusive(async () => {
162-
const { output, updatedFiles } =
163-
await postMessage<RunPayloadFromWorker>({
164-
type: "runFile",
165-
payload: { name, files },
166-
});
167-
for (const [newName, content] of updatedFiles) {
168-
writeFile(newName, content);
169-
}
170-
return output;
171-
});
172-
},
173-
[files, ready, writeFile]
174-
);
175-
176147
const checkSyntax = useCallback(
177148
async (code: string): Promise<SyntaxStatus> => {
178149
if (!workerRef.current || !ready) return "invalid";
@@ -197,9 +168,26 @@ export function PyodideProvider({ children }: { children: ReactNode }) {
197168
},
198169
];
199170
}
200-
return runFile(filenames[0]);
171+
// Incorporate runFile logic directly
172+
if (!workerRef.current || !ready) {
173+
return [{ type: "error", message: "Pyodide is not ready yet." }];
174+
}
175+
if (interruptBuffer.current) {
176+
interruptBuffer.current[0] = 0;
177+
}
178+
return mutex.current.runExclusive(async () => {
179+
const { output, updatedFiles } =
180+
await postMessage<RunPayloadFromWorker>({
181+
type: "runFile",
182+
payload: { name: filenames[0], files },
183+
});
184+
for (const [newName, content] of updatedFiles) {
185+
writeFile(newName, content);
186+
}
187+
return output;
188+
});
201189
},
202-
[runFile]
190+
[files, ready, writeFile]
203191
);
204192

205193
const splitContents = useCallback((content: string): ReplCommand[] => {
@@ -231,11 +219,9 @@ export function PyodideProvider({ children }: { children: ReactNode }) {
231219
init,
232220
initializing,
233221
ready,
234-
runPython,
235-
runCommand: runPython, // Alias for consistency with RuntimeContext
222+
runCommand,
236223
checkSyntax,
237224
mutex: mutex.current,
238-
runFile,
239225
runFiles,
240226
interrupt,
241227
splitContents,

app/terminal/runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface RuntimeContext {
1212
ready: boolean;
1313
mutex: MutexInterface;
1414
runFiles: (filenames: string[]) => Promise<ReplOutput[]>;
15-
runCommand?: (command: string) => Promise<ReplOutput[]>; // For REPL command execution
15+
runCommand: (command: string) => Promise<ReplOutput[]>; // For REPL command execution
1616
checkSyntax?: (code: string) => Promise<SyntaxStatus>;
1717
interrupt?: () => void;
1818
splitContents?: (content: string) => ReplCommand[];
@@ -28,7 +28,7 @@ export interface RuntimeContext {
2828
*/
2929
export function useRuntime(language: string): RuntimeContext {
3030
const pyodide = usePyodide();
31-
const wandbox = useWandbox();
31+
const wandbox = useWandbox("C++");
3232

3333
switch (language) {
3434
case "python":

app/terminal/wandbox/wandbox.tsx

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,35 @@ import { MutexInterface } from "async-mutex";
1010

1111
type WandboxLang = "C++";
1212

13-
interface IWandboxContext extends RuntimeContext {
14-
// 表示用のコマンドライン文字列を取得 (language-specific version)
15-
getCommandlineStrWithLang: (lang: WandboxLang, filenames: string[]) => string;
13+
interface IWandboxContext {
14+
init: () => Promise<void>;
15+
initializing: boolean;
16+
ready: boolean;
17+
mutex: MutexInterface;
18+
runFilesWithLang: (lang: WandboxLang) => (filenames: string[]) => Promise<ReturnType<typeof cppRunFiles>>;
19+
getCommandlineStrWithLang: (lang: WandboxLang) => (filenames: string[]) => string;
1620
}
1721

1822
const WandboxContext = createContext<IWandboxContext>(null!);
19-
export function useWandbox() {
23+
24+
export function useWandbox(lang: WandboxLang = "C++"): RuntimeContext {
2025
const context = useContext(WandboxContext);
2126
if (!context) {
2227
throw new Error("useWandbox must be used within a WandboxProvider");
2328
}
24-
return context;
29+
30+
return {
31+
init: context.init,
32+
initializing: context.initializing,
33+
ready: context.ready,
34+
mutex: context.mutex,
35+
runFiles: context.runFilesWithLang(lang),
36+
runCommand: async () => {
37+
// Wandbox doesn't support REPL, so return error
38+
return [{ type: "error" as const, message: "REPL not supported for this language" }];
39+
},
40+
getCommandlineStr: context.getCommandlineStrWithLang(lang),
41+
};
2542
}
2643

2744
export function WandboxProvider({ children }: { children: ReactNode }) {
@@ -33,8 +50,9 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
3350

3451
const ready = !!compilerList;
3552

36-
const getCommandlineStr = useCallback(
37-
(lang: WandboxLang, filenames: string[]) => {
53+
// Curried function for language-specific commandline string generation
54+
const getCommandlineStrWithLang = useCallback(
55+
(lang: WandboxLang) => (filenames: string[]) => {
3856
if (compilerList) {
3957
switch (lang) {
4058
case "C++":
@@ -50,14 +68,9 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
5068
[compilerList]
5169
);
5270

53-
// Wrapper for RuntimeContext compatibility
54-
const getCommandlineStrSimple = useCallback(
55-
(filenames: string[]) => getCommandlineStr("C++", filenames),
56-
[getCommandlineStr]
57-
);
58-
71+
// Curried function for language-specific file execution
5972
const runFilesWithLang = useCallback(
60-
async (lang: WandboxLang, filenames: string[]) => {
73+
(lang: WandboxLang) => async (filenames: string[]) => {
6174
if (!compilerList) {
6275
return [
6376
{ type: "error" as const, message: "Wandbox is not ready yet." },
@@ -80,13 +93,6 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
8093
[compilerList, files]
8194
);
8295

83-
const runFiles = useCallback(
84-
async (filenames: string[]) => {
85-
return runFilesWithLang("C++", filenames);
86-
},
87-
[runFilesWithLang]
88-
);
89-
9096
const init = useCallback(async () => {
9197
// Wandbox doesn't need initialization
9298
}, []);
@@ -114,9 +120,8 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
114120
initializing: false,
115121
ready,
116122
mutex,
117-
runFiles,
118-
getCommandlineStr: getCommandlineStrSimple, // RuntimeContext compatible version
119-
getCommandlineStrWithLang: getCommandlineStr, // Language-specific version
123+
runFilesWithLang,
124+
getCommandlineStrWithLang,
120125
}}
121126
>
122127
{children}

0 commit comments

Comments
 (0)