From 1e26c9cac07052da54b58dffc1ce3eab5670ee5a Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Thu, 18 Jul 2024 22:04:27 +0530 Subject: [PATCH] fix: just pass C pointers in NewState & PushCClosureK --- internal/clua.c | 15 ++++++++------- internal/clua.h | 3 +-- internal/lua.go | 29 +++++++++++++++++++---------- main.go | 8 ++++++-- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/internal/clua.c b/internal/clua.c index 1ad986f..479f1a5 100644 --- a/internal/clua.c +++ b/internal/clua.c @@ -2,14 +2,15 @@ #include #include <_cgo_export.h> -void* clua_alloc(void* ud, void *ptr, size_t osize, size_t nsize) -{ - return (void*) go_allocf((GoUintptr) ud,(GoUintptr) ptr, osize, nsize); -} +// void* clua_alloc(void* ud, void *ptr, size_t osize, size_t nsize) +// { +// return (void*) go_allocf((GoUintptr) ud,(GoUintptr) ptr, osize, nsize); +// } -lua_State* clua_newstate(void* goallocf) + +lua_State* clua_newstate(void* f, void* ud) { - return lua_newstate(&clua_alloc, goallocf); + return lua_newstate((lua_Alloc)f, ud); } l_noret cluaL_errorL(lua_State* L, char* msg) @@ -18,5 +19,5 @@ l_noret cluaL_errorL(lua_State* L, char* msg) } void clua_pushcclosurek(lua_State* L, void* f, char* debugname, int nup, void* cont) { - return lua_pushcclosurek(L, f, debugname, nup, cont); + return lua_pushcclosurek(L, (lua_CFunction)f, debugname, nup, (lua_Continuation)cont); } \ No newline at end of file diff --git a/internal/clua.h b/internal/clua.h index 5693d3c..997dbda 100644 --- a/internal/clua.h +++ b/internal/clua.h @@ -1,7 +1,6 @@ #include #include -void* clua_alloc(void* ud, void *ptr, size_t osize, size_t nsize); -lua_State* clua_newstate(void* goallocf); +lua_State* clua_newstate(void* f, void* ud); l_noret cluaL_errorL(lua_State* L, char* msg); void clua_pushcclosurek(lua_State* L, void* f, char* debugname, int nup, void* cont); \ No newline at end of file diff --git a/internal/lua.go b/internal/lua.go index 2c80c8d..5801fbf 100644 --- a/internal/lua.go +++ b/internal/lua.go @@ -22,14 +22,13 @@ type LuaContinuation func(L *LuaState, status int32) int32 type LuaUDestructor = func(*C.void) type LuaDestructor = func(L *LuaState, _ unsafe.Pointer) -// type LuaAlloc = func(ud, ptr *C.void, osize, nsize C.size_t) *C.void -type LuaAlloc = func(ptr unsafe.Pointer, osize, nsize uint64) unsafe.Pointer +type LuaAlloc = func(ud, ptr unsafe.Pointer, osize, nsize C.size_t) *C.void -//export go_allocf -func go_allocf(fp uintptr, ptr uintptr, osize uint64, nsize uint64) uintptr { - p := ((*((*LuaAlloc)(unsafe.Pointer(fp))))(unsafe.Pointer(ptr), osize, nsize)) - return uintptr(p) -} +// TODO: Figure out when to C.malloc, and when to pass Go ptr +// Answer: If C frees some memory, then it's to be C.malloc'd, else pass go ptr +// Basically, some lua functions will manage memory themselves, and in that case +// we give them a C pointer - else we manage it ourselves. Technically you can give +// everything a C pointer, but you don't need to! // // ================== @@ -37,8 +36,14 @@ func go_allocf(fp uintptr, ptr uintptr, osize uint64, nsize uint64) uintptr { // ================== // -func NewState(ud unsafe.Pointer) *LuaState { - return C.clua_newstate(unsafe.Pointer(ud)) +func NewState(f LuaAlloc, ud unsafe.Pointer) *LuaState { + cf := C.malloc(C.size_t(unsafe.Sizeof(f))) + *(*LuaAlloc)(cf) = f + + cud := C.malloc(C.size_t(unsafe.Sizeof(ud))) + cud = ud + + return C.clua_newstate(cf, cud) } func LuaClose(L *LuaState) { @@ -299,10 +304,14 @@ func PushCClosureK(L *LuaState, f LuaCFunction, debugname string, nup int32, con if cont == nil { ccont = C.NULL } else { + ccont = C.malloc(C.size_t(unsafe.Sizeof(cont))) ccont = unsafe.Pointer(&cont) } - C.clua_pushcclosurek(L, unsafe.Pointer(&f), cdebugname, C.int(nup), ccont) + cf := C.malloc(C.size_t(unsafe.Sizeof(f))) + *(*LuaCFunction)(cf) = f + + C.clua_pushcclosurek(L, cf, cdebugname, C.int(nup), ccont) } // TODO: Rest of it diff --git a/main.go b/main.go index f34d754..1204f31 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,14 @@ import lualib "github.com/CompeyDev/lei/internal" func main() { lua := lualib.LNewState() + println("Lua VM Address: ", lua) + lualib.PushCClosureK(lua, func(L *lualib.LuaState) int32 { - println("hi from closure? ") + println("hi from closure?") return 0 }, "test", 0, nil) - println(lua) + if !lualib.IsCFunction(lua, 1) { + panic("CFunction was not correctly pushed onto stack") + } }