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 Mar 22, 2024
1 parent 87c783a commit d7e2ff0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ export default class LuaEngine {
// Contains the :await functionality.
this.global.registerTypeExtension(1, createPromiseType(this.global, injectObjects))

if (injectObjects) {
// Should be higher priority than table since that catches generic objects along
// with userdata so it doesn't end up a userdata type.
this.global.registerTypeExtension(5, createNullType(this.global))
}
// Should be higher priority than table since that catches generic objects along
// with userdata so it doesn't end up a userdata type.
this.global.registerTypeExtension(5, createNullType(this.global, injectObjects))

if (enableProxy) {
// This extension only really overrides tables and arrays.
Expand Down
25 changes: 25 additions & 0 deletions src/type-extensions/default-null.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Global from '../global'
import Thread from '../thread'
import TypeExtension from '../type-extension'

// A default extension that treats js null as lua nil
class DefaultNullTypeExtension extends TypeExtension<unknown> {
constructor(thread: Global) {
super(thread, 'js_null')
}
public getValue(): null {
throw new Error('nil values should be converted by pushValue')
}
public pushValue(thread: Thread, decoration: any): boolean {
if (decoration?.target !== null) {
return false
}
thread.lua.lua_pushnil(thread.address)
return true
}
public close(): void {}
}

export default function createTypeExtension(thread: Global): TypeExtension<null> {
return new DefaultNullTypeExtension(thread)
}
8 changes: 6 additions & 2 deletions src/type-extensions/null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LuaReturn, LuaState } from '../types'
import Global from '../global'
import Thread from '../thread'
import TypeExtension from '../type-extension'
import createDefaultNullType from './default-null'

class NullTypeExtension extends TypeExtension<unknown> {
private gcPointer: number
Expand Down Expand Up @@ -73,6 +74,9 @@ class NullTypeExtension extends TypeExtension<unknown> {
}
}

export default function createTypeExtension(thread: Global): TypeExtension<null> {
return new NullTypeExtension(thread)
export default function createTypeExtension(thread: Global, userDataType: boolean): TypeExtension<null> {
if (userDataType) {
return new NullTypeExtension(thread)
}
return createDefaultNullType(thread)
}
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 d7e2ff0

Please sign in to comment.