Skip to content

Commit 6eab2b7

Browse files
committed
Add a timeout for Lua code execution on playground
1 parent dc4b764 commit 6eab2b7

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/pages/play/Playground.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ import styles from "./styles.module.scss";
1717
type LuaMessage = import("./fengari.worker").LuaMessage;
1818
type CustomTypeScriptWorker = import("./ts.worker").CustomTypeScriptWorker;
1919

20-
const fengariWorker = new FengariWorker();
20+
let fengariWorker = new FengariWorker();
2121
async function executeLua(code: string) {
2222
return new Promise<LuaMessage[]>(resolve => {
23+
const timeout = setTimeout(() => {
24+
resolve([{ type: "print", text: "Lua code execution timed out" }]);
25+
fengariWorker.terminate();
26+
fengariWorker = new FengariWorker();
27+
}, 2500);
28+
2329
fengariWorker.postMessage({ code });
2430
fengariWorker.addEventListener("message", event => {
31+
clearTimeout(timeout);
2532
resolve(event.data.messages);
2633
});
2734
});
@@ -46,10 +53,10 @@ function EditorContextProvider({ children }: { children: React.ReactNode }) {
4653
const getWorker = await monaco.languages.typescript.getTypeScriptWorker();
4754
const client = (await getWorker(model.uri)) as CustomTypeScriptWorker;
4855
const { lua, ast, sourceMap } = await client.getTranspileOutput(model.uri.toString());
49-
5056
const source = model.getValue();
51-
const results = await executeLua(lua);
5257

58+
setState({ source, lua, ast, sourceMap, results: [] });
59+
const results = await executeLua(lua);
5360
setState({ source, lua, ast, sourceMap, results });
5461
}, []);
5562

0 commit comments

Comments
 (0)