runtime: allow more files

This commit is contained in:
sfja 2025-09-23 17:35:09 +02:00
parent ea38234ae2
commit 67cd456762
3 changed files with 253 additions and 254 deletions

View File

@ -27,9 +27,7 @@
(fn generate () (do (fn generate () (do
(emit "#!/usr/bin/env node\n") (emit "#!/usr/bin/env node\n")
(emit "import { Runtime } from \"./runtime.js\";\n") (emit "import { Runtime } from \"./runtime.js\";\n")
(emit "const runtime = new Runtime({ filename: \"") (emit (format "const runtime = new Runtime(\"%\");\n" filename))
(emit filename)
(emit "\" });\n")
(for (ident builtin_id) builtin_syms (do (for (ident builtin_id) builtin_syms (do
(define_builtin ident builtin_id) (define_builtin ident builtin_id)

View File

@ -2,19 +2,22 @@ import fs from "node:fs";
import process from "node:process"; import process from "node:process";
export class Runtime { export class Runtime {
constructor({ filename, args } = {}) { constructor(filename) {
this.callStack = [{ name: "<entry>", line: 0 }]; this.callStack = [{ name: "<entry>", filename, line: 0 }];
this.filename = filename; this.currentFilename = filename;
this.args = args ?? process.argv.slice(2);
this.currentLine = 0; this.currentLine = 0;
} }
setFile(filename) {
this.currentFilename = filename;
}
setLine(line) { setLine(line) {
this.currentLine = line; this.currentLine = line;
} }
pushCall(name, line = this.currentLine) { pushCall(name, filename = this.filename, line = this.currentLine) {
this.callStack.push({ name, line }); this.callStack.push({ name, filename, line });
} }
popCall() { popCall() {
@ -35,13 +38,13 @@ export class Runtime {
const reversedStack = this.callStack.toReversed(); const reversedStack = this.callStack.toReversed();
const last = reversedStack[0]; const last = reversedStack[0];
console.error( console.error(
` \x1b[90mat \x1b[37m${last.name} \x1b[90m(${this.filename}:${this.currentLine})\x1b[0m`, ` \x1b[90mat \x1b[37m${last.name} \x1b[90m(${this.currentFilename}:${this.currentLine})\x1b[0m`,
); );
for (let i = 0; i < reversedStack.length - 1 && i < 20; ++i) { for (let i = 0; i < reversedStack.length - 1 && i < 20; ++i) {
const name = reversedStack[i + 1].name; const name = reversedStack[i + 1].name;
const line = reversedStack[i].line; const { filename, line } = reversedStack[i];
console.error( console.error(
` \x1b[90mat \x1b[37m${name} \x1b[90m(${this.filename}:${line})\x1b[0m`, ` \x1b[90mat \x1b[37m${name} \x1b[90m(${filename}:${line})\x1b[0m`,
); );
} }
} }
@ -219,7 +222,7 @@ export class Runtime {
builtinGetArgs() { builtinGetArgs() {
return { return {
type: "list", type: "list",
values: this.args values: process.argv.slice(2)
.map((value) => ({ type: "string", value })), .map((value) => ({ type: "string", value })),
}; };
} }

480
stage2.js

File diff suppressed because it is too large Load Diff