mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Add a basic managment of obisequenceslice to obiscript
Former-commit-id: c3d2888ebbfb237826630548b839ce93cdbf86eb
This commit is contained in:
@ -81,6 +81,8 @@ func LuaWorker(proto *lua.FunctionProto) obiseq.SeqWorker {
|
|||||||
switch val := s.(type) {
|
switch val := s.(type) {
|
||||||
case *obiseq.BioSequence:
|
case *obiseq.BioSequence:
|
||||||
return obiseq.BioSequenceSlice{val}, err
|
return obiseq.BioSequenceSlice{val}, err
|
||||||
|
case *obiseq.BioSequenceSlice:
|
||||||
|
return *val, err
|
||||||
default:
|
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")
|
||||||
}
|
}
|
||||||
|
@ -146,25 +146,6 @@ func pushMapStringFloat64ToLua(L *lua.LState, m map[string]float64) {
|
|||||||
L.Push(luaTable)
|
L.Push(luaTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pushSliceIntToLua creates a new Lua table and sets the elements of a Go slice in the Lua table. Then, it pushes the Lua table onto the stack.
|
|
||||||
//
|
|
||||||
// L *lua.LState, slice []int
|
|
||||||
// None
|
|
||||||
func pushSliceIntToLua(L *lua.LState, slice []int) {
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pushSliceNumericToLua[T float64 | int | byte](L *lua.LState, slice []T) {
|
func pushSliceNumericToLua[T float64 | int | byte](L *lua.LState, slice []T) {
|
||||||
// Create a new Lua table
|
// Create a new Lua table
|
||||||
luaTable := L.NewTable()
|
luaTable := L.NewTable()
|
||||||
@ -215,21 +196,3 @@ func pushSliceBoolToLua(L *lua.LState, slice []bool) {
|
|||||||
// Push the Lua table onto the stack
|
// Push the Lua table onto the stack
|
||||||
L.Push(luaTable)
|
L.Push(luaTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pushSliceFloat64ToLua creates a new Lua table and pushes it onto the stack.
|
|
||||||
//
|
|
||||||
// L *lua.LState - the Lua state
|
|
||||||
// slice []float64 - the Go slice to be inserted into the Lua table
|
|
||||||
func pushSliceFloat64ToLua(L *lua.LState, slice []float64) {
|
|
||||||
// Create a new Lua table
|
|
||||||
luaTable := L.NewTable()
|
|
||||||
|
|
||||||
// Iterate over the Go slice and insert each float64 into the Lua table
|
|
||||||
for _, value := range slice {
|
|
||||||
// Lua is 1-indexed, so we append the value to the Lua table
|
|
||||||
luaTable.Append(lua.LNumber(value))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push the Lua table onto the stack
|
|
||||||
L.Push(luaTable)
|
|
||||||
}
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
func RegisterObiSeq(luaState *lua.LState) {
|
func RegisterObiSeq(luaState *lua.LState) {
|
||||||
registerBioSequenceType(luaState)
|
registerBioSequenceType(luaState)
|
||||||
|
registerBioSequenceSliceType(luaState)
|
||||||
}
|
}
|
||||||
|
|
||||||
const luaBioSequenceTypeName = "BioSequence"
|
const luaBioSequenceTypeName = "BioSequence"
|
||||||
|
107
pkg/obilua/obiseqslice.go
Normal file
107
pkg/obilua/obiseqslice.go
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
package obilua
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
const luaBioSequenceSliceTypeName = "BioSequenceSlice"
|
||||||
|
|
||||||
|
func registerBioSequenceSliceType(luaState *lua.LState) {
|
||||||
|
bioSequenceSliceType := luaState.NewTypeMetatable(luaBioSequenceSliceTypeName)
|
||||||
|
luaState.SetGlobal(luaBioSequenceSliceTypeName, bioSequenceSliceType)
|
||||||
|
luaState.SetField(bioSequenceSliceType, "new", luaState.NewFunction(newObiSeqSlice))
|
||||||
|
|
||||||
|
luaState.SetField(bioSequenceSliceType, "__index",
|
||||||
|
luaState.SetFuncs(luaState.NewTable(),
|
||||||
|
bioSequenceSliceMethods))
|
||||||
|
}
|
||||||
|
|
||||||
|
func obiseqslice2Lua(interpreter *lua.LState,
|
||||||
|
seqslice *obiseq.BioSequenceSlice) lua.LValue {
|
||||||
|
ud := interpreter.NewUserData()
|
||||||
|
ud.Value = seqslice
|
||||||
|
interpreter.SetMetatable(ud, interpreter.GetTypeMetatable(luaBioSequenceSliceTypeName))
|
||||||
|
|
||||||
|
return ud
|
||||||
|
}
|
||||||
|
|
||||||
|
func newObiSeqSlice(luaState *lua.LState) int {
|
||||||
|
seqslice := obiseq.NewBioSequenceSlice()
|
||||||
|
luaState.Push(obiseqslice2Lua(luaState, seqslice))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
var bioSequenceSliceMethods = map[string]lua.LGFunction{
|
||||||
|
"push": bioSequenceSlicePush,
|
||||||
|
"pop": bioSequenceSlicePop,
|
||||||
|
"sequence": bioSequenceSliceGetSetSequence,
|
||||||
|
"len": bioSequenceSliceGetLength,
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkBioSequenceSlice(L *lua.LState) *obiseq.BioSequenceSlice {
|
||||||
|
ud := L.CheckUserData(1)
|
||||||
|
if v, ok := ud.Value.(*obiseq.BioSequenceSlice); ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
L.ArgError(1, "obiseq.BioSequenceSlice expected")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func bioSequenceSliceGetLength(luaState *lua.LState) int {
|
||||||
|
s := checkBioSequenceSlice(luaState)
|
||||||
|
luaState.Push(lua.LNumber(s.Len()))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func bioSequenceSliceGetSetSequence(luaState *lua.LState) int {
|
||||||
|
s := checkBioSequenceSlice(luaState)
|
||||||
|
index := luaState.CheckInt(2)
|
||||||
|
|
||||||
|
if index > s.Len() || index < 0 {
|
||||||
|
luaState.RaiseError("BioSequenceSlice index out of range")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if luaState.GetTop() == 3 {
|
||||||
|
ud := luaState.CheckUserData(3)
|
||||||
|
if v, ok := ud.Value.(*obiseq.BioSequence); ok {
|
||||||
|
(*s)[index] = v
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
luaState.ArgError(1, "obiseq.BioSequenceSlice expected")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
value := obiseq2Lua(luaState, (*s)[index])
|
||||||
|
luaState.Push(value)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func bioSequenceSlicePush(luaState *lua.LState) int {
|
||||||
|
s := checkBioSequenceSlice(luaState)
|
||||||
|
|
||||||
|
ud := luaState.CheckUserData(2)
|
||||||
|
if v, ok := ud.Value.(*obiseq.BioSequence); ok {
|
||||||
|
(*s) = append((*s), v)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
luaState.ArgError(1, "obiseq.BioSequenceSlice expected")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func bioSequenceSlicePop(luaState *lua.LState) int {
|
||||||
|
s := checkBioSequenceSlice(luaState)
|
||||||
|
if len(*s) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
seq := (*s)[len(*s)-1]
|
||||||
|
(*s) = (*s)[0 : len(*s)-1]
|
||||||
|
value := obiseq2Lua(luaState, seq)
|
||||||
|
luaState.Push(value)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user