-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_array.go
136 lines (101 loc) · 2.69 KB
/
lua_array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gobindlua
import (
lua "github.com/yuin/gopher-lua"
)
const ARRAY_MODULES_NAME = "GblSlice"
const ARRAY_METATABLE_NAME = "GblSliceTable"
func LuaArrayModuleLoader(L *lua.LState) int {
staticMethodsTable := L.NewTable()
L.SetField(staticMethodsTable, "ToTable", L.NewFunction(arrayToTable))
L.Push(staticMethodsTable)
return 1
}
func LuaArrayRegisterGlobalMetatable(L *lua.LState) {
fieldsTable := L.NewTypeMetatable(ARRAY_METATABLE_NAME)
L.SetGlobal(ARRAY_METATABLE_NAME, fieldsTable)
L.SetField(fieldsTable, "__index", L.NewFunction(arrayIndex))
L.SetField(fieldsTable, "__newindex", L.NewFunction(arrayNewIndex))
L.SetField(fieldsTable, "__len", L.NewFunction(arrayLen))
}
type GblSlice struct {
Slice interface{}
Len func() int
Index func(idx int) lua.LValue
SetIndex func(idx int, val lua.LValue)
}
func (*GblSlice) LuaMetatableType() string {
return ARRAY_METATABLE_NAME
}
func checkArray(param int, L *lua.LState) *GblSlice {
slice, ok := L.CheckUserData(param).Value.(*GblSlice)
if !ok {
L.ArgError(1, "GblSlice type expected")
}
return slice
}
func arrayIndex(L *lua.LState) int {
slice := checkArray(1, L)
idx := int(L.CheckNumber(2) - 1)
if idx < 0 || idx >= slice.Len() {
L.ArgError(2, "out of bounds array index")
}
L.Push(slice.Index(idx))
return 1
}
func arrayNewIndex(L *lua.LState) int {
slice := checkArray(1, L)
idx := int(L.CheckNumber(2) - 1)
if idx < 0 || idx >= slice.Len() {
L.ArgError(1, "out of bounds array index")
}
n := L.CheckAny(3)
slice.SetIndex(idx, n)
return 0
}
func arrayLen(L *lua.LState) int {
L.Push(lua.LNumber(checkArray(1, L).Len()))
return 1
}
func arrayToTable(L *lua.LState) int {
slice := checkArray(1, L)
ret := L.NewTable()
len := slice.Len()
for i := len - 1; i >= 0; i-- {
ret.RawSetInt(i+1, slice.Index(i))
}
L.Push(ret)
return 1
}
func MapLuaArrayOrTableToGoSlice[T any](p lua.LValue, level int, mapper func(val lua.LValue) T) ([]T, error) {
var ret []T
switch t := p.(type) {
case *lua.LUserData:
ar, ok := t.Value.(*GblSlice)
if !ok {
return nil, badArrayOrTableCast(ret, t, level)
}
ret, ok = ar.Slice.([]T)
if !ok {
return nil, badArrayOrTableCast(ret, t, level)
}
return ret, nil
case *lua.LTable:
ret = make([]T, t.MaxN())
for i := 1; i <= t.MaxN(); i++ {
ret[i-1] = mapper(t.RawGetInt(i))
}
return ret, nil
case *lua.LNilType:
return nil, nil
default:
return nil, badArrayOrTableCast(ret, t, level)
}
}
func MapVariadicArgsToGoSlice[T any](start int, L *lua.LState, mapper func(val lua.LValue) T) ([]T, error) {
ret := make([]T, 0)
for i := start; i <= L.GetTop(); i++ {
val := L.CheckAny(i)
ret = append(ret, mapper(val))
}
return ret, nil
}