silverbullet/common/syscalls/system.ts

74 lines
2.1 KiB
TypeScript

import { SysCallMapping, System } from "../../plugos/system.ts";
import type { Client } from "../../web/client.ts";
import { CommandDef } from "../../web/hooks/command.ts";
import { proxySyscall } from "../../web/syscalls/util.ts";
export function systemSyscalls(
system: System<any>,
readOnlyMode: boolean,
client?: Client,
): SysCallMapping {
const api: SysCallMapping = {
"system.invokeFunction": (
ctx,
fullName: string, // plug.function
...args: any[]
) => {
const [plugName, functionName] = fullName.split(".");
if (!plugName || !functionName) {
throw Error(`Invalid function name ${fullName}`);
}
const plug = system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
const functionDef = plug.manifest!.functions[functionName];
if (!functionDef) {
throw Error(`Function ${functionName} not found`);
}
if (
client && functionDef.env && system.env &&
functionDef.env !== system.env
) {
// Proxy to another environment
return proxySyscall(
ctx,
client.httpSpacePrimitives,
"system.invokeFunction",
[fullName, ...args],
);
}
return plug.invoke(functionName, args);
},
"system.invokeCommand": (_ctx, name: string, args?: string[]) => {
if (!client) {
throw new Error("Not supported");
}
return client.runCommandByName(name, args);
},
"system.listCommands": (): { [key: string]: CommandDef } => {
if (!client) {
throw new Error("Not supported");
}
const allCommands: { [key: string]: CommandDef } = {};
for (const [cmd, def] of client.system.commandHook.editorCommands) {
allCommands[cmd] = def.command;
}
return allCommands;
},
"system.reloadPlugs": () => {
if (!client) {
throw new Error("Not supported");
}
return client.loadPlugs();
},
"system.getEnv": () => {
return system.env;
},
"system.getMode": () => {
return readOnlyMode ? "ro" : "rw";
},
};
return api;
}