LuaJIT-to-Go/functions.go
Sky Johnson f4bfff470f massive rewrite
fix go func mallocs
add helper utils
2025-05-31 17:42:58 -05:00

117 lines
2.5 KiB
Go

package luajit
/*
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
extern int goFunctionWrapper(lua_State* L);
// Helper function to access upvalues
static int get_upvalue_index(int i) {
return lua_upvalueindex(i);
}
*/
import "C"
import (
"fmt"
"sync"
"unsafe"
)
// GoFunction defines the signature for Go functions callable from Lua
type GoFunction func(*State) int
// Static registry size reduces resizing operations
const initialRegistrySize = 64
var (
// functionRegistry stores all registered Go functions
functionRegistry = struct {
sync.RWMutex
funcs map[unsafe.Pointer]GoFunction
initOnce sync.Once
}{
funcs: make(map[unsafe.Pointer]GoFunction, initialRegistrySize),
}
// statePool reuses State structs to avoid allocations
statePool = sync.Pool{
New: func() any {
return &State{}
},
}
)
//export goFunctionWrapper
func goFunctionWrapper(L *C.lua_State) C.int {
state := statePool.Get().(*State)
state.L = L
defer statePool.Put(state)
ptr := C.lua_touserdata(L, C.get_upvalue_index(1))
if ptr == nil {
state.PushString("error: function pointer not found")
return -1
}
functionRegistry.RLock()
fn, ok := functionRegistry.funcs[ptr]
functionRegistry.RUnlock()
if !ok {
state.PushString("error: function not found in registry")
return -1
}
return C.int(fn(state))
}
// PushGoFunction wraps a Go function and pushes it onto the Lua stack
func (s *State) PushGoFunction(fn GoFunction) error {
// Allocate unique memory for each function
ptr := C.malloc(C.size_t(unsafe.Sizeof(uintptr(0))))
if ptr == nil {
return fmt.Errorf("failed to allocate memory for function pointer")
}
// Register the function
functionRegistry.Lock()
functionRegistry.funcs[ptr] = fn
functionRegistry.Unlock()
// Push the pointer as lightuserdata
C.lua_pushlightuserdata(s.L, ptr)
C.lua_pushcclosure(s.L, (*[0]byte)(C.goFunctionWrapper), 1)
return nil
}
// RegisterGoFunction registers a Go function as a global Lua function
func (s *State) RegisterGoFunction(name string, fn GoFunction) error {
if err := s.PushGoFunction(fn); err != nil {
return err
}
s.SetGlobal(name)
return nil
}
// UnregisterGoFunction removes a global function
func (s *State) UnregisterGoFunction(name string) {
s.PushNil()
s.SetGlobal(name)
}
// Cleanup frees all function pointers and clears the registry
func (s *State) Cleanup() {
functionRegistry.Lock()
defer functionRegistry.Unlock()
// Free all allocated pointers
for ptr := range functionRegistry.funcs {
C.free(ptr)
delete(functionRegistry.funcs, ptr)
}
}