-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
377 lines (354 loc) · 7.39 KB
/
util.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package glu
import (
"errors"
"fmt"
. "github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/parse"
"reflect"
"strings"
)
// CompileChunk compile code to FunctionProto
func CompileChunk(code string, source string) (*FunctionProto, error) {
name := fmt.Sprintf(source)
chunk, err := parse.Parse(strings.NewReader(code), name)
if err != nil {
return nil, err
}
return Compile(chunk, name)
}
// Operator operate stored state
type Operator = func(s *Vm) error
var (
//OpNone operator do nothing, better to use nil
OpNone = func(s *Vm) error { return nil }
//OpPush operator to push N value
OpPush = func(n ...LValue) Operator {
return func(s *Vm) error {
for _, value := range n {
s.Push(value)
}
return nil
}
}
//OpPushUserData operator to push N UserDate
OpPushUserData = func(n ...any) Operator {
return func(s *Vm) error {
for _, i := range n {
ud := s.NewUserData()
ud.Value = i
s.Push(ud)
}
return nil
}
}
//OpSafe operator to wrap as none error will happen
OpSafe = func(fn func(s *Vm)) Operator {
return func(s *Vm) error {
fn(s)
return nil
}
}
//OpPop operator to pop and consume N value from start,then pop to n
OpPop = func(fn func(value ...LValue), start, count int) Operator {
if start < 0 || count <= 0 {
panic("start must greater than 0 and count must greater than 0")
}
t := start + count
return func(s *Vm) error {
v := make([]LValue, 0, count)
for i := start; i < t; i++ {
v = append(v, s.Get(i))
}
s.Pop(t - 1)
fn(v...)
return nil
}
}
//OpPopN operator to pop n value (Reset stack)
OpPopN = func(count int) Operator {
if count < 1 {
panic("count must greater than 0")
}
return func(s *Vm) error {
s.Pop(count)
return nil
}
}
)
// ExecuteChunk execute pre complied FunctionProto
func ExecuteChunk(code *FunctionProto, argN, retN int, before Operator, after Operator) (err error) {
s := Get()
defer Put(s)
s.Push(s.NewFunctionFromProto(code))
if before != nil {
if err = before(s); err != nil {
return err
}
}
err = s.PCall(argN, retN, nil)
if err != nil {
return err
}
if after != nil {
return after(s)
}
return nil
}
func ExecuteFunction(fn *LFunction, argN, retN int, before Operator, after Operator) (err error) {
s := Get()
defer Put(s)
s.Push(fn)
if before != nil {
if err = before(s); err != nil {
return err
}
}
err = s.PCall(argN, retN, nil)
if err != nil {
return err
}
if after != nil {
return after(s)
}
return nil
}
// ExecuteCode run code in LState, use before to push args, after to extract return value
func ExecuteCode(code string, argsN, retN int, before Operator, after Operator) error {
s := Get()
defer Put(s)
if fn, err := s.LoadString(code); err != nil {
return err
} else {
s.Push(fn)
if before != nil {
if err = before(s); err != nil {
return err
}
}
err = s.PCall(argsN, retN, nil)
if err != nil {
return err
}
if after != nil {
return after(s)
}
return nil
}
}
// TableToSlice convert LTable to a Slice with all Number index values
func TableToSlice(s *LTable) (r []LValue) {
s.ForEach(func(key LValue, value LValue) {
if key.Type() == LTNumber {
r = append(r, value)
}
})
return
}
// TableToMap convert LTable to a Map with all key values
func TableToMap(s *LTable) (r map[LValue]LValue) {
r = make(map[LValue]LValue)
s.ForEach(func(key LValue, value LValue) {
r[key] = value
})
return
}
/*
TableUnpack convert LTable to a Map with all key values
All keys and values may be:
LTNumber: float64
LTBool: bool
LTTable: map[any]any
LTString: string
(those type will not output with noLua=true )
LTFunction: *LFunction
LTUserData: *LUserData
LChannel: LChannel
*/
func TableUnpack(s *LTable, noLua bool, history map[LValue]any) (r map[any]any, keys []any) {
h := history
if h == nil {
h = make(map[LValue]any)
}
r = make(map[any]any)
s.ForEach(func(key LValue, value LValue) {
var k any
var ok bool
if k, ok = h[key]; !ok {
switch key.Type() {
case LTBool:
k = key == LTrue
case LTNumber:
k = float64(key.(LNumber))
case LTString:
k = string(key.(LString))
case LTFunction:
if noLua {
k = nil
} else {
k = key.(*LFunction)
}
case LTUserData:
if noLua {
k = nil
} else {
k = key.(*LUserData)
}
case LTThread:
if noLua {
k = nil
} else {
k = key.(*LState)
}
case LTTable:
k, _ = TableUnpack(key.(*LTable), noLua, h)
case LTChannel:
if noLua {
k = nil
} else {
k = value.(LChannel)
}
}
h[key] = k
}
if k == nil {
return
}
var val any
keys = append(keys, k)
if val, ok = h[value]; !ok {
switch value.Type() {
case LTBool:
val = value == LTrue
case LTNumber:
val = float64(value.(LNumber))
case LTString:
val = string(value.(LString))
case LTFunction:
if noLua {
val = nil
} else {
val = value.(*LFunction)
}
case LTUserData:
if noLua {
val = nil
} else {
val = value.(*LUserData)
}
case LTThread:
if noLua {
val = nil
} else {
val = value.(*LState)
}
case LTTable:
val, _ = TableUnpack(value.(*LTable), noLua, h)
case LTChannel:
if noLua {
val = nil
} else {
val = value.(LChannel)
}
}
h[value] = val
}
if val != nil {
r[k] = val
}
})
return
}
var (
//ErrorSuppress not raise error ,use for SafeFunc
ErrorSuppress = errors.New("")
)
// Raw extract raw LValue: nil bool float64 string *LUserData *LState *LTable *LChannel
func Raw(v LValue) any {
switch v.Type() {
case LTNil:
return nil
case LTBool:
return v == LTrue
case LTNumber:
return float64(v.(LNumber))
case LTString:
return v.String()
case LTUserData:
return v.(*LUserData)
case LTThread:
return v.(*LState)
case LTTable:
return v.(*LTable)
case LTChannel:
return v.(*LChannel)
default:
panic(fmt.Sprintf("unknown LuaType: %d", v.Type()))
}
}
// Pack any to LValue.
//
// 1. nil, bool, numbers and other Lua value packed as normal LValue
//
// 2. array, slice,map[string]any packed into LTable (the elements also packed)
//
// 3. others are packed into LUserData
func Pack(v any, s *LState) LValue {
switch v.(type) {
case nil:
return LNil
case LValue:
return v.(LValue)
case bool:
return LBool(v.(bool))
case string:
return LString(v.(string))
case *LState:
return v.(*LState)
case *LFunction:
return v.(*LFunction)
case *LTable:
return v.(*LTable)
case *LUserData:
return v.(*LUserData)
case *LChannel:
return v.(*LChannel)
default:
vv := reflect.ValueOf(v)
switch {
case vv.Kind() < reflect.Complex64:
switch {
case vv.CanInt():
return LNumber(vv.Int())
case vv.CanFloat():
return LNumber(vv.Float())
default:
panic(fmt.Sprintf("unknown : %#v", v))
}
case vv.Kind() == reflect.Array || vv.Kind() == reflect.Slice:
t := s.NewTable()
for i := 0; i < vv.Len(); i++ {
t.Insert(i, Pack(vv.Index(i).Interface(), s))
}
return t
case vv.Kind() == reflect.Map:
kt := vv.Type().Key()
if (kt.Kind() > reflect.Bool && kt.Kind() < reflect.Complex64) || kt.Kind() == reflect.String {
t := s.NewTable()
r := vv.MapRange()
for r.Next() {
k := Pack(r.Key().Interface(), s)
val := Pack(r.Value().Interface(), s)
t.RawSet(k, val)
}
return t
}
u := s.NewUserData()
u.Value = v
return u
default:
u := s.NewUserData()
u.Value = v
return u
}
}
}