From 9088f6188dba0b3bec36151e053ed0dedf1db6fe Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 22 Jul 2024 18:56:04 +0530 Subject: [PATCH] fix: glance over lua.h bindings and fix issues --- internal/lauxlib.go | 1 + internal/lua.go | 18 ++++++++++++----- internal/util.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 internal/util.go diff --git a/internal/lauxlib.go b/internal/lauxlib.go index 6b20016..e971875 100644 --- a/internal/lauxlib.go +++ b/internal/lauxlib.go @@ -172,6 +172,7 @@ func LErrorL(L *LuaState, msg string) { defer C.free(unsafe.Pointer(cmsg)) C.cluaL_errorL(L, cmsg) + panic(msg) } func LCheckOption(L *LuaState, narg int32, def string, lst []string) int32 { diff --git a/internal/lua.go b/internal/lua.go index ae5e9da..7461199 100644 --- a/internal/lua.go +++ b/internal/lua.go @@ -22,9 +22,9 @@ import ( // const ( - LUAI_MAXCSTACK = 1000000 // Max allowed values in lua stack - LUA_UTAG_LIMIT = 128 // Max number of lua userdata tags - LUA_LUTAG_LIMIT = 128 // Max number of light lua userdata tags + LUAI_MAXCSTACK = 8000 // Max allowed values in lua stack + LUA_UTAG_LIMIT = 128 // Max number of lua userdata tags + LUA_LUTAG_LIMIT = 128 // Max number of light lua userdata tags ) const LUA_MULTRET = -1 @@ -504,6 +504,10 @@ func SetSafeEnv(L *LuaState, idx int32, enabled bool) { C.lua_setsafeenv(L, C.int(idx), cenabled) } +func GetMetatable(L *LuaState, objindex int32) int32 { + return int32(C.lua_getmetatable(L, C.int(objindex))) +} + func Getfenv(L *LuaState, idx int32) { C.lua_getfenv(L, C.int(idx)) } @@ -525,6 +529,10 @@ func SetField(L *LuaState, idx int32, k string) { C.lua_setfield(L, C.int(idx), ck) } +func RawSet(L *LuaState, idx int32) { + C.lua_rawset(L, C.int(idx)) +} + func RawSetI(L *LuaState, idx int32, n int32) { C.lua_rawseti(L, C.int(idx), C.int(n)) } @@ -553,11 +561,11 @@ func LuauLoad(L *LuaState, chunkname string, data string, size uint64, env int32 return int32(C.luau_load(L, cchunkname, cdata, C.size_t(size), C.int(env))) } -func LuaCall(L *LuaState, nargs int32, nresults int32) { +func Call(L *LuaState, nargs int32, nresults int32) { C.lua_call(L, C.int(nargs), C.int(nresults)) } -func LuaPcall(L *LuaState, nargs int32, nresults int32, errfunc int32) int32 { +func Pcall(L *LuaState, nargs int32, nresults int32, errfunc int32) int32 { return int32(C.lua_pcall(L, C.int(nargs), C.int(nresults), C.int(errfunc))) } diff --git a/internal/util.go b/internal/util.go new file mode 100644 index 0000000..6af0e99 --- /dev/null +++ b/internal/util.go @@ -0,0 +1,49 @@ +package internal + +//#include +import "C" + +func GetSubtable(L *LuaState, idx int32, fname string) bool { + absIdx := AbsIndex(L, idx) + if !CheckStack(L, 3+20) { + panic("stack overflow") + } + + PushString(L, fname) + if GetTable(L, absIdx) == LUA_TTABLE { + return true + } + + Pop(L, 1) + NewTable(L) + PushString(L, fname) + PushValue(L, -2) + SetTable(L, absIdx) + return false +} + +func RequireLib(L *LuaState, modName string, openF LuaCFunction, isGlobal bool) { + if !CheckStack(L, 3+20) { + LErrorL(L, "stack overflow") + } + + GetSubtable(L, LUA_REGISTRYINDEX, "_LOADED") + if GetField(L, -1, modName) == LUA_TNIL { + Pop(L, 1) + PushCFunction(L, openF) + PushString(L, modName) + Call(L, 1, 1) + PushValue(L, -1) + SetField(L, -3, modName) + } + + if isGlobal { + PushNil(L) + SetGlobal(L, modName) + } else { + PushValue(L, -1) + SetGlobal(L, modName) + } + + Replace(L, -2) +}