-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various safety changes, bugfixes and added null type
* Support timing out callbacks from JS into Lua * Made calling functions from JS into Lua more robust. Previously there was a failure cause where if you created a thread, had that thread return a function, close the thread and attempt to call the function it would try to call the Lua function from within the context of a closed thread. All function calls from JS into Lua now use there own individual calling thread as Lua does when calling C functions and those threads are within a thread only used for functions within the Lua reference registry. This had an interesting side-effect that setInterval blocked the tests from completing because it no longer silently errored. * Inject null type * Protect against unable to allocate memory for thread creation * Fix thread missing parent reference in some cases
- Loading branch information
1 parent
f716b2f
commit 8577f9c
Showing
7 changed files
with
261 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Decoration } from '../decoration' | ||
import { LuaReturn, LuaState } from '../types' | ||
import Global from '../global' | ||
import Thread from '../thread' | ||
import TypeExtension from '../type-extension' | ||
|
||
class NullTypeExtension extends TypeExtension<unknown> { | ||
private gcPointer: number | ||
|
||
public constructor(thread: Global) { | ||
super(thread, 'js_null') | ||
|
||
this.gcPointer = thread.lua.module.addFunction((functionStateAddress: LuaState) => { | ||
// Throws a lua error which does a jump if it does not match. | ||
const userDataPointer = thread.lua.luaL_checkudata(functionStateAddress, 1, this.name) | ||
const referencePointer = thread.lua.module.getValue(userDataPointer, '*') | ||
thread.lua.unref(referencePointer) | ||
|
||
return LuaReturn.Ok | ||
}, 'ii') | ||
|
||
if (thread.lua.luaL_newmetatable(thread.address, this.name)) { | ||
const metatableIndex = thread.lua.lua_gettop(thread.address) | ||
|
||
// Mark it as uneditable | ||
thread.lua.lua_pushstring(thread.address, 'protected metatable') | ||
thread.lua.lua_setfield(thread.address, metatableIndex, '__metatable') | ||
|
||
// Add the gc function | ||
thread.lua.lua_pushcclosure(thread.address, this.gcPointer, 0) | ||
thread.lua.lua_setfield(thread.address, metatableIndex, '__gc') | ||
|
||
// Add an __index method that returns nothing. | ||
thread.pushValue(() => null) | ||
thread.lua.lua_setfield(thread.address, metatableIndex, '__index') | ||
|
||
thread.pushValue(() => 'null') | ||
thread.lua.lua_setfield(thread.address, metatableIndex, '__tostring') | ||
|
||
thread.pushValue((self: unknown, other: unknown) => self === other) | ||
thread.lua.lua_setfield(thread.address, metatableIndex, '__eq') | ||
} | ||
// Pop the metatable from the stack. | ||
thread.lua.lua_pop(thread.address, 1) | ||
|
||
// Create a new table, this is unique and will be the "null" value by attaching the | ||
// metatable created above. The first argument is the target, the second options. | ||
super.pushValue(thread, new Decoration<unknown>({}, {})) | ||
// Put it into the global field named null. | ||
thread.lua.lua_setglobal(thread.address, 'null') | ||
} | ||
|
||
public getValue(thread: Thread, index: number): null { | ||
const refUserData = thread.lua.luaL_testudata(thread.address, index, this.name) | ||
if (!refUserData) { | ||
throw new Error(`data does not have the expected metatable: ${this.name}`) | ||
} | ||
return null | ||
} | ||
|
||
// any because LuaDecoration is not exported from the Lua lib. | ||
public pushValue(thread: Thread, decoration: any): boolean { | ||
if (decoration?.target !== null) { | ||
return false | ||
} | ||
// Rather than pushing a new value, get the global "null" onto the stack. | ||
thread.lua.lua_getglobal(thread.address, 'null') | ||
return true | ||
} | ||
|
||
public close(): void { | ||
this.thread.lua.module.removeFunction(this.gcPointer) | ||
} | ||
} | ||
|
||
export default function createTypeExtension(thread: Global): TypeExtension<null> { | ||
return new NullTypeExtension(thread) | ||
} |
Oops, something went wrong.