2022-10-28 14:17:40 +00:00
|
|
|
import type { Plug } from "../../plugos/plug.ts";
|
|
|
|
import { SysCallMapping, System } from "../../plugos/system.ts";
|
2023-07-14 14:56:20 +00:00
|
|
|
import type { Client } from "../client.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { CommandDef } from "../hooks/command.ts";
|
2023-08-27 12:13:18 +00:00
|
|
|
import { proxySyscall } from "./util.ts";
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-10-28 14:17:40 +00:00
|
|
|
export function systemSyscalls(
|
2023-07-14 14:56:20 +00:00
|
|
|
editor: Client,
|
2022-10-28 14:17:40 +00:00
|
|
|
system: System<any>,
|
|
|
|
): SysCallMapping {
|
2023-08-27 12:13:18 +00:00
|
|
|
const api: SysCallMapping = {
|
2022-10-14 13:11:33 +00:00
|
|
|
"system.invokeFunction": (
|
2022-04-03 16:42:12 +00:00
|
|
|
ctx,
|
2023-07-14 10:15:10 +00:00
|
|
|
_env: string,
|
2022-04-03 16:42:12 +00:00
|
|
|
name: string,
|
|
|
|
...args: any[]
|
2023-08-27 12:13:18 +00:00
|
|
|
) => {
|
|
|
|
// For backwards compatibility
|
|
|
|
// TODO: Remove at some point
|
|
|
|
return api["system.invoke"](ctx, name, ...args);
|
|
|
|
},
|
|
|
|
"system.invoke": (
|
|
|
|
ctx,
|
|
|
|
name: string,
|
|
|
|
...args: any[]
|
2022-04-03 16:42:12 +00:00
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
if (!ctx.plug) {
|
|
|
|
throw Error("No plug associated with context");
|
|
|
|
}
|
2022-04-05 15:02:17 +00:00
|
|
|
|
2022-10-28 14:17:40 +00:00
|
|
|
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 12:13:18 +00: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
|
|
|
|
return proxySyscall(editor.remoteSpacePrimitives, name, args);
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
return plug.invoke(name, args);
|
2022-04-26 17:04:36 +00:00
|
|
|
},
|
2023-05-23 18:53:53 +00:00
|
|
|
"system.invokeCommand": (_ctx, name: string) => {
|
2022-07-11 07:08:22 +00:00
|
|
|
return editor.runCommandByName(name);
|
|
|
|
},
|
2022-10-14 13:11:33 +00:00
|
|
|
"system.listCommands": (): { [key: string]: CommandDef } => {
|
|
|
|
const allCommands: { [key: string]: CommandDef } = {};
|
2023-07-14 11:44:30 +00:00
|
|
|
for (const [cmd, def] of editor.system.commandHook.editorCommands) {
|
2022-09-06 12:36:06 +00:00
|
|
|
allCommands[cmd] = def.command;
|
|
|
|
}
|
|
|
|
return allCommands;
|
|
|
|
},
|
2022-10-14 13:11:33 +00:00
|
|
|
"system.reloadPlugs": () => {
|
2023-07-27 13:25:33 +00:00
|
|
|
return editor.loadPlugs();
|
2022-03-25 11:03:06 +00:00
|
|
|
},
|
2023-01-14 17:51:00 +00:00
|
|
|
"system.getEnv": () => {
|
|
|
|
return system.env;
|
|
|
|
},
|
2022-03-25 11:03:06 +00:00
|
|
|
};
|
2023-08-27 12:13:18 +00:00
|
|
|
return api;
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|