mirror of
https://github.com/CompeyDev/lei.git
synced 2024-12-12 04:40:42 +00:00
fix: just pass C pointers in NewState & PushCClosureK
This commit is contained in:
parent
594added91
commit
1e26c9cac0
4 changed files with 34 additions and 21 deletions
|
@ -2,14 +2,15 @@
|
|||
#include <lua.h>
|
||||
#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);
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <lua.h>
|
||||
|
||||
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);
|
|
@ -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
|
||||
|
|
8
main.go
8
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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue