diff --git a/app/terminal/worker/jsEval.worker.ts b/app/terminal/worker/jsEval.worker.ts
index d266282..1377410 100644
--- a/app/terminal/worker/jsEval.worker.ts
+++ b/app/terminal/worker/jsEval.worker.ts
@@ -1,7 +1,8 @@
///
+import { expose } from "comlink";
import type { ReplOutput } from "../repl";
-import type { MessageType, WorkerRequest, WorkerResponse } from "./runtime";
+import type { WorkerCapabilities } from "./runtime";
import inspect from "object-inspect";
function format(...args: unknown[]): string {
@@ -29,12 +30,12 @@ self.console = {
},
};
-async function init({ id }: WorkerRequest["init"]) {
+async function init(/*_interruptBuffer?: Uint8Array*/): Promise<{
+ capabilities: WorkerCapabilities;
+}> {
// Initialize the worker and report capabilities
- self.postMessage({
- id,
- payload: { capabilities: { interrupt: "restart" } },
- } satisfies WorkerResponse["init"]);
+ // interruptBuffer is not used for JavaScript (restart-based interruption)
+ return { capabilities: { interrupt: "restart" } };
}
async function replLikeEval(code: string): Promise {
@@ -72,8 +73,10 @@ async function replLikeEval(code: string): Promise {
}
}
-async function runCode({ id, payload }: WorkerRequest["runCode"]) {
- const { code } = payload;
+async function runCode(code: string): Promise<{
+ output: ReplOutput[];
+ updatedFiles: Record;
+}> {
try {
const result = await replLikeEval(code);
jsOutput.push({
@@ -99,14 +102,13 @@ async function runCode({ id, payload }: WorkerRequest["runCode"]) {
const output = [...jsOutput];
jsOutput = []; // Clear output
- self.postMessage({
- id,
- payload: { output, updatedFiles: [] },
- } satisfies WorkerResponse["runCode"]);
+ return { output, updatedFiles: {} as Record };
}
-function runFile({ id, payload }: WorkerRequest["runFile"]) {
- const { name, files } = payload;
+function runFile(
+ name: string,
+ files: Record
+): { output: ReplOutput[]; updatedFiles: Record } {
// pyodide worker などと異なり、複数ファイルを読み込んでimportのようなことをするのには対応していません。
try {
self.eval(files[name]);
@@ -129,23 +131,17 @@ function runFile({ id, payload }: WorkerRequest["runFile"]) {
const output = [...jsOutput];
jsOutput = []; // Clear output
- self.postMessage({
- id,
- payload: { output, updatedFiles: [] },
- } satisfies WorkerResponse["runFile"]);
+ return { output, updatedFiles: {} as Record };
}
-async function checkSyntax({ id, payload }: WorkerRequest["checkSyntax"]) {
- const { code } = payload;
-
+async function checkSyntax(
+ code: string
+): Promise<{ status: "complete" | "incomplete" | "invalid" }> {
try {
// Try to create a Function to check syntax
// new Function(code); // <- not working
self.eval(`() => {${code}}`);
- self.postMessage({
- id,
- payload: { status: "complete" },
- } satisfies WorkerResponse["checkSyntax"]);
+ return { status: "complete" };
} catch (e) {
// Check if it's a syntax error or if more input is expected
if (e instanceof SyntaxError) {
@@ -154,28 +150,18 @@ async function checkSyntax({ id, payload }: WorkerRequest["checkSyntax"]) {
e.message.includes("Unexpected token '}'") ||
e.message.includes("Unexpected end of input")
) {
- self.postMessage({
- id,
- payload: { status: "incomplete" },
- } satisfies WorkerResponse["checkSyntax"]);
+ return { status: "incomplete" };
} else {
- self.postMessage({
- id,
- payload: { status: "invalid" },
- } satisfies WorkerResponse["checkSyntax"]);
+ return { status: "invalid" };
}
} else {
- self.postMessage({
- id,
- payload: { status: "invalid" },
- } satisfies WorkerResponse["checkSyntax"]);
+ return { status: "invalid" };
}
}
}
-async function restoreState({ id, payload }: WorkerRequest["restoreState"]) {
+async function restoreState(commands: string[]): Promise