-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime.tsx
More file actions
162 lines (149 loc) · 4.15 KB
/
Copy pathruntime.tsx
File metadata and controls
162 lines (149 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"use client";
import {
createContext,
ReactNode,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
} from "react";
import useSWR from "swr";
import { compilerInfoFetcher, SelectedCompiler } from "./api";
import { cppRunFiles, selectCppCompiler } from "./cpp";
import { RuntimeLang } from "../languages";
import { rustRunFiles, selectRustCompiler } from "./rust";
import {
ReplOutput,
RuntimeContext,
RuntimeErrorHandler,
RuntimeInfo,
UpdatedFile,
} from "../interface";
type WandboxLang = "cpp" | "rust";
interface IWandboxContext {
init: (onError?: RuntimeErrorHandler) => void;
ready: boolean;
getCommandlineStrWithLang: (
lang: WandboxLang
) => (filenames: string[]) => string;
runFilesWithLang: (
lang: WandboxLang
) => (
filenames: string[],
files: Readonly<Record<string, string>>,
onOutput: (output: ReplOutput | UpdatedFile) => void
) => Promise<void>;
runtimeInfo: Record<WandboxLang, RuntimeInfo> | undefined,
}
const WandboxContext = createContext<IWandboxContext>(null!);
export function WandboxProvider({ children }: { children: ReactNode }) {
const onErrorRef = useRef<RuntimeErrorHandler | undefined>(undefined);
const init = useCallback((onError?: RuntimeErrorHandler) => {
onErrorRef.current = onError;
}, []);
const { data: compilerList, error } = useSWR("list", compilerInfoFetcher);
useEffect(() => {
if (error) {
console.error("Failed to fetch compiler list from Wandbox:", error);
onErrorRef.current?.(error);
}
}, [error]);
const ready = !!compilerList;
const selectedCompiler = useMemo<
Record<WandboxLang, SelectedCompiler> | undefined
>(() => {
if (!compilerList) {
return undefined;
}
return {
cpp: selectCppCompiler(compilerList),
rust: selectRustCompiler(compilerList),
};
}, [compilerList]);
const getCommandlineStrWithLang = useCallback(
(lang: WandboxLang) => {
if (selectedCompiler) {
return selectedCompiler[lang].getCommandlineStr;
} else {
return () => "";
}
},
[selectedCompiler]
);
// Curried function for language-specific file execution
const runFilesWithLang = useCallback(
(lang: WandboxLang) =>
async (
filenames: string[],
files: Readonly<Record<string, string>>,
onOutput: (output: ReplOutput | UpdatedFile) => void
) => {
if (!selectedCompiler) {
onOutput({ type: "error", message: "Wandbox is not ready yet." });
return;
}
try {
switch (lang) {
case "cpp":
await cppRunFiles(selectedCompiler.cpp, files, filenames, onOutput);
break;
case "rust":
await rustRunFiles(
selectedCompiler.rust,
files,
filenames,
onOutput
);
break;
default:
lang satisfies never;
throw new Error(`unsupported language: ${lang}`);
}
} catch (error) {
onErrorRef.current?.(error);
onOutput({
type: "fatalError",
message: error instanceof Error ? error.message : String(error),
});
}
},
[selectedCompiler]
);
return (
<WandboxContext.Provider
value={{
init,
ready,
getCommandlineStrWithLang,
runFilesWithLang,
runtimeInfo: selectedCompiler,
}}
>
{children}
</WandboxContext.Provider>
);
}
export function useWandbox(lang: WandboxLang): RuntimeContext {
lang satisfies RuntimeLang;
const context = useContext(WandboxContext);
if (!context) {
throw new Error("useWandbox must be used within a WandboxProvider");
}
const { runFilesWithLang, getCommandlineStrWithLang } = context;
const runFiles = useMemo(
() => runFilesWithLang(lang),
[lang, runFilesWithLang]
);
const getCommandlineStr = useMemo(
() => getCommandlineStrWithLang(lang),
[lang, getCommandlineStrWithLang]
);
return {
init: context.init,
ready: context.ready,
runFiles,
getCommandlineStr,
runtimeInfo: context.runtimeInfo?.[lang],
};
}