-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime.tsx
More file actions
127 lines (122 loc) · 3.33 KB
/
runtime.tsx
File metadata and controls
127 lines (122 loc) · 3.33 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
import { MutexInterface } from "async-mutex";
import { ReplOutput, SyntaxStatus, ReplCommand } from "./repl";
import { PyodideProvider, usePyodide } from "./python/runtime";
import { useWandbox, WandboxProvider } from "./wandbox/runtime";
import { JavaScriptProvider, useJavaScript } from "./javascript/runtime";
import { AceLang } from "./editor";
import { ReactNode } from "react";
/**
* Common runtime context interface for different languages
*
* see README.md for details
*
*/
export interface RuntimeContext {
ready: boolean;
mutex?: MutexInterface;
interrupt?: () => void;
// repl
runCommand?: (command: string) => Promise<ReplOutput[]>;
checkSyntax?: (code: string) => Promise<SyntaxStatus>;
splitReplExamples?: (content: string) => ReplCommand[];
// file
runFiles: (filenames: string[]) => Promise<ReplOutput[]>;
getCommandlineStr: (filenames: string[]) => string;
}
export interface LangConstants {
tabSize: number;
prompt?: string;
promptMore?: string;
}
export type RuntimeLang = "python" | "cpp" | "javascript";
export function getRuntimeLang(
lang: string | undefined
): RuntimeLang | undefined {
// markdownで指定される可能性のある言語名からRuntimeLangを取得
switch (lang) {
case "python":
case "py":
return "python";
case "cpp":
case "c++":
return "cpp";
case "javascript":
case "js":
return "javascript";
default:
console.warn(`Unsupported language for runtime: ${lang}`);
return undefined;
}
}
export function useRuntime(language: RuntimeLang): RuntimeContext {
// すべての言語のcontextをインスタンス化
const pyodide = usePyodide();
const wandboxCpp = useWandbox("cpp");
const javascript = useJavaScript();
switch (language) {
case "python":
return pyodide;
case "cpp":
return wandboxCpp;
case "javascript":
return javascript;
default:
language satisfies never;
throw new Error(`Runtime not implemented for language: ${language}`);
}
}
export function RuntimeProvider({ children }: { children: ReactNode }) {
return (
<PyodideProvider>
<WandboxProvider>
<JavaScriptProvider>{children}</JavaScriptProvider>
</WandboxProvider>
</PyodideProvider>
);
}
export function langConstants(lang: RuntimeLang | AceLang): LangConstants {
switch (lang) {
case "python":
return {
tabSize: 4,
prompt: ">>> ",
promptMore: "... ",
};
case "javascript":
return {
tabSize: 2,
prompt: "> ",
};
case "c_cpp":
case "cpp":
return {
tabSize: 2,
};
case "json":
return {
tabSize: 2,
};
case "csv":
case "text":
return {
// tabは使わないが、0は指定できないようなので適当にデフォルト値
tabSize: 4,
};
default:
lang satisfies never;
throw new Error(`LangConstants not defined for language: ${lang}`);
}
}
export const emptyMutex: MutexInterface = {
async runExclusive<T>(fn: () => Promise<T> | T) {
const result = fn();
return result instanceof Promise ? result : Promise.resolve(result);
},
acquire: async () => {
return () => {}; // Release function (no-op)
},
waitForUnlock: async () => {},
isLocked: () => false,
cancel: () => {},
release: () => {},
};