MCPcopy Create free account
hub / github.com/canwhite/Krebs / initialize

Function initialize

tools/lua-runtime.ts:76–138  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

74
75export const luaRuntime: LuaRuntime = {
76 async initialize(): Promise<void> {
77 if (luaEngine) return;
78 if (initPromise) return initPromise;
79
80 initPromise = (async () => {
81 const factory = new LuaFactory();
82 luaEngine = await factory.createEngine();
83
84 // ===== 注册 JS 桥接函数 =====
85
86 // 时间相关
87 luaEngine.global.set("js_now", () => Date.now());
88
89 luaEngine.global.set(
90 "js_format_date",
91 (ts: number, tz?: string) => {
92 return new Date(ts).toLocaleString("zh-CN", {
93 timeZone: tz || Intl.DateTimeFormat().resolvedOptions().timeZone,
94 });
95 }
96 );
97
98 // 数学相关
99 luaEngine.global.set(
100 "js_random",
101 (min: number, max: number) => {
102 return (
103 Math.floor(Math.random() * (max - min + 1)) + min
104 );
105 }
106 );
107
108 // 文件操作相关
109 luaEngine.global.set(
110 "js_write_file",
111 async (path: string, content: string) => {
112 const fs = await import("node:fs/promises");
113 const safePath = validatePath(path);
114 await fs.writeFile(safePath, content, "utf-8");
115 return { success: true, path: safePath };
116 }
117 );
118
119 luaEngine.global.set("js_read_file", async (path: string) => {
120 const fs = await import("node:fs/promises");
121 const safePath = validatePath(path);
122 const content = await fs.readFile(safePath, "utf-8");
123 return { success: true, content };
124 });
125
126 luaEngine.global.set("js_exists", async (path: string) => {
127 const fs = await import("node:fs/promises");
128 try {
129 await fs.access(validatePath(path));
130 return true;
131 } catch {
132 return false;
133 }

Callers

nothing calls this directly

Calls 1

validatePathFunction · 0.85

Tested by

no test coverage detected