Skip to content

Commit

Permalink
Default null to nil when injectObjects is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
gudzpoz committed Apr 3, 2024
1 parent 87c783a commit 11bbd9d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,14 @@ export default class Thread {
this.lua.lua_pushboolean(this.address, target ? 1 : 0)
break
default:
if (!this.typeExtensions.find((wrapper) => wrapper.extension.pushValue(this, decoratedValue, userdata))) {
throw new Error(`The type '${typeof target}' is not supported by Lua`)
if (this.typeExtensions.find((wrapper) => wrapper.extension.pushValue(this, decoratedValue, userdata))) {
break
}
if (target === null) {
this.lua.lua_pushnil(this.address)
break
}
throw new Error(`The type '${typeof target}' is not supported by Lua`)
}

if (decoratedValue.options.metatable) {
Expand Down
2 changes: 1 addition & 1 deletion src/type-extensions/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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 (Promise.resolve(decoration.target) !== decoration.target && typeof decoration.target?.then !== 'function') {
return false
}
return super.pushValue(thread, decoration)
Expand Down
12 changes: 12 additions & 0 deletions test/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,18 @@ describe('Engine', () => {
expect(res).to.deep.equal([null, null, 'null'])
})

it('null injected as nil', async () => {
const engine = await getEngine({ injectObjects: false })
engine.global.loadString(`
local args = { ... }
assert(type(args[1]) == "nil", string.format("expected first argument to be nil, got %s", type(args[1])))
return nil, args[1], tostring(nil)
`)
engine.global.pushValue(null)
const res = await engine.global.run(1)
expect(res).to.deep.equal([null, null, 'nil'])
})

it('Nested callback from JS to Lua', async () => {
const engine = await getEngine()
engine.global.set('call', (fn) => fn())
Expand Down

0 comments on commit 11bbd9d

Please sign in to comment.