2024-10-11 21:34:27 +08:00
|
|
|
import { luaBuildStandardEnv } from "$common/space_lua/stdlib.ts";
|
|
|
|
import { parsePageRef } from "@silverbulletmd/silverbullet/lib/page_ref";
|
|
|
|
import {
|
|
|
|
LuaEnv,
|
|
|
|
LuaNativeJSFunction,
|
2024-10-20 21:06:23 +08:00
|
|
|
LuaStackFrame,
|
2024-10-11 21:34:27 +08:00
|
|
|
LuaTable,
|
|
|
|
} from "$common/space_lua/runtime.ts";
|
|
|
|
import type { System } from "$lib/plugos/system.ts";
|
|
|
|
|
2025-01-19 21:32:11 +08:00
|
|
|
export function buildLuaEnv(system: System<any>) {
|
2024-10-11 21:34:27 +08:00
|
|
|
const env = new LuaEnv(luaBuildStandardEnv());
|
|
|
|
|
2024-10-13 21:14:22 +08:00
|
|
|
// Expose all syscalls to Lua
|
|
|
|
exposeSyscalls(env, system);
|
|
|
|
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
function exposeSyscalls(env: LuaEnv, system: System<any>) {
|
2024-10-11 21:34:27 +08:00
|
|
|
// Expose all syscalls to Lua
|
2025-01-15 03:26:47 +08:00
|
|
|
// Except...
|
2025-01-19 20:02:01 +08:00
|
|
|
const blacklist = ["template", "shell"];
|
2024-10-20 21:06:23 +08:00
|
|
|
const nativeFs = new LuaStackFrame(env, null);
|
2024-10-11 21:34:27 +08:00
|
|
|
for (const syscallName of system.registeredSyscalls.keys()) {
|
2025-01-19 20:02:01 +08:00
|
|
|
if (blacklist.includes(syscallName)) {
|
2025-01-15 03:26:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
2024-10-11 21:34:27 +08:00
|
|
|
const [ns, fn] = syscallName.split(".");
|
2024-10-20 21:06:23 +08:00
|
|
|
if (!env.has(ns)) {
|
|
|
|
env.set(ns, new LuaTable(), nativeFs);
|
2024-10-11 21:34:27 +08:00
|
|
|
}
|
|
|
|
const luaFn = new LuaNativeJSFunction((...args) => {
|
|
|
|
return system.localSyscall(syscallName, args);
|
|
|
|
});
|
|
|
|
// Register the function with the same name as the syscall both in regular and snake_case
|
2024-10-20 21:06:23 +08:00
|
|
|
env.get(ns, nativeFs).set(fn, luaFn, nativeFs);
|
|
|
|
env.get(ns, nativeFs).set(snakeCase(fn), luaFn, nativeFs);
|
2024-10-11 21:34:27 +08:00
|
|
|
}
|
2024-10-13 21:14:22 +08:00
|
|
|
}
|
2024-10-11 21:34:27 +08:00
|
|
|
|
2025-01-19 21:32:11 +08:00
|
|
|
export function buildThreadLocalEnv(_system: System<any>, globalEnv: LuaEnv) {
|
2025-01-09 00:09:09 +08:00
|
|
|
const tl = new LuaEnv();
|
2025-01-18 01:32:13 +08:00
|
|
|
// const currentPageMeta = await system.localSyscall(
|
|
|
|
// "editor.getCurrentPageMeta",
|
|
|
|
// [],
|
|
|
|
// );
|
|
|
|
// tl.setLocal("pageMeta", currentPageMeta);
|
2025-01-09 18:45:15 +08:00
|
|
|
tl.setLocal("_GLOBAL", globalEnv);
|
2025-01-18 01:32:13 +08:00
|
|
|
return Promise.resolve(tl);
|
2025-01-09 00:09:09 +08:00
|
|
|
}
|
|
|
|
|
2025-01-19 21:32:11 +08:00
|
|
|
export async function handleLuaError(e: any, system: System<any>) {
|
2024-10-13 21:14:22 +08:00
|
|
|
console.error(
|
|
|
|
"Lua eval exception",
|
|
|
|
e.message,
|
|
|
|
e.context,
|
|
|
|
);
|
|
|
|
if (e.context && e.context.ref) {
|
|
|
|
// We got an error and actually know where it came from, let's navigate there to help debugging
|
|
|
|
const pageRef = parsePageRef(e.context.ref);
|
|
|
|
await system.localSyscall(
|
|
|
|
"editor.flashNotification",
|
|
|
|
[
|
|
|
|
`Lua error: ${e.message}`,
|
|
|
|
"error",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
await system.localSyscall(
|
|
|
|
"editor.flashNotification",
|
|
|
|
[
|
|
|
|
`Navigating to the place in the code where this error occurred in ${pageRef.page}`,
|
|
|
|
"info",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
await system.localSyscall("editor.navigate", [
|
|
|
|
{
|
|
|
|
page: pageRef.page,
|
|
|
|
pos: pageRef.pos + e.context.from +
|
|
|
|
"```space-lua\n".length,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
2024-10-11 21:34:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function snakeCase(s: string) {
|
|
|
|
return s.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
|
|
}
|