add fs builtins
This commit is contained in:
parent
6b7bb78976
commit
7e42abb239
@ -33,6 +33,10 @@
|
|||||||
(list "char_code" "builtinCharCode")
|
(list "char_code" "builtinCharCode")
|
||||||
(list "strings_join" "builtinStringsJoin")
|
(list "strings_join" "builtinStringsJoin")
|
||||||
(list "get_args" "builtinGetArgs")
|
(list "get_args" "builtinGetArgs")
|
||||||
|
(list "fs_basename" "builtinFsBasename")
|
||||||
|
(list "fs_dirname" "builtinFsDirname")
|
||||||
|
(list "fs_cwd" "builtinFsCwd")
|
||||||
|
(list "fs_resolve" "builtinFsResolve")
|
||||||
))
|
))
|
||||||
|
|
||||||
(fn generate () (do
|
(fn generate () (do
|
||||||
|
|||||||
28
runtime.js
28
runtime.js
@ -1,4 +1,5 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
import process from "node:process";
|
import process from "node:process";
|
||||||
|
|
||||||
export class Runtime {
|
export class Runtime {
|
||||||
@ -165,13 +166,13 @@ export class Runtime {
|
|||||||
return { type: "null" };
|
return { type: "null" };
|
||||||
}
|
}
|
||||||
|
|
||||||
builtinReadTextFile(path) {
|
builtinReadTextFile(filename) {
|
||||||
const text = fs.readFileSync(path.value).toString();
|
const text = fs.readFileSync(filename.value).toString();
|
||||||
return { type: "string", value: text };
|
return { type: "string", value: text };
|
||||||
}
|
}
|
||||||
|
|
||||||
builtinWriteTextFile(path, text) {
|
builtinWriteTextFile(filename, text) {
|
||||||
fs.writeFileSync(path.value, text.value);
|
fs.writeFileSync(filename.value, text.value);
|
||||||
return { type: "null" };
|
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) {
|
static valueToPrint(value) {
|
||||||
if (value.type === "null") {
|
if (value.type === "null") {
|
||||||
return "null";
|
return "null";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user