(
script: string,
params?: Record<string, unknown>
)
| 142 | }, |
| 143 | |
| 144 | async execute( |
| 145 | script: string, |
| 146 | params?: Record<string, unknown> |
| 147 | ): Promise<unknown> { |
| 148 | if (!this.isReady()) { |
| 149 | await this.initialize(); |
| 150 | } |
| 151 | |
| 152 | return mutex.withLock(async () => { |
| 153 | const lua = luaEngine; |
| 154 | |
| 155 | // 将 params 设置为全局变量 |
| 156 | if (params) { |
| 157 | lua.global.set("params", params); |
| 158 | } |
| 159 | |
| 160 | // 执行脚本,params 通过全局变量访问 |
| 161 | const wrappedScript = ` |
| 162 | local params = params |
| 163 | ${script} |
| 164 | `; |
| 165 | |
| 166 | // 执行,带超时 |
| 167 | const result = await Promise.race([ |
| 168 | lua.doString(wrappedScript), |
| 169 | new Promise<never>((_, reject) => |
| 170 | setTimeout( |
| 171 | () => reject(new Error("Lua execution timeout")), |
| 172 | DEFAULT_TIMEOUT_MS |
| 173 | ) |
| 174 | ), |
| 175 | ]); |
| 176 | |
| 177 | return result; |
| 178 | }); |
| 179 | }, |
| 180 | |
| 181 | registerJsFunction(name: string, fn: (...args: any[]) => any): void { |
| 182 | if (!luaEngine) throw new Error("Lua engine not initialized"); |
nothing calls this directly
no test coverage detected