* Loads all Lua scripts from the database and evaluates them in a new environment * @param system
()
| 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 | |
| 87 | export function resolveASTReference(ctx?: ASTCtx): Ref | null { |
no test coverage detected