More event refactoring work

This commit is contained in:
Zef Hemel
2023-08-27 14:13:18 +02:00
parent 593597454a
commit c3d384330d
13 changed files with 140 additions and 149 deletions
+20 -1
View File
@@ -2,17 +2,27 @@ import type { Plug } from "../../plugos/plug.ts";
import { SysCallMapping, System } from "../../plugos/system.ts";
import type { Client } from "../client.ts";
import { CommandDef } from "../hooks/command.ts";
import { proxySyscall } from "./util.ts";
export function systemSyscalls(
editor: Client,
system: System<any>,
): SysCallMapping {
return {
const api: SysCallMapping = {
"system.invokeFunction": (
ctx,
_env: string,
name: string,
...args: any[]
) => {
// For backwards compatibility
// TODO: Remove at some point
return api["system.invoke"](ctx, name, ...args);
},
"system.invoke": (
ctx,
name: string,
...args: any[]
) => {
if (!ctx.plug) {
throw Error("No plug associated with context");
@@ -28,6 +38,14 @@ export function systemSyscalls(
}
name = functionName;
}
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);
}
return plug.invoke(name, args);
},
"system.invokeCommand": (_ctx, name: string) => {
@@ -47,4 +65,5 @@ export function systemSyscalls(
return system.env;
},
};
return api;
}
+28 -22
View File
@@ -1,3 +1,4 @@
import { HttpSpacePrimitives } from "../../common/spaces/http_space_primitives.ts";
import { SysCallMapping } from "../../plugos/system.ts";
import { SyscallResponse } from "../../server/rpc.ts";
import { Client } from "../client.ts";
@@ -5,29 +6,34 @@ import { Client } from "../client.ts";
export function proxySyscalls(client: Client, names: string[]): SysCallMapping {
const syscalls: SysCallMapping = {};
for (const name of names) {
syscalls[name] = async (_ctx, ...args: any[]) => {
if (!client.remoteSpacePrimitives) {
throw new Error("Not supported");
}
const resp = await client.remoteSpacePrimitives.authenticatedFetch(
`${client.remoteSpacePrimitives.url}/.rpc`,
{
method: "POST",
body: JSON.stringify({
operation: "syscall",
name,
args,
}),
},
);
const result: SyscallResponse = await resp.json();
if (result.error) {
console.error("Remote syscall error", result.error);
throw new Error(result.error);
} else {
return result.result;
}
syscalls[name] = (_ctx, ...args: any[]) => {
return proxySyscall(client.remoteSpacePrimitives, name, args);
};
}
return syscalls;
}
export async function proxySyscall(
httpSpacePrimitives: HttpSpacePrimitives,
name: string,
args: any[],
): Promise<any> {
const resp = await httpSpacePrimitives.authenticatedFetch(
`${httpSpacePrimitives.url}/.rpc`,
{
method: "POST",
body: JSON.stringify({
operation: "syscall",
name,
args,
}),
},
);
const result: SyscallResponse = await resp.json();
if (result.error) {
console.error("Remote syscall error", result.error);
throw new Error(result.error);
} else {
return result.result;
}
}