diff --git a/bin/wasmoon b/bin/wasmoon index 2bcf6cb..9100384 100755 --- a/bin/wasmoon +++ b/bin/wasmoon @@ -76,7 +76,7 @@ async function main() { input: process.stdin, output: process.stdout, terminal: true, - removeHistoryDuplicates: true + removeHistoryDuplicates: true, }) rl.prompt() @@ -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) @@ -109,7 +108,7 @@ async function main() { } } -main().catch(err => { +main().catch((err) => { console.error(err) process.exit(1) }) diff --git a/src/engine.ts b/src/engine.ts index f8875e2..5a55a7b 100755 --- a/src/engine.ts +++ b/src/engine.ts @@ -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) diff --git a/src/type-extensions/promise.ts b/src/type-extensions/promise.ts index 160c463..f37718f 100644 --- a/src/type-extensions/promise.ts +++ b/src/type-extensions/promise.ts @@ -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' @@ -34,8 +35,8 @@ class PromiseTypeExtension extends TypeExtension> { thread.lua.lua_setfield(thread.address, metatableIndex, '__gc') const checkSelf = (self: Promise): 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 } @@ -131,7 +132,7 @@ class PromiseTypeExtension extends TypeExtension> { } public pushValue(thread: Thread, decoration: Decoration>): boolean { - if (Promise.resolve(decoration.target) !== decoration.target && typeof decoration.target?.then !== 'function') { + if (!isPromise(decoration.target)) { return false } return super.pushValue(thread, decoration) diff --git a/src/type-extensions/proxy.ts b/src/type-extensions/proxy.ts index 3797b73..5bc1301 100644 --- a/src/type-extensions/proxy.ts +++ b/src/type-extensions/proxy.ts @@ -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' @@ -150,7 +151,7 @@ class ProxyTypeExtension extends TypeExtension { } } - if (Promise.resolve(target) === target || typeof target.then === 'function') { + if (isPromise(target)) { return false } } else if (options.proxy === false) { diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..68b54c0 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,3 @@ +export const isPromise = (target: any): target is Promise => { + return target && (Promise.resolve(target) === target || typeof target.then === 'function') +}