This commit is contained in:
2025-10-29 20:36:09 -07:00
commit d90a9b5826
33 changed files with 3239 additions and 0 deletions

53
webui/src/lib/runner.ts Normal file
View File

@@ -0,0 +1,53 @@
"use client";
let worker: Worker | null = null;
export async function runScript(
script: string,
appendOutput: (line: string) => void,
appendTerminal?: (line: string) => void
): Promise<void> {
return new Promise((resolve, reject) => {
if (worker) {
worker.terminate();
}
worker = new Worker(new URL('./script-runner.worker.ts', import.meta.url));
worker.onmessage = (event) => {
const { type, line, error } = event.data;
if (type === 'output') {
appendOutput(line);
} else if (type === 'terminal') {
appendTerminal?.(line);
} else if (type === 'complete') {
worker?.terminate();
worker = null;
resolve();
} else if (type === 'error') {
worker?.terminate();
worker = null;
reject(new Error(error));
} else if (type === 'stopped') {
worker?.terminate();
worker = null;
resolve();
}
};
worker.onerror = (error) => {
worker?.terminate();
worker = null;
reject(error);
};
worker.postMessage({ type: 'run', script });
});
}
export function stopScript(): void {
if (worker) {
worker.postMessage({ type: 'stop' });
}
}

View File

@@ -0,0 +1,73 @@
import init, { GameState } from "./wasm/script_runner";
let wasmReady = false;
let wasmInitPromise: Promise<void> | null = null;
let currentGame: GameState | null = null;
async function initWasm(): Promise<void> {
if (wasmReady) return;
if (wasmInitPromise) {
return wasmInitPromise;
}
wasmInitPromise = (async () => {
await init();
wasmReady = true;
})();
return wasmInitPromise;
}
self.onmessage = async (event) => {
const { type, script } = event.data;
if (type === "run") {
try {
await initWasm();
const appendOutput = (line: string) => {
self.postMessage({ type: "output", line });
};
const appendDebug = (line: string) => {
self.postMessage({ type: "output", line }); // debug also goes to output
};
const appendTerminal = (line: string) => {
self.postMessage({ type: "terminal", line });
};
currentGame = new GameState(
script,
"max",
appendOutput,
appendDebug,
script,
"min",
appendOutput,
appendDebug,
appendTerminal
);
while (currentGame && !currentGame.is_done()) {
const res = currentGame.step();
if (res === undefined) {
self.postMessage({ type: "complete" });
return;
}
}
if (currentGame) {
self.postMessage({ type: "complete" });
}
} catch (error) {
self.postMessage({ type: "error", error: String(error) });
}
} else if (type === "stop") {
currentGame = null;
self.postMessage({ type: "stopped" });
}
};