diff --git a/functions.go b/functions.go index db5f2e1..728910b 100644 --- a/functions.go +++ b/functions.go @@ -90,6 +90,25 @@ func (s *State) RegisterGoFunction(name string, fn GoFunction) error { // UnregisterGoFunction removes a global function 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.SetGlobal(name) }