2024-02-06 23:51:04 +08:00
|
|
|
import { SyscallMeta } from "$sb/types.ts";
|
2022-10-28 22:17:40 +08:00
|
|
|
import { SysCallMapping, System } from "../../plugos/system.ts";
|
2024-02-06 23:51:04 +08:00
|
|
|
import type { ServerSystem } from "../../server/server_system.ts";
|
2024-01-27 00:05:10 +08:00
|
|
|
import type { Client } from "../../web/client.ts";
|
|
|
|
import { CommandDef } from "../../web/hooks/command.ts";
|
|
|
|
import { proxySyscall } from "../../web/syscalls/util.ts";
|
2022-03-25 19:03:06 +08:00
|
|
|
|
2022-10-28 22:17:40 +08:00
|
|
|
export function systemSyscalls(
|
|
|
|
system: System<any>,
|
2024-01-27 00:05:10 +08:00
|
|
|
readOnlyMode: boolean,
|
2024-02-06 23:51:04 +08:00
|
|
|
client: Client | undefined,
|
|
|
|
serverSystem: ServerSystem | undefined,
|
2022-10-28 22:17:40 +08:00
|
|
|
): 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,
|
2024-01-15 23:43:12 +08:00
|
|
|
fullName: string, // plug.function
|
2023-08-27 20:13:18 +08:00
|
|
|
...args: any[]
|
2022-04-04 00:42:12 +08:00
|
|
|
) => {
|
2024-01-15 23:43:12 +08:00
|
|
|
const [plugName, functionName] = fullName.split(".");
|
|
|
|
if (!plugName || !functionName) {
|
|
|
|
throw Error(`Invalid function name ${fullName}`);
|
2023-08-28 23:12:15 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
const plug = system.loadedPlugs.get(plugName);
|
|
|
|
if (!plug) {
|
|
|
|
throw Error(`Plug ${plugName} not found`);
|
2022-10-28 22:17:40 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
const functionDef = plug.manifest!.functions[functionName];
|
2023-08-27 20:13:18 +08:00
|
|
|
if (!functionDef) {
|
2024-01-15 23:43:12 +08:00
|
|
|
throw Error(`Function ${functionName} not found`);
|
2023-08-27 20:13:18 +08:00
|
|
|
}
|
2023-08-30 03:17:29 +08:00
|
|
|
if (
|
|
|
|
client && functionDef.env && system.env &&
|
|
|
|
functionDef.env !== system.env
|
|
|
|
) {
|
2023-08-27 20:13:18 +08:00
|
|
|
// Proxy to another environment
|
2023-10-03 20:16:33 +08:00
|
|
|
return proxySyscall(
|
|
|
|
ctx,
|
2023-12-17 18:46:18 +08:00
|
|
|
client.httpSpacePrimitives,
|
2023-10-03 20:16:33 +08:00
|
|
|
"system.invokeFunction",
|
|
|
|
[fullName, ...args],
|
|
|
|
);
|
2023-08-27 20:13:18 +08:00
|
|
|
}
|
2024-01-15 23:43:12 +08:00
|
|
|
return plug.invoke(functionName, args);
|
2022-04-27 01:04:36 +08:00
|
|
|
},
|
2023-11-26 01:57:00 +08:00
|
|
|
"system.invokeCommand": (_ctx, name: string, args?: string[]) => {
|
2023-08-30 03:17:29 +08:00
|
|
|
if (!client) {
|
|
|
|
throw new Error("Not supported");
|
|
|
|
}
|
2023-11-26 01:57:00 +08:00
|
|
|
return client.runCommandByName(name, args);
|
2022-07-11 15:08:22 +08:00
|
|
|
},
|
2022-10-14 21:11:33 +08:00
|
|
|
"system.listCommands": (): { [key: string]: CommandDef } => {
|
2024-02-06 23:51:04 +08:00
|
|
|
const commandHook = client?.system.commandHook ||
|
|
|
|
serverSystem!.commandHook;
|
2022-10-14 21:11:33 +08:00
|
|
|
const allCommands: { [key: string]: CommandDef } = {};
|
2024-02-06 23:51:04 +08:00
|
|
|
for (const [cmd, def] of commandHook.editorCommands) {
|
2022-09-06 20:36:06 +08:00
|
|
|
allCommands[cmd] = def.command;
|
|
|
|
}
|
|
|
|
return allCommands;
|
|
|
|
},
|
2024-02-06 23:51:04 +08:00
|
|
|
"system.listSyscalls": (): SyscallMeta[] => {
|
|
|
|
const syscalls: SyscallMeta[] = [];
|
|
|
|
for (const [name, info] of system.registeredSyscalls) {
|
|
|
|
syscalls.push({
|
|
|
|
name,
|
|
|
|
requiredPermissions: info.requiredPermissions,
|
|
|
|
argCount: Math.max(0, info.callback.length - 1),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return syscalls;
|
|
|
|
},
|
2022-10-14 21:11:33 +08:00
|
|
|
"system.reloadPlugs": () => {
|
2023-08-30 03:17:29 +08:00
|
|
|
if (!client) {
|
|
|
|
throw new Error("Not supported");
|
|
|
|
}
|
|
|
|
return client.loadPlugs();
|
2022-03-25 19:03:06 +08:00
|
|
|
},
|
2024-02-06 23:51:04 +08:00
|
|
|
"system.loadSpaceScripts": async () => {
|
|
|
|
if (client) {
|
|
|
|
// If this is invoked on the client, we need to load the space scripts locally
|
|
|
|
await client.loadSpaceScripts();
|
|
|
|
// And we are in a hybrud mode, tell the server to do the same
|
|
|
|
if (system.env === "client") {
|
|
|
|
console.info(
|
|
|
|
"Sending syscall to server to trigger space script reload",
|
|
|
|
);
|
|
|
|
await proxySyscall(
|
|
|
|
{},
|
|
|
|
client.httpSpacePrimitives,
|
|
|
|
"system.loadSpaceScripts",
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (serverSystem) {
|
|
|
|
return serverSystem.loadSpaceScripts();
|
|
|
|
} else {
|
|
|
|
throw new Error("Load space scripts in an undefined environment");
|
|
|
|
}
|
|
|
|
},
|
2023-01-15 01:51:00 +08:00
|
|
|
"system.getEnv": () => {
|
|
|
|
return system.env;
|
|
|
|
},
|
2024-01-27 00:05:10 +08:00
|
|
|
"system.getMode": () => {
|
|
|
|
return readOnlyMode ? "ro" : "rw";
|
|
|
|
},
|
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
|
|
|
}
|