Support documented syscall registrations
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Hook, Manifest } from "../types.ts";
|
||||
import type { SysCallMapping, System } from "../system.ts";
|
||||
import type { SysCallMapping, SyscallContext, System } from "../system.ts";
|
||||
import type { SyscallHookT } from "@silverbulletmd/silverbullet/type/manifest";
|
||||
|
||||
export class SyscallHook implements Hook<SyscallHookT> {
|
||||
@@ -24,16 +24,31 @@ export class SyscallHook implements Hook<SyscallHookT> {
|
||||
continue;
|
||||
}
|
||||
|
||||
const syscallName = functionDef.syscall;
|
||||
const syscallDefinition = functionDef.syscall;
|
||||
const syscallName =
|
||||
typeof syscallDefinition === "string"
|
||||
? syscallDefinition
|
||||
: syscallDefinition.name;
|
||||
|
||||
// Add the syscall to our mapping
|
||||
syscalls[syscallName] = (ctx, ...args) => {
|
||||
// Delegate to the system to invoke the function
|
||||
return system.syscall(ctx, "system.invokeFunction", [
|
||||
const callback = (ctx: SyscallContext, ...args: any[]) =>
|
||||
system.syscall(ctx, "system.invokeFunction", [
|
||||
`${plug.manifest!.name}.${name}`,
|
||||
...args,
|
||||
]);
|
||||
};
|
||||
syscalls[syscallName] =
|
||||
typeof syscallDefinition === "string"
|
||||
? callback
|
||||
: {
|
||||
callback,
|
||||
documentation: {
|
||||
description: syscallDefinition.description,
|
||||
parameters: syscallDefinition.parameters,
|
||||
returns: syscallDefinition.returns,
|
||||
deprecated: syscallDefinition.deprecated,
|
||||
see: syscallDefinition.see,
|
||||
},
|
||||
};
|
||||
|
||||
// Register the syscalls with no required permissions
|
||||
system.registerSyscalls([], syscalls);
|
||||
@@ -48,16 +63,19 @@ export class SyscallHook implements Hook<SyscallHookT> {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate syscall name is provided
|
||||
if (!functionDef.syscall) {
|
||||
const syscallName =
|
||||
typeof functionDef.syscall === "string"
|
||||
? functionDef.syscall
|
||||
: functionDef.syscall.name;
|
||||
if (!syscallName) {
|
||||
errors.push(`Function ${name} has a syscall but no name`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate syscall name format (should be namespaced)
|
||||
if (!functionDef.syscall.includes(".")) {
|
||||
if (!syscallName.includes(".")) {
|
||||
errors.push(
|
||||
`Function ${name} has invalid syscall name "${functionDef.syscall}" - must be in format "namespace.name"`,
|
||||
`Function ${name} has invalid syscall name "${syscallName}" - must be in format "namespace.name"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+25
-5
@@ -7,9 +7,10 @@ import {
|
||||
builtinPlugNames,
|
||||
builtinPlugPaths,
|
||||
} from "../../plugs/builtin_plugs.ts";
|
||||
import type { LuaFunctionDocumentation } from "../../plug-api/types/index.ts";
|
||||
|
||||
export interface SysCallMapping {
|
||||
[key: string]: (ctx: SyscallContext, ...args: any) => Promise<any> | any;
|
||||
[key: string]: SyscallSignature | SyscallDefinition;
|
||||
}
|
||||
|
||||
export type SystemEvents<HookT> = {
|
||||
@@ -26,9 +27,15 @@ export type SyscallContext = {
|
||||
|
||||
type SyscallSignature = (...args: any[]) => Promise<any> | any;
|
||||
|
||||
type Syscall = {
|
||||
export type SyscallDefinition = {
|
||||
callback: SyscallSignature;
|
||||
documentation?: LuaFunctionDocumentation;
|
||||
};
|
||||
|
||||
type RegisteredSyscall = {
|
||||
requiredPermissions: string[];
|
||||
callback: SyscallSignature;
|
||||
documentation: LuaFunctionDocumentation;
|
||||
};
|
||||
|
||||
export type SystemOptions = {
|
||||
@@ -37,7 +44,7 @@ export type SystemOptions = {
|
||||
};
|
||||
|
||||
export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
|
||||
registeredSyscalls = new Map<string, Syscall>();
|
||||
registeredSyscalls = new Map<string, RegisteredSyscall>();
|
||||
protected plugs = new Map<string, Plug<HookT>>();
|
||||
protected enabledHooks = new Set<Hook<HookT>>();
|
||||
|
||||
@@ -68,10 +75,23 @@ export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
|
||||
...registrationObjects: SysCallMapping[]
|
||||
) {
|
||||
for (const registrationObject of registrationObjects) {
|
||||
for (const [name, callback] of Object.entries(registrationObject)) {
|
||||
for (const [name, registration] of Object.entries(registrationObject)) {
|
||||
const definition =
|
||||
typeof registration === "function"
|
||||
? { callback: registration }
|
||||
: registration;
|
||||
const cleanName = name.startsWith("lua:")
|
||||
? name.slice("lua:".length)
|
||||
: name;
|
||||
const namespace = cleanName.split(".")[0];
|
||||
const defaultDocumentation = { see: `API/${namespace}` };
|
||||
this.registeredSyscalls.set(name, {
|
||||
requiredPermissions: requiredCapabilities,
|
||||
callback,
|
||||
callback: definition.callback,
|
||||
documentation: {
|
||||
...defaultDocumentation,
|
||||
...definition.documentation,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { expect, test } from "vitest";
|
||||
import { System } from "./plugos/system.ts";
|
||||
import {
|
||||
type ILuaFunction,
|
||||
LuaEnv,
|
||||
LuaStackFrame,
|
||||
type LuaTable,
|
||||
} from "./space_lua/runtime.ts";
|
||||
import { luaBuildStandardEnv } from "./space_lua/stdlib.ts";
|
||||
import { exposeSyscalls } from "./space_lua_api.ts";
|
||||
|
||||
test("documented syscalls retain metadata when exposed to Lua", async () => {
|
||||
const system = new System();
|
||||
system.registerSyscalls([], {
|
||||
"example.greet": {
|
||||
callback: (_ctx, name: string) => `Hello ${name}`,
|
||||
documentation: {
|
||||
description: "Greets someone.",
|
||||
parameters: [{ name: "name", type: "string" }],
|
||||
returns: [{ type: "string" }],
|
||||
see: "API/example",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const env = new LuaEnv(luaBuildStandardEnv());
|
||||
exposeSyscalls(env, system);
|
||||
const namespace = env.get("example") as LuaTable;
|
||||
const fn = namespace.get("greet") as ILuaFunction;
|
||||
expect(fn.info).toEqual({
|
||||
kind: "syscall",
|
||||
name: "example.greet",
|
||||
description: "Greets someone.",
|
||||
parameters: [{ name: "name", type: "string" }],
|
||||
returns: [{ type: "string" }],
|
||||
see: "API/example",
|
||||
});
|
||||
|
||||
const sf = LuaStackFrame.createWithGlobalEnv(env);
|
||||
const spacelua = env.get("spacelua", sf) as LuaTable;
|
||||
const renderDocumentation = spacelua.get(
|
||||
"renderApiDocumentation",
|
||||
sf,
|
||||
) as ILuaFunction;
|
||||
expect(await renderDocumentation.call(sf, "example")).toContain(
|
||||
"### `example.greet`",
|
||||
);
|
||||
expect(await renderDocumentation.call(sf)).toContain("### `print`");
|
||||
expect(renderDocumentation.info?.parameters?.[0]?.optional).toBe(true);
|
||||
});
|
||||
|
||||
test("legacy syscall registrations get an API page reference", () => {
|
||||
const system = new System();
|
||||
system.registerSyscalls([], {
|
||||
"example.ping": () => "pong",
|
||||
});
|
||||
expect(system.registeredSyscalls.get("example.ping")?.documentation).toEqual({
|
||||
see: "API/example",
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,7 @@ export function buildLuaEnv(system: System<any>) {
|
||||
export function exposeSyscalls(env: LuaEnv, system: System<any>) {
|
||||
// Expose all syscalls to Lua
|
||||
const nativeFs = new LuaStackFrame(env, null);
|
||||
for (const syscallName of system.registeredSyscalls.keys()) {
|
||||
for (const [syscallName, syscall] of system.registeredSyscalls) {
|
||||
const isLuaNativeSyscall = syscallName.startsWith("lua:");
|
||||
let cleanSyscallName = syscallName;
|
||||
if (isLuaNativeSyscall) {
|
||||
@@ -36,13 +36,18 @@ export function exposeSyscalls(env: LuaEnv, system: System<any>) {
|
||||
if (!env.has(ns)) {
|
||||
env.set(ns, new LuaTable(), nativeFs);
|
||||
}
|
||||
const info = {
|
||||
kind: "syscall" as const,
|
||||
name: cleanSyscallName,
|
||||
...syscall.documentation,
|
||||
};
|
||||
const luaFn = isLuaNativeSyscall
|
||||
? new LuaBuiltinFunction((_sf, ...args) => {
|
||||
return system.localSyscall(syscallName, args);
|
||||
})
|
||||
}, info)
|
||||
: new LuaNativeJSFunction((...args) => {
|
||||
return system.localSyscall(syscallName, args);
|
||||
});
|
||||
}, info);
|
||||
env.get(ns, nativeFs).set(fn, luaFn, nativeFs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Manifest as PlugosManifest } from "../../client/plugos/types.ts";
|
||||
import type { NamespaceOperation } from "./namespace.ts";
|
||||
import type { LuaFunctionDocumentation } from "./index.ts";
|
||||
|
||||
export type CodeWidgetT = {
|
||||
codeWidget?: string;
|
||||
@@ -143,7 +144,11 @@ export type SlashCommandHookT = {
|
||||
};
|
||||
|
||||
export type SyscallHookT = {
|
||||
syscall?: string;
|
||||
syscall?:
|
||||
| string
|
||||
| ({
|
||||
name: string;
|
||||
} & LuaFunctionDocumentation);
|
||||
};
|
||||
|
||||
export type DocumentEditorT = {
|
||||
|
||||
Reference in New Issue
Block a user