Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default null to nil when injectObjects is not set #112

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading