Skip to content

Commit

Permalink
add util isPromise function
Browse files Browse the repository at this point in the history
  • Loading branch information
ceifa committed Apr 5, 2024
1 parent 0377b49 commit 58696da
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
7 changes: 3 additions & 4 deletions bin/wasmoon
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function main() {
input: process.stdin,
output: process.stdout,
terminal: true,
removeHistoryDuplicates: true
removeHistoryDuplicates: true,
})

rl.prompt()
Expand All @@ -91,8 +91,7 @@ async function main() {

const result = luamodule.lua_pcallk(lua.global.address, 0, LUA_MULTRET, 0, 0, null)
if (result === LuaReturn.Ok) {
const returnValues = Array.from({ length: lua.global.getTop() })
.map((_, i) => lua.global.indexToString(i + 1))
const returnValues = Array.from({ length: lua.global.getTop() }).map((_, i) => lua.global.indexToString(i + 1))

if (returnValues.length) {
console.log(...returnValues)
Expand All @@ -109,7 +108,7 @@ async function main() {
}
}

main().catch(err => {
main().catch((err) => {
console.error(err)
process.exit(1)
})
2 changes: 1 addition & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class LuaEngine {
// Move all stack results to the global state to avoid referencing the thread values
// which will be cleaned up in the finally below.
this.cmodule.lua_xmove(thread.address, this.global.address, result.length)
// The shenanigans here are to return the first reuslt value on the stack.
// The shenanigans here are to return the first result value on the stack.
// Say there's 2 values at stack indexes 1 and 2. Then top is 2, result.length is 2.
// That's why there's a + 1 sitting at the end.
return this.global.getValue(this.global.getTop() - result.length + 1)
Expand Down
7 changes: 4 additions & 3 deletions src/type-extensions/promise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Decoration } from '../decoration'
import { LuaReturn, LuaState } from '../types'
import { decorateFunction } from './function'
import { isPromise } from '../utils'
import Global from '../global'
import MultiReturn from '../multireturn'
import RawResult from '../raw-result'
Expand Down Expand Up @@ -34,8 +35,8 @@ class PromiseTypeExtension<T = unknown> extends TypeExtension<Promise<T>> {
thread.lua.lua_setfield(thread.address, metatableIndex, '__gc')

const checkSelf = (self: Promise<any>): true => {
if (Promise.resolve(self) !== self && typeof self.then !== 'function') {
throw new Error('promise method called without self instance')
if (!isPromise(self)) {
throw new Error('self instance is not a promise')
}
return true
}
Expand Down Expand Up @@ -131,7 +132,7 @@ class PromiseTypeExtension<T = unknown> extends TypeExtension<Promise<T>> {
}

public pushValue(thread: Thread, decoration: Decoration<Promise<T>>): boolean {
if (Promise.resolve(decoration.target) !== decoration.target && typeof decoration.target?.then !== 'function') {
if (!isPromise(decoration.target)) {
return false
}
return super.pushValue(thread, decoration)
Expand Down
3 changes: 2 additions & 1 deletion src/type-extensions/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseDecorationOptions, Decoration } from '../decoration'
import { LuaReturn, LuaState, LuaType } from '../types'
import { decorateFunction } from './function'
import { isPromise } from '../utils'
import Global from '../global'
import MultiReturn from '../multireturn'
import Thread from '../thread'
Expand Down Expand Up @@ -150,7 +151,7 @@ class ProxyTypeExtension extends TypeExtension<any, ProxyDecorationOptions> {
}
}

if (Promise.resolve(target) === target || typeof target.then === 'function') {
if (isPromise(target)) {
return false
}
} else if (options.proxy === false) {
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isPromise = (target: any): target is Promise<unknown> => {
return target && (Promise.resolve(target) === target || typeof target.then === 'function')
}

0 comments on commit 58696da

Please sign in to comment.