correction of several small bugs

This commit is contained in:
Eric Coissac
2024-09-03 06:08:07 -03:00
parent 373464cb06
commit 65ae82622e
22 changed files with 770 additions and 79 deletions

63
pkg/obilua/mutex.go Normal file
View File

@@ -0,0 +1,63 @@
package obilua
import (
lua "github.com/yuin/gopher-lua"
"sync"
)
const luaMutexTypeName = "Mutex"
func registerMutexType(luaState *lua.LState) {
mutexType := luaState.NewTypeMetatable(luaMutexTypeName)
luaState.SetGlobal(luaMutexTypeName, mutexType)
luaState.SetField(mutexType, "new", luaState.NewFunction(newMutex))
luaState.SetField(mutexType, "__index",
luaState.SetFuncs(luaState.NewTable(),
mutexMethods))
}
func mutex2Lua(interpreter *lua.LState, mutex *sync.Mutex) lua.LValue {
ud := interpreter.NewUserData()
ud.Value = mutex
interpreter.SetMetatable(ud, interpreter.GetTypeMetatable(luaMutexTypeName))
return ud
}
func pushMutexToLua(luaState *lua.LState, mutex *sync.Mutex) {
luaState.Push(mutex2Lua(luaState, mutex))
}
func newMutex(luaState *lua.LState) int {
m := &sync.Mutex{}
pushMutexToLua(luaState, m)
return 1
}
var mutexMethods = map[string]lua.LGFunction{
"lock": mutexLock,
"unlock": mutexUnlock,
}
func checkMutex(L *lua.LState) *sync.Mutex {
ud := L.CheckUserData(1)
if mutex, ok := ud.Value.(*sync.Mutex); ok {
return mutex
}
L.ArgError(1, "Mutex expected")
return nil
}
func mutexLock(L *lua.LState) int {
mutex := checkMutex(L)
mutex.Lock()
return 0
}
func mutexUnlock(L *lua.LState) int {
mutex := checkMutex(L)
mutex.Unlock()
return 0
}