silverbullet/web/syscalls/system.ts

66 lines
2.0 KiB
TypeScript
Raw Normal View History

import type { Plug } from "../../plugos/plug.ts";
import { SysCallMapping, System } from "../../plugos/system.ts";
2023-07-14 22:56:20 +08:00
import type { Client } from "../client.ts";
import { CommandDef } from "../hooks/command.ts";
2023-08-27 20:13:18 +08:00
import { proxySyscall } from "./util.ts";
2022-03-25 19:03:06 +08:00
export function systemSyscalls(
2023-07-14 22:56:20 +08:00
editor: Client,
system: System<any>,
): SysCallMapping {
2023-08-27 20:13:18 +08:00
const api: SysCallMapping = {
2022-10-14 21:11:33 +08:00
"system.invokeFunction": (
2023-08-27 20:13:18 +08:00
ctx,
name: string,
...args: any[]
) => {
2022-03-25 19:03:06 +08:00
if (!ctx.plug) {
throw Error("No plug associated with context");
}
2022-04-05 23:02:17 +08:00
2023-08-28 23:12:15 +08:00
if (name === "server" || name === "client") {
// Backwards compatibility mode (previously there was an 'env' argument)
name = args[0];
args = args.slice(1);
}
let plug: Plug<any> | undefined = ctx.plug;
if (name.indexOf(".") !== -1) {
// plug name in the name
const [plugName, functionName] = name.split(".");
plug = system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
name = functionName;
}
2023-08-27 20:13:18 +08:00
const functionDef = plug.manifest!.functions[name];
if (!functionDef) {
throw Error(`Function ${name} not found`);
}
if (functionDef.env && system.env && functionDef.env !== system.env) {
// Proxy to another environment
2023-08-28 23:12:15 +08:00
return proxySyscall(ctx, editor.remoteSpacePrimitives, name, args);
2023-08-27 20:13:18 +08:00
}
return plug.invoke(name, args);
2022-04-27 01:04:36 +08:00
},
"system.invokeCommand": (_ctx, name: string) => {
2022-07-11 15:08:22 +08:00
return editor.runCommandByName(name);
},
2022-10-14 21:11:33 +08:00
"system.listCommands": (): { [key: string]: CommandDef } => {
const allCommands: { [key: string]: CommandDef } = {};
2023-07-14 19:44:30 +08:00
for (const [cmd, def] of editor.system.commandHook.editorCommands) {
2022-09-06 20:36:06 +08:00
allCommands[cmd] = def.command;
}
return allCommands;
},
2022-10-14 21:11:33 +08:00
"system.reloadPlugs": () => {
2023-07-27 21:25:33 +08:00
return editor.loadPlugs();
2022-03-25 19:03:06 +08:00
},
2023-01-15 01:51:00 +08:00
"system.getEnv": () => {
return system.env;
},
2022-03-25 19:03:06 +08:00
};
2023-08-27 20:13:18 +08:00
return api;
2022-03-25 19:03:06 +08:00
}