add fs builtins

This commit is contained in:
sfja 2025-09-25 15:51:58 +02:00
parent 6b7bb78976
commit 7e42abb239
3 changed files with 786 additions and 762 deletions

View File

@ -33,6 +33,10 @@
(list "char_code" "builtinCharCode")
(list "strings_join" "builtinStringsJoin")
(list "get_args" "builtinGetArgs")
(list "fs_basename" "builtinFsBasename")
(list "fs_dirname" "builtinFsDirname")
(list "fs_cwd" "builtinFsCwd")
(list "fs_resolve" "builtinFsResolve")
))
(fn generate () (do

View File

@ -1,4 +1,5 @@
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
export class Runtime {
@ -165,13 +166,13 @@ export class Runtime {
return { type: "null" };
}
builtinReadTextFile(path) {
const text = fs.readFileSync(path.value).toString();
builtinReadTextFile(filename) {
const text = fs.readFileSync(filename.value).toString();
return { type: "string", value: text };
}
builtinWriteTextFile(path, text) {
fs.writeFileSync(path.value, text.value);
builtinWriteTextFile(filename, text) {
fs.writeFileSync(filename.value, text.value);
return { type: "null" };
}
@ -228,6 +229,25 @@ export class Runtime {
};
}
builtinFsBasename(filepath) {
return { type: "string", value: path.basename(filepath.value) };
}
builtinFsDirname(filepath) {
return { type: "string", value: path.dirname(filepath.value) };
}
builtinFsCwd() {
return { type: "string", value: process.cwd() };
}
builtinFsResolve(base, relative) {
return {
type: "string",
value: path.resolve(base.value, relative.value),
};
}
static valueToPrint(value) {
if (value.type === "null") {
return "null";

1516
stage2.js

File diff suppressed because it is too large Load Diff