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

View File

@ -20,6 +20,8 @@ import (
func NewInterpreter() *lua.LState {
lua := lua.NewState()
registerMutexType(lua)
RegisterObilib(lua)
RegisterObiContext(lua)
@ -117,7 +119,7 @@ func LuaWorker(proto *lua.FunctionProto) obiseq.SeqWorker {
case *obiseq.BioSequenceSlice:
return *val, err
default:
return nil, fmt.Errorf("worker function doesn't return the correct type")
return nil, fmt.Errorf("worker function doesn't return the correct type %T", val)
}
}

View File

@ -36,6 +36,8 @@ func obicontextGetSet(interpreter *lua.LState) int {
__lua_obicontext.Store(key, float64(val))
case lua.LString:
__lua_obicontext.Store(key, string(val))
case *lua.LUserData:
__lua_obicontext.Store(key, val.Value)
case *lua.LTable:
__lua_obicontext.Store(key, Table2Interface(interpreter, val))
default:

View File

@ -1,6 +1,8 @@
package obilua
import (
"sync"
log "github.com/sirupsen/logrus"
lua "github.com/yuin/gopher-lua"
@ -46,6 +48,8 @@ func pushInterfaceToLua(L *lua.LState, val interface{}) {
pushSliceBoolToLua(L, v)
case nil:
L.Push(lua.LNil)
case *sync.Mutex:
pushMutexToLua(L, v)
default:
log.Fatalf("Cannot deal with value (%T) : %v", val, val)
}

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
}