[ENH] New way to register managed delegates with Garry's Mod Lua engine.
GlebChili opened this issue · 3 comments
Description
Current way of pushing managed delegate to Gmod's Lua engine consists of creating native callable wrapper for delegate with Marshal
class, optionally applying safe wrapper, and pushing resulting native function pointer to lua stack. This process is suboptimal, since end users can't distinguish native functions from managed in lua code.
TODO
Some experiments need to be done to get a finale solution, but current sketch is following:
-
Introduce new Lua type for callable objects to represent managed functions.
-
Replace
ILua.PushCFunction(CFuncManagedDelegate)
with something likeILua.PushManagedDelegate(Func<IntPtr, int>)
. -
Eliminate need for manual delegate reference retention by working with
__gc
metamethod of custom Lua type.
We have added new methods to the ILua
interface: PushManagedFunction(Func<ILua, int>)
and PushManagedClosure(Func<ILua, int>, int)
which keep track of managed delegates pushed. Thus developers no longer need to keep explicit references to the pushed delegates.
ILua.PushCFunction(CFuncManagedDelegate)
method and CFuncManagedDelegate
type were removed.