Skip to content

Commit 1f9008d

Browse files
Copilotna-trium-144
andcommitted
Address code review feedback - improve type safety and remove redundancies
Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent fc89e86 commit 1f9008d

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

app/terminal/exec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { writeOutput } from "./repl";
1111
import { useState } from "react";
1212
import { useEmbedContext } from "./embedContext";
1313
import { useRuntime } from "./runtime";
14-
import { useWandbox } from "./wandbox/wandbox";
1514

1615
export type ExecLang = "python" | "cpp";
1716

@@ -37,7 +36,6 @@ export function ExecFile(props: ExecProps) {
3736
const sectionContext = useEmbedContext();
3837

3938
const runtime = useRuntime(props.language);
40-
const wandbox = useWandbox();
4139

4240
// 表示するコマンドライン文字列
4341
let commandline: string;
@@ -50,7 +48,9 @@ export function ExecFile(props: ExecProps) {
5048
if (!props.filenames || props.filenames.length === 0) {
5149
throw new Error("C++の実行には filenames プロパティが必要です");
5250
}
53-
commandline = wandbox.getCommandlineStr("C++", props.filenames);
51+
commandline = runtime.getCommandlineStr
52+
? runtime.getCommandlineStr(props.filenames)
53+
: `g++ ${props.filenames.join(" ")}`;
5454
} else {
5555
props.language satisfies never;
5656
commandline = `エラー: 非対応の言語 ${props.language}`;

app/terminal/python/pyodide.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { RuntimeContext } from "../runtime";
1616

1717
interface IPyodideContext extends RuntimeContext {
1818
runPython: (code: string) => Promise<ReplOutput[]>;
19+
runCommand: (command: string) => Promise<ReplOutput[]>; // Alias for runPython for consistency
1920
runFile: (name: string) => Promise<ReplOutput[]>;
2021
}
2122

@@ -231,6 +232,7 @@ export function PyodideProvider({ children }: { children: ReactNode }) {
231232
initializing,
232233
ready,
233234
runPython,
235+
runCommand: runPython, // Alias for consistency with RuntimeContext
234236
checkSyntax,
235237
mutex: mutex.current,
236238
runFile,

app/terminal/repl.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ export function ReplTerminal(props: ReplComponentProps) {
9494

9595
const sendCommand = useCallback(
9696
async (command: string) => {
97-
// Python の場合は runPython があればそれを使用、なければ runFiles
98-
if ("runPython" in runtime && typeof runtime.runPython === "function") {
99-
return runtime.runPython(command);
97+
// Use runCommand if available (for REPL), otherwise use runFiles
98+
if (runtime.runCommand) {
99+
return runtime.runCommand(command);
100100
}
101-
// それ以外の言語では runFiles を使用(ただし REPL として使用しないので通常呼ばれない)
101+
// Fallback for languages without REPL support
102102
return runtime.runFiles([command]);
103103
},
104104
[runtime]

app/terminal/runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ 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
1516
checkSyntax?: (code: string) => Promise<SyntaxStatus>;
1617
interrupt?: () => void;
1718
splitContents?: (content: string) => ReplCommand[];
19+
getCommandlineStr?: (filenames: string[]) => string; // For displaying command line
1820
// Language-specific properties
1921
prompt?: string;
2022
promptMore?: string;

app/terminal/wandbox/wandbox.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { MutexInterface } from "async-mutex";
1111
type WandboxLang = "C++";
1212

1313
interface IWandboxContext extends RuntimeContext {
14-
// 表示用のコマンドライン文字列を取得
15-
getCommandlineStr: (lang: WandboxLang, filenames: string[]) => string;
14+
// 表示用のコマンドライン文字列を取得 (language-specific version)
15+
getCommandlineStrWithLang: (lang: WandboxLang, filenames: string[]) => string;
1616
}
1717

1818
const WandboxContext = createContext<IWandboxContext>(null!);
@@ -50,6 +50,12 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
5050
[compilerList]
5151
);
5252

53+
// Wrapper for RuntimeContext compatibility
54+
const getCommandlineStrSimple = useCallback(
55+
(filenames: string[]) => getCommandlineStr("C++", filenames),
56+
[getCommandlineStr]
57+
);
58+
5359
const runFilesWithLang = useCallback(
5460
async (lang: WandboxLang, filenames: string[]) => {
5561
if (!compilerList) {
@@ -85,11 +91,18 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
8591
// Wandbox doesn't need initialization
8692
}, []);
8793

88-
// Create a simple mutex that just executes the function
94+
// Create a simple mutex that just executes the function immediately
95+
// Wandbox doesn't need mutex locking as it makes synchronous API calls
8996
const mutex: MutexInterface = {
90-
runExclusive: async <T,>(fn: () => Promise<T>) => fn(),
91-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
92-
} as any;
97+
runExclusive: async <T,>(fn: () => Promise<T> | T) => fn(),
98+
acquire: async () => {
99+
return () => {}; // Release function (no-op)
100+
},
101+
waitForUnlock: async () => {},
102+
isLocked: () => false,
103+
cancel: () => {},
104+
release: () => {},
105+
};
93106

94107
return (
95108
<WandboxContext.Provider
@@ -99,7 +112,8 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
99112
ready,
100113
mutex,
101114
runFiles,
102-
getCommandlineStr,
115+
getCommandlineStr: getCommandlineStrSimple, // RuntimeContext compatible version
116+
getCommandlineStrWithLang: getCommandlineStr, // Language-specific version
103117
}}
104118
>
105119
{children}

0 commit comments

Comments
 (0)