| 98 | |
| 99 | // WARNING: It will not wait for open handles and can potentially cause bugs if JS code tries to reference Lua after executed |
| 100 | private async callByteCode(loader: (thread: Thread) => void): Promise<any> { |
| 101 | const thread = this.global.newThread() |
| 102 | const threadIndex = this.global.getTop() |
| 103 | try { |
| 104 | loader(thread) |
| 105 | const result = await thread.run(0) |
| 106 | if (result.length > 0) { |
| 107 | // The shenanigans here are to return the first result value on the stack. |
| 108 | // Say there's 2 values at stack indexes 1 and 2. Then top is 2, result.length is 2. |
| 109 | // That's why there's a + 1 sitting at the end. |
| 110 | return thread.getValue(thread.getTop() - result.length + 1) |
| 111 | } |
| 112 | return undefined |
| 113 | } finally { |
| 114 | // Pop the read on success or failure |
| 115 | this.global.remove(threadIndex) |
| 116 | } |
| 117 | } |
| 118 | } |