// deno-lint-ignore-file ban-types import { System } from "../plugos/system.ts"; import { ScriptObject } from "../plugs/index/script.ts"; export class ScriptEnvironment { functions: Record = {}; // Public API registerFunction(name: string, fn: Function) { this.functions[name] = fn; } // Internal API evalScript(script: string, system: System) { try { Function("silverbullet", "syscall", script)( this, (name: string, ...args: any[]) => system.syscall({}, name, args), ); } catch (e: any) { throw new Error( `Error evaluating script: ${e.message} for script: ${script}`, ); } } async loadFromSystem(system: System) { if (!system.loadedPlugs.has("index")) { console.warn("Index plug not found, skipping loading space scripts"); return; } const allScripts: ScriptObject[] = await system.invokeFunction( "index.queryObjects", ["space-script", {}], ); for (const script of allScripts) { this.evalScript(script.script, system); } } }