(script: ChibiJson<any>, startState: any = null)
| 36 | } |
| 37 | |
| 38 | _subroutine(script: ChibiJson<any>, startState: any = null) { |
| 39 | this.ctx.setAsyncContext(false); |
| 40 | let state: any = startState; |
| 41 | // eslint-disable-next-line no-restricted-syntax |
| 42 | for (const [functionName, ...args] of script) { |
| 43 | if (!functionsRegistry[functionName]) { |
| 44 | throw new UnknownChibiFunctionError(functionName); |
| 45 | } |
| 46 | const func = functionsRegistry[functionName]; |
| 47 | try { |
| 48 | // Resolve chibiJson arguments if necessary |
| 49 | for (let i = 0; i < args.length; i += 1) { |
| 50 | if (this.isChibiJson(i, functionName, args[i])) { |
| 51 | args[i] = this._subroutine(args[i] as unknown as ChibiJson<any>); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | state = func(this.ctx, state, ...args); |
| 56 | } catch (error) { |
| 57 | if (error instanceof ChibiError) { |
| 58 | throw error; |
| 59 | } |
| 60 | this.logError(error, functionName, state, args); |
| 61 | state = null; |
| 62 | } |
| 63 | |
| 64 | if (state && state instanceof Promise) { |
| 65 | throw new ChibiError('Async functions are not supported in this context'); |
| 66 | } |
| 67 | |
| 68 | if (state && state instanceof ChibiReturn) { |
| 69 | return state; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return state; |
| 74 | } |
| 75 | |
| 76 | async runAsync() { |
| 77 | if (this.hasRun) { |
no test coverage detected