forked from mangalorg/luaprovider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.go
132 lines (112 loc) 路 2.75 KB
/
lua.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
package util
import (
"fmt"
lua "github.com/yuin/gopher-lua"
"reflect"
"strconv"
)
func NewTable(L *lua.LState, fields map[string]lua.LValue, funcs map[string]lua.LGFunction) *lua.LTable {
t := L.NewTable()
for k, v := range fields {
L.SetField(t, k, v)
}
if funcs != nil {
L.SetFuncs(t, funcs)
}
return t
}
func FromLTable(table *lua.LTable) (map[string]any, error) {
var err error
result := make(map[string]any)
table.ForEach(func(key lua.LValue, value lua.LValue) {
if key.Type() == lua.LTString {
result[string(key.(lua.LString))], err = FromLValue(value)
if err != nil {
return
}
}
})
return result, nil
}
func FromLValue(value lua.LValue) (any, error) {
switch value.Type() {
case lua.LTNil:
return nil, nil
case lua.LTBool:
return bool(value.(lua.LBool)), nil
case lua.LTNumber:
return float64(value.(lua.LNumber)), nil
case lua.LTString:
return string(value.(lua.LString)), nil
case lua.LTTable:
return FromLTable(value.(*lua.LTable))
case lua.LTUserData:
return value.(*lua.LUserData).Value, nil
default:
return nil, fmt.Errorf("unsupported type: %s", value.Type())
}
}
func ToLValue(L *lua.LState, value any) (lua.LValue, error) {
switch value := value.(type) {
case lua.LValue:
return value, nil
case nil:
return lua.LNil, nil
case bool:
return lua.LBool(value), nil
case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64, float32, float64:
f, err := strconv.ParseFloat(fmt.Sprint(value), 64)
if err != nil {
return nil, err
}
return lua.LNumber(f), nil
case string:
return lua.LString(value), nil
default:
switch reflect.TypeOf(value).Kind() {
case reflect.Slice:
table := L.NewTable()
// iterate over the slice
slice := reflect.ValueOf(value)
for i := 0; i < slice.Len(); i++ {
lvalue, err := ToLValue(L, slice.Index(i).Interface())
if err != nil {
return nil, err
}
L.RawSetInt(table, i+1, lvalue)
}
return table, nil
case reflect.Map:
table := L.NewTable()
// iterate over the map
m := reflect.ValueOf(value)
for _, key := range m.MapKeys() {
lkey, err := ToLValue(L, key.Interface())
if err != nil {
return nil, err
}
lvalue, err := ToLValue(L, m.MapIndex(key).Interface())
if err != nil {
return nil, err
}
L.SetTable(table, lkey, lvalue)
}
return table, nil
default:
return nil, fmt.Errorf("unsupported type: %T", value)
}
}
}
func Push[T any](state *lua.LState, t T, typeName string) {
ud := state.NewUserData()
ud.Value = t
state.SetMetatable(ud, state.GetTypeMetatable(typeName))
state.Push(ud)
}
func Check[T any](state *lua.LState, n int) T {
t, ok := state.CheckUserData(n).Value.(T)
if !ok {
state.ArgError(n, fmt.Sprintf("%T expected", t))
}
return t
}