MCPcopy
hub / github.com/silverbulletmd/silverbullet / reload

Method reload

client/space_lua.ts:30–84  ·  view source on GitHub ↗

* Loads all Lua scripts from the database and evaluates them in a new environment * @param system

()

Source from the content-addressed store, hash-verified

28 * @param system
29 */
30 async reload() {
31 const allScripts: SpaceLuaObject[] = await this.objectIndex.queryLuaObjects(
32 this.env,
33 "space-lua",
34 {
35 objectVariable: "script",
36 orderBy: [
37 {
38 expr: parseExpressionString("script.priority or 0"),
39 desc: true,
40 nulls: "first",
41 },
42 {
43 expr: parseExpressionString("script.ref"),
44 desc: false,
45 },
46 ],
47 } as LuaCollectionQuery,
48 );
49 try {
50 this.env = buildLuaEnv(this.system);
51 const tl = new LuaEnv();
52 tl.setLocal("_GLOBAL", this.env);
53 for (const script of allScripts) {
54 try {
55 console.log("Now evaluating", script.ref);
56 const ast = parseBlock(script.script, { ref: script.ref });
57 // We create a local scope for each script
58 const scriptEnv = new LuaEnv(this.env);
59 const sf = new LuaStackFrame(tl, ast.ctx);
60 await evalStatement(ast, scriptEnv, sf);
61 } catch (e: any) {
62 if (e instanceof LuaRuntimeError) {
63 const origin = resolveASTReference(e.sf.astCtx!);
64 if (origin) {
65 console.error(
66 `Error evaluating script: ${e.message} at [[${encodeRef(
67 origin,
68 )}]]`,
69 );
70 continue;
71 }
72 }
73 console.error(
74 `Error evaluating script: ${e.message} for script: ${script.script}`,
75 e,
76 );
77 }
78 }
79
80 console.log("[Lua] Loaded", allScripts.length, "scripts");
81 } catch (e: any) {
82 console.error("Error reloading Lua scripts:", e.message);
83 }
84 }
85}
86
87export function resolveASTReference(ctx?: ASTCtx): Ref | null {

Callers 6

initMethod · 0.80
initSpaceMethod · 0.80
loadLuaScriptsMethod · 0.80
cachedFetchFunction · 0.80
editorSyscallsFunction · 0.80

Calls 8

setLocalMethod · 0.95
parseExpressionStringFunction · 0.90
buildLuaEnvFunction · 0.90
parseBlockFunction · 0.90
evalStatementFunction · 0.90
encodeRefFunction · 0.90
resolveASTReferenceFunction · 0.85
queryLuaObjectsMethod · 0.45

Tested by

no test coverage detected