2024-11-20 14:49:59 +00:00
|
|
|
import { Application, Router } from "jsr:@oak/oak";
|
2024-11-21 11:08:07 +00:00
|
|
|
import { parseArgs } from "jsr:@std/cli/parse-args";
|
|
|
|
import { Runtime } from "./runtime.ts";
|
|
|
|
import * as compiler from "../compiler/mod.ts";
|
2024-11-20 14:49:59 +00:00
|
|
|
|
2024-11-21 11:08:07 +00:00
|
|
|
const port = 8000;
|
2024-11-21 03:12:07 +00:00
|
|
|
|
2024-11-21 11:08:07 +00:00
|
|
|
const flags = parseArgs(Deno.args, {
|
|
|
|
boolean: ["flame-graph", "code-coverage"],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (flags._.length !== 1) {
|
|
|
|
throw new Error("please specify a filename");
|
|
|
|
}
|
|
|
|
|
2024-12-14 23:07:36 +00:00
|
|
|
//
|
|
|
|
|
2024-11-21 11:08:07 +00:00
|
|
|
const filepath = flags._[0] as string;
|
|
|
|
const text = await Deno.readTextFile(filepath);
|
|
|
|
|
|
|
|
const runtime = new Runtime(13370);
|
|
|
|
|
2024-11-26 13:32:21 +00:00
|
|
|
async function runProgramWithDebug(program: number[]) {
|
2024-11-21 11:08:07 +00:00
|
|
|
const connection = await runtime.connect();
|
|
|
|
connection.send({
|
|
|
|
type: "run-debug",
|
|
|
|
program,
|
|
|
|
});
|
|
|
|
const res = await connection.receive<{
|
|
|
|
ok: boolean;
|
|
|
|
}>();
|
|
|
|
connection.close();
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error("could not run code");
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 14:49:59 +00:00
|
|
|
|
2024-12-14 23:07:36 +00:00
|
|
|
const { program, fnNames } = await compiler.compileWithDebug(filepath);
|
|
|
|
await runProgramWithDebug(program);
|
|
|
|
|
|
|
|
//
|
2024-12-12 09:46:14 +00:00
|
|
|
|
2024-11-20 14:49:59 +00:00
|
|
|
const router = new Router();
|
|
|
|
|
2024-11-21 11:08:07 +00:00
|
|
|
router.get("/api/source", (ctx) => {
|
|
|
|
ctx.response.body = { ok: true, filepath, text };
|
|
|
|
ctx.response.status = 200;
|
|
|
|
ctx.respond = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/api/status", async (ctx) => {
|
|
|
|
const connection = await runtime.connect();
|
|
|
|
connection.send({ type: "status" });
|
|
|
|
const res = await connection.receive<{
|
|
|
|
ok: boolean;
|
2024-12-15 02:07:33 +00:00
|
|
|
status: { running: boolean };
|
2024-11-21 11:08:07 +00:00
|
|
|
}>();
|
|
|
|
connection.close();
|
|
|
|
if (!res.ok) {
|
|
|
|
ctx.response.body = { ok: false };
|
|
|
|
ctx.response.status = 500;
|
|
|
|
ctx.respond = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx.response.body = { ok: true, status: res.status };
|
|
|
|
ctx.response.status = 200;
|
|
|
|
ctx.respond = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/api/flame-graph", async (ctx) => {
|
|
|
|
const connection = await runtime.connect();
|
|
|
|
connection.send({ type: "flame-graph" });
|
|
|
|
const res = await connection.receive<{
|
|
|
|
ok: boolean;
|
|
|
|
flameGraph: string;
|
|
|
|
}>();
|
|
|
|
connection.close();
|
|
|
|
if (!res.ok) {
|
|
|
|
ctx.response.body = { ok: false };
|
|
|
|
ctx.response.status = 500;
|
|
|
|
ctx.respond = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx.response.body = { ok: true, flameGraph: res.flameGraph };
|
|
|
|
ctx.response.status = 200;
|
|
|
|
ctx.respond = true;
|
|
|
|
});
|
|
|
|
|
2024-12-14 23:07:36 +00:00
|
|
|
router.get("/api/flame-graph-fn-names", (ctx) => {
|
|
|
|
ctx.response.body = { ok: true, fnNames };
|
|
|
|
ctx.response.status = 200;
|
|
|
|
ctx.respond = true;
|
|
|
|
});
|
|
|
|
|
2024-11-21 11:08:07 +00:00
|
|
|
router.get("/api/code-coverage", async (ctx) => {
|
|
|
|
const connection = await runtime.connect();
|
|
|
|
connection.send({ type: "code-coverage" });
|
|
|
|
const res = await connection.receive<{
|
|
|
|
ok: boolean;
|
|
|
|
codeCoverage: string;
|
|
|
|
}>();
|
|
|
|
connection.close();
|
|
|
|
if (!res.ok) {
|
|
|
|
ctx.response.body = { ok: false };
|
|
|
|
ctx.response.status = 500;
|
|
|
|
ctx.respond = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx.response.body = { ok: true, codeCoverage: res.codeCoverage };
|
|
|
|
ctx.response.status = 200;
|
|
|
|
ctx.respond = true;
|
|
|
|
});
|
|
|
|
|
2024-12-14 23:07:36 +00:00
|
|
|
//
|
|
|
|
|
2024-11-21 11:08:07 +00:00
|
|
|
const app = new Application();
|
2024-11-20 14:49:59 +00:00
|
|
|
app.use(router.routes());
|
|
|
|
app.use(router.allowedMethods());
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await ctx.send({ root: "./public", index: "index.html" });
|
|
|
|
} catch {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const listener = app.listen({ port });
|
2024-11-21 11:08:07 +00:00
|
|
|
console.log(`Devtools at http://localhost:${port}/`);
|
2024-11-20 14:49:59 +00:00
|
|
|
await listener;
|