new --version option and qualities method in obiscript

Former-commit-id: 7b0ce2901785d5c7494dec3a7a95d1fc5dc4a52b
This commit is contained in:
Eric Coissac
2024-04-13 12:40:43 +02:00
parent 0a2ea962db
commit cb35612a6c
5 changed files with 97 additions and 5 deletions

View File

@ -37,15 +37,17 @@ func pushInterfaceToLua(L *lua.LState, val interface{}) {
case []string:
pushSliceStringToLua(L, v)
case []int:
pushSliceIntToLua(L, v)
pushSliceNumericToLua(L, v)
case []byte:
pushSliceNumericToLua(L, v)
case []float64:
pushSliceFloat64ToLua(L, v)
pushSliceNumericToLua(L, v)
case []bool:
pushSliceBoolToLua(L, v)
case nil:
L.Push(lua.LNil)
default:
log.Fatalf("Cannot deal with value %v", val)
log.Fatalf("Cannot deal with value (%T) : %v", val, val)
}
}
@ -163,6 +165,21 @@ func pushSliceIntToLua(L *lua.LState, slice []int) {
L.Push(luaTable)
}
func pushSliceNumericToLua[T float64 | int | byte](L *lua.LState, slice []T) {
// Create a new Lua table
luaTable := L.NewTable()
// Iterate over the Go slice and set the elements in the Lua table
for _, value := range slice {
// Append the value to the Lua table
// Lua is 1-indexed, so we use the length of the table + 1 as the next index
luaTable.Append(lua.LNumber(value))
}
// Push the Lua table onto the stack
L.Push(luaTable)
}
// pushSliceStringToLua creates a new Lua table and sets the elements in the table from the given Go slice. It then pushes the Lua table onto the stack.
//
// L *lua.LState - The Lua state