more robust unregistergofunction
This commit is contained in:
parent
13686b3e66
commit
075b45768f
19
functions.go
19
functions.go
@ -90,6 +90,25 @@ func (s *State) RegisterGoFunction(name string, fn GoFunction) error {
|
|||||||
|
|
||||||
// UnregisterGoFunction removes a global function
|
// UnregisterGoFunction removes a global function
|
||||||
func (s *State) UnregisterGoFunction(name string) {
|
func (s *State) UnregisterGoFunction(name string) {
|
||||||
|
// First, find the function to get its pointer
|
||||||
|
s.GetGlobal(name)
|
||||||
|
if s.IsFunction(-1) {
|
||||||
|
// Get the first upvalue (function pointer)
|
||||||
|
if C.lua_getupvalue(s.L, -1, 1) != nil {
|
||||||
|
ptr := C.lua_touserdata(s.L, -1)
|
||||||
|
if ptr != nil {
|
||||||
|
// Remove from registry
|
||||||
|
functionRegistry.Lock()
|
||||||
|
delete(functionRegistry.funcs, ptr)
|
||||||
|
C.free(ptr)
|
||||||
|
functionRegistry.Unlock()
|
||||||
|
}
|
||||||
|
s.Pop(1) // Pop upvalue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.Pop(1) // Pop function or nil
|
||||||
|
|
||||||
|
// Remove global
|
||||||
s.PushNil()
|
s.PushNil()
|
||||||
s.SetGlobal(name)
|
s.SetGlobal(name)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user