Skip to content

Commit

Permalink
Fix thread call returning wrong return values
Browse files Browse the repository at this point in the history
  • Loading branch information
ceifa committed Dec 18, 2022
1 parent 888deed commit e718b6f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ export default class Thread {
this.pushValue(arg)
}

const base = this.getTop() - args.length - 1 // The 1 is for the function to run
this.lua.lua_callk(this.address, args.length, LUA_MULTRET, 0, null)
return this.getStackValues()
return this.getStackValues(base)
}

public getStackValues(start = 0): MultiReturn {
Expand Down
16 changes: 16 additions & 0 deletions test/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,24 @@ test('should get only the last result on run', async () => {
const a = await engine.doString(`return 1`)
const b = await engine.doString(`return 3`)
const c = engine.doStringSync(`return 2`)
const d = engine.doStringSync(`return 5`)

expect(a).toEqual(1)
expect(b).toEqual(3)
expect(c).toEqual(2)
expect(d).toEqual(5)
})

test('should get only the return values on call function', async () => {
const engine = await getEngine()
engine.global.set('hello', (name) => `Hello ${name}!`)

const a = await engine.doString(`return 1`)
const b = engine.doStringSync(`return 5`)
const values = engine.global.call('hello', 'joao')

expect(a).toEqual(1)
expect(b).toEqual(5)
expect(values).toHaveLength(1)
expect(values[0]).toEqual('Hello joao!')
})

0 comments on commit e718b6f

Please sign in to comment.