From 31b62816d3c80bbd069d5d6e4fca0b850f656409 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 15 Jul 2026 17:56:00 +0200 Subject: [PATCH] Support documented syscall registrations --- client/plugos/hooks/syscall.ts | 38 +++++++++++++++------ client/plugos/system.ts | 30 ++++++++++++++--- client/space_lua_api.test.ts | 60 ++++++++++++++++++++++++++++++++++ client/space_lua_api.ts | 11 +++++-- plug-api/types/manifest.ts | 7 +++- 5 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 client/space_lua_api.test.ts diff --git a/client/plugos/hooks/syscall.ts b/client/plugos/hooks/syscall.ts index 8975105d..630f39bd 100644 --- a/client/plugos/hooks/syscall.ts +++ b/client/plugos/hooks/syscall.ts @@ -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 { @@ -24,16 +24,31 @@ export class SyscallHook implements Hook { 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 { 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"`, ); } } diff --git a/client/plugos/system.ts b/client/plugos/system.ts index 7d290a8d..cf2c86ea 100644 --- a/client/plugos/system.ts +++ b/client/plugos/system.ts @@ -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; + [key: string]: SyscallSignature | SyscallDefinition; } export type SystemEvents = { @@ -26,9 +27,15 @@ export type SyscallContext = { type SyscallSignature = (...args: any[]) => Promise | 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 extends EventEmitter> { - registeredSyscalls = new Map(); + registeredSyscalls = new Map(); protected plugs = new Map>(); protected enabledHooks = new Set>(); @@ -68,10 +75,23 @@ export class System extends EventEmitter> { ...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, + }, }); } } diff --git a/client/space_lua_api.test.ts b/client/space_lua_api.test.ts new file mode 100644 index 00000000..9afaaa14 --- /dev/null +++ b/client/space_lua_api.test.ts @@ -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", + }); +}); diff --git a/client/space_lua_api.ts b/client/space_lua_api.ts index f6888df2..72a3b87f 100644 --- a/client/space_lua_api.ts +++ b/client/space_lua_api.ts @@ -26,7 +26,7 @@ export function buildLuaEnv(system: System) { export function exposeSyscalls(env: LuaEnv, system: System) { // 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) { 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); } } diff --git a/plug-api/types/manifest.ts b/plug-api/types/manifest.ts index 86ebedb9..8bc3570e 100644 --- a/plug-api/types/manifest.ts +++ b/plug-api/types/manifest.ts @@ -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 = {