runtime: allow more files
This commit is contained in:
parent
ea38234ae2
commit
67cd456762
@ -27,9 +27,7 @@
|
||||
(fn generate () (do
|
||||
(emit "#!/usr/bin/env node\n")
|
||||
(emit "import { Runtime } from \"./runtime.js\";\n")
|
||||
(emit "const runtime = new Runtime({ filename: \"")
|
||||
(emit filename)
|
||||
(emit "\" });\n")
|
||||
(emit (format "const runtime = new Runtime(\"%\");\n" filename))
|
||||
|
||||
(for (ident builtin_id) builtin_syms (do
|
||||
(define_builtin ident builtin_id)
|
||||
|
23
runtime.js
23
runtime.js
@ -2,19 +2,22 @@ import fs from "node:fs";
|
||||
import process from "node:process";
|
||||
|
||||
export class Runtime {
|
||||
constructor({ filename, args } = {}) {
|
||||
this.callStack = [{ name: "<entry>", line: 0 }];
|
||||
this.filename = filename;
|
||||
this.args = args ?? process.argv.slice(2);
|
||||
constructor(filename) {
|
||||
this.callStack = [{ name: "<entry>", filename, line: 0 }];
|
||||
this.currentFilename = filename;
|
||||
this.currentLine = 0;
|
||||
}
|
||||
|
||||
setFile(filename) {
|
||||
this.currentFilename = filename;
|
||||
}
|
||||
|
||||
setLine(line) {
|
||||
this.currentLine = line;
|
||||
}
|
||||
|
||||
pushCall(name, line = this.currentLine) {
|
||||
this.callStack.push({ name, line });
|
||||
pushCall(name, filename = this.filename, line = this.currentLine) {
|
||||
this.callStack.push({ name, filename, line });
|
||||
}
|
||||
|
||||
popCall() {
|
||||
@ -35,13 +38,13 @@ export class Runtime {
|
||||
const reversedStack = this.callStack.toReversed();
|
||||
const last = reversedStack[0];
|
||||
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) {
|
||||
const name = reversedStack[i + 1].name;
|
||||
const line = reversedStack[i].line;
|
||||
const { filename, line } = reversedStack[i];
|
||||
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() {
|
||||
return {
|
||||
type: "list",
|
||||
values: this.args
|
||||
values: process.argv.slice(2)
|
||||
.map((value) => ({ type: "string", value })),
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user