fix: glance over lua.h bindings and fix issues

This commit is contained in:
Erica Marigold 2024-07-22 18:56:04 +05:30
parent 982735a0a4
commit 9088f6188d
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
3 changed files with 63 additions and 5 deletions

View file

@ -172,6 +172,7 @@ func LErrorL(L *LuaState, msg string) {
defer C.free(unsafe.Pointer(cmsg)) defer C.free(unsafe.Pointer(cmsg))
C.cluaL_errorL(L, cmsg) C.cluaL_errorL(L, cmsg)
panic(msg)
} }
func LCheckOption(L *LuaState, narg int32, def string, lst []string) int32 { func LCheckOption(L *LuaState, narg int32, def string, lst []string) int32 {

View file

@ -22,9 +22,9 @@ import (
// //
const ( const (
LUAI_MAXCSTACK = 1000000 // Max allowed values in lua stack LUAI_MAXCSTACK = 8000 // Max allowed values in lua stack
LUA_UTAG_LIMIT = 128 // Max number of lua userdata tags LUA_UTAG_LIMIT = 128 // Max number of lua userdata tags
LUA_LUTAG_LIMIT = 128 // Max number of light lua userdata tags LUA_LUTAG_LIMIT = 128 // Max number of light lua userdata tags
) )
const LUA_MULTRET = -1 const LUA_MULTRET = -1
@ -504,6 +504,10 @@ func SetSafeEnv(L *LuaState, idx int32, enabled bool) {
C.lua_setsafeenv(L, C.int(idx), cenabled) 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) { func Getfenv(L *LuaState, idx int32) {
C.lua_getfenv(L, C.int(idx)) 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) 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) { func RawSetI(L *LuaState, idx int32, n int32) {
C.lua_rawseti(L, C.int(idx), C.int(n)) 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))) 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)) 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))) return int32(C.lua_pcall(L, C.int(nargs), C.int(nresults), C.int(errfunc)))
} }

49
internal/util.go Normal file
View file

@ -0,0 +1,49 @@
package internal
//#include <stdlib.h>
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)
}