forked from stevedonovan/luar
/
luar.go
797 lines (728 loc) · 19.9 KB
/
luar.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
package luar
import lua "github.com/stevedonovan/golua/lua51"
import "fmt"
import "strings"
import "errors"
//import "os"
import "reflect"
import "unsafe"
// Lua proxy objects for Slices and Maps
type ValueProxy struct {
value reflect.Value
t reflect.Type
}
const (
SLICE_META = "sliceMT"
MAP_META = "mapMT"
STRUCT_META = "structMT"
INTERFACE_META = "interfaceMT"
CHANNEL_META = "ChannelMT"
)
func makeValueProxy(L *lua.State, val reflect.Value, proxyMT string) {
rawptr := L.NewUserdata(uintptr(unsafe.Sizeof(ValueProxy{})))
ptr := (*ValueProxy)(rawptr)
ptr.value = val
ptr.t = val.Type()
lua.LGetMetaTable(L,proxyMT)
L.SetMetaTable(-2)
}
var valueOf = reflect.ValueOf
func valueOfProxy(L *lua.State, idx int) (reflect.Value,reflect.Type) {
vp := (*ValueProxy)(L.ToUserdata(idx))
return vp.value, vp.t
}
func isValueProxy(L *lua.State, idx int) bool {
res := false
if L.IsUserdata(idx) {
L.GetMetaTable(idx)
if ! L.IsNil(-1) {
L.GetField(-1,"luago.value")
res = ! L.IsNil(-1)
L.Pop(1)
}
L.Pop(1)
}
return res
}
func unwrapProxy(L *lua.State, idx int) interface{} {
if isValueProxy(L,idx) {
v,_ := valueOfProxy(L,idx)
return v.Interface()
}
return nil
}
func unwrapProxyValue(L *lua.State, idx int) reflect.Value{
return valueOf(unwrapProxy(L,idx))
}
func channel_send(L *lua.State) int {
fmt.Println("yield send")
L.PushValue(2)
L.PushValue(1)
L.PushBoolean(true)
return L.Yield(3)
//~ ch,t := valueOfProxy(L,1)
//~ val := valueOf(LuaToGo(L, t.Elem(),2))
//~ ch.Send(val)
//~ return 0
}
func channel_recv(L *lua.State) int {
fmt.Println("yield recv")
L.PushValue(1)
L.PushBoolean(false)
return L.Yield(2)
//~ ch,t := valueOfProxy(L,1)
//~ L.Yield(0)
//~ val,ok := ch.Recv()
//~ GoToLua(L,t.Elem(),val)
//~ L.PushBoolean(ok)
//~ L.Resume(0)
//~ return 2
}
func GoLua(L *lua.State) int {
go func() {
LT := L.NewThread()
L.PushValue(1)
lua.XMove(L,LT,1)
res := LT.Resume(0)
for res != 0 {
if res == 2 {
emsg := LT.ToString(-1)
fmt.Println("error",emsg)
}
ch,t := valueOfProxy(LT,-2)
if LT.ToBoolean(-1) { // send on a channel
val := valueOf(LuaToGo(LT, t.Elem(),-3))
ch.Send(val)
res = LT.Resume(0)
} else { // receive on a channel
val,ok := ch.Recv()
GoToLua(LT,t.Elem(),val)
LT.PushBoolean(ok)
res = LT.Resume(2)
}
}
} ()
return 0
}
func MakeChannel(L *lua.State) int {
ch := make(chan interface{})
makeValueProxy(L,valueOf(ch),CHANNEL_META)
return 1
}
func initializeProxies(L *lua.State) {
add_index := func (name string, fun lua.GoFunction) {
L.PushGoCallback(fun)
L.SetField(-2,name)
}
flagValue := func () {
L.PushBoolean(true)
L.SetField(-2,"luago.value")
L.Pop(1)
}
L.NewMetaTable(SLICE_META)
add_index("__index", slice__index)
add_index("__newindex",slice__newindex)
add_index("__len",slicemap__len)
flagValue()
L.NewMetaTable(MAP_META)
add_index("__index",map__index)
add_index("__newindex",map__newindex)
add_index("__len",slicemap__len)
flagValue()
L.NewMetaTable(STRUCT_META)
add_index("__index",struct__index)
add_index("__newindex",struct__newindex)
flagValue()
L.NewMetaTable(INTERFACE_META)
add_index("__index",interface__index)
flagValue()
L.NewMetaTable(CHANNEL_META)
//~ RegisterFunctions(L,"*",FMap {
//~ "Send":channel_send,
//~ "Recv":channel_recv,
//~ })
L.NewTable()
L.PushGoFunction(channel_send)
L.SetField(-2,"Send")
L.PushGoFunction(channel_recv)
L.SetField(-2,"Recv")
L.SetField(-2,"__index")
flagValue()
}
func slice_slice(L *lua.State) int {
slice,_ := valueOfProxy(L,1)
i1,i2 := L.ToInteger(2), L.ToInteger(3)
newslice := slice.Slice(i1,i2)
makeValueProxy(L,newslice,SLICE_META)
return 1
}
func slice__index (L *lua.State) int {
slice,_ := valueOfProxy(L,1)
if L.IsNumber(2) {
idx := L.ToInteger(2)
ret := slice.Index(idx-1)
GoToLua(L,ret.Type(),ret)
} else {
name := L.ToString(2)
switch name {
case "Slice":
L.PushGoFunction(slice_slice)
default:
fmt.Println("unknown slice method")
}
}
return 1
}
func slice__newindex (L *lua.State) int {
slice,t := valueOfProxy(L,1)
idx := L.ToInteger(2)
val := LuaToGo(L,t.Elem(),3)
slice.Index(idx-1).Set(valueOf(val))
return 0
}
func slicemap__len(L *lua.State) int {
val,_ := valueOfProxy(L,1)
L.PushInteger(val.Len())
return 1
}
func map__index (L *lua.State) int {
val,t := valueOfProxy(L, 1)
key := LuaToGo(L,t.Key(),2)
ret := val.MapIndex(valueOf(key))
GoToLua(L,ret.Type(),ret)
return 1
}
func map__newindex (L *lua.State) int {
m,t := valueOfProxy(L,1)
key := LuaToGo(L,t.Key(),2)
val := LuaToGo(L, t.Elem(),3)
m.SetMapIndex(valueOf(key),valueOf(val))
return 0
}
func callGoMethod (L *lua.State, name string, st reflect.Value) {
ret := st.MethodByName(name)
if ! ret.IsValid() {
fmt.Println("whoops")
}
L.PushGoFunction(GoLuaFunc(L,ret))
}
func struct__index (L *lua.State) int {
st,t := valueOfProxy(L,1)
name := L.ToString(2)
est := st
if t.Kind() == reflect.Ptr {
est = st.Elem()
}
ret := est.FieldByName(name)
if ! ret.IsValid() { // no such field, try for method?
callGoMethod(L,name,st)
} else {
GoToLua(L,ret.Type(),ret)
}
return 1
}
func interface__index (L *lua.State) int {
st,_ := valueOfProxy(L,1)
name := L.ToString(2)
callGoMethod(L,name,st)
return 1
}
func struct__newindex (L *lua.State) int {
st,t := valueOfProxy(L,1)
name := L.ToString(2)
if t.Kind() == reflect.Ptr {
st = st.Elem()
}
field := st.FieldByName(name)
val := LuaToGo(L,field.Type(),3)
field.Set(valueOf(val))
return 0
}
// end of proxy code
var (
tslice = make([]interface{},0)
tmap = make(map[string]interface{})
)
// return the Lua table at 'idx' as a copied Go slice. If 't' is nil then the slice
// type is []interface{}
func CopyTableToSlice (L *lua.State, t reflect.Type, idx int) interface{} {
if t == nil {
t = reflect.TypeOf(tslice)
}
te := t.Elem()
n := int(L.ObjLen(idx))
slice := reflect.MakeSlice(t,n,n)
for i := 1; i <= n; i++ {
L.RawGeti(idx,i)
val := LuaToGo(L,te,-1)
slice.Index(i-1).Set(valueOf(val))
L.Pop(1)
}
return slice.Interface()
}
// return the Lua table at 'idx' as a copied Go map. If 't' is nil then the map
// type is map[string]interface{}
func CopyTableToMap(L *lua.State, t reflect.Type, idx int) interface{} {
if t == nil {
t = reflect.TypeOf(tmap)
}
te,tk := t.Elem(), t.Key()
m := reflect.MakeMap(t)
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
// key at -2, value at -1
key := valueOf(LuaToGo(L,tk,-2))
val := valueOf(LuaToGo(L,te,-1))
m.SetMapIndex (key,val)
L.Pop(1)
}
return m.Interface()
}
func CopyTableToStruct(L *lua.State, t reflect.Type,idx int) interface{} {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
s := reflect.New(t) // T -> *T
ref := s.Elem()
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
key := L.ToString(-2)
f := ref.FieldByName(key)
if f.IsValid() {
val := valueOf(LuaToGo(L,f.Type(),-1))
f.Set(val)
}
L.Pop(1)
}
return s.Interface()
}
// copy a Go slice to a Lua table
func CopySliceToTable(L *lua.State, vslice reflect.Value) int {
if vslice.IsValid() && vslice.Type().Kind() == reflect.Slice {
n := vslice.Len()
L.CreateTable(n,0)
for i := 0; i < n; i++ {
L.PushInteger(i+1)
GoToLua(L,nil,vslice.Index(i))
L.SetTable(-3)
}
return 1
} else {
L.PushNil()
L.PushString("not a slice!")
}
return 2
}
// copy a Go map to a Lua table
func CopyMapToTable(L *lua.State, vmap reflect.Value) int {
if vmap.IsValid() && vmap.Type().Kind() == reflect.Map {
n := vmap.Len()
L.CreateTable(0,n)
for _,key := range vmap.MapKeys() {
val := vmap.MapIndex(key)
GoToLua(L,nil,key)
GoToLua(L,nil,val)
L.SetTable(-3)
}
return 1
} else {
L.PushNil()
L.PushString("not a map!")
}
return 2
}
// Push a Go value 'val' of type 't' on the Lua stack.
// If we haven't been given a concrete type, use the type of the value
// and unbox any interfaces.
func GoToLua (L *lua.State, t reflect.Type, val reflect.Value) {
proxify := true
if t == nil {
t = val.Type()
if t.Kind() == reflect.Interface { // unbox interfaces!
val = valueOf(val.Interface())
t = val.Type()
}
proxify = false
}
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
switch(t.Kind()) {
case reflect.Float64:
case reflect.Float32: {
L.PushNumber(val.Float())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: {
L.PushNumber(float64(val.Int()))
}
case reflect.Uint, reflect.Uint8: {
L.PushNumber(float64(val.Uint()))
}
case reflect.String: {
L.PushString(val.String())
}
case reflect.Bool: {
L.PushBoolean(val.Bool())
}
case reflect.Slice: {
if proxify {
makeValueProxy(L,val,SLICE_META)
} else {
CopySliceToTable(L,val)
}
}
case reflect.Map: {
if proxify {
makeValueProxy(L,val,MAP_META)
} else {
CopyMapToTable(L,val)
}
}
case reflect.Struct: {
makeValueProxy(L,val,STRUCT_META)
}
default: {
if val.IsNil() {
L.PushNil()
} else {
makeValueProxy(L,val,INTERFACE_META)
}
}
}
}
// Convert a Lua value 'idx' on the stack to the Go value of desired type 't'. Handles
// numerical and string types in a straightforward way, and will convert tables to
// either map or slice types.
func LuaToGo (L *lua.State, t reflect.Type, idx int) interface{}{
var value interface{}
var kind reflect.Kind
if t == nil { // let the Lua type drive the conversion...
switch(L.Type(idx)) {
case lua.LUA_TNIL: return nil // well, d'oh
case lua.LUA_TBOOLEAN: kind = reflect.Bool
case lua.LUA_TSTRING: kind = reflect.String
case lua.LUA_TTABLE: kind = reflect.Interface
default: return NewLuaObject(L,idx)
}
} else if t.Kind() == reflect.Ptr {
kind = t.Elem().Kind()
} else {
kind = t.Kind()
}
switch(kind) {
// various numerical types are tedious but straightforward
case reflect.Float64: {
ptr := new(float64)
*ptr = L.ToNumber(idx)
value = *ptr
}
case reflect.Float32: {
ptr := new(float32)
*ptr = float32(L.ToNumber(idx))
value = *ptr
}
case reflect.Int: {
ptr := new(int)
*ptr = int(L.ToNumber(idx))
value = *ptr
}
case reflect.Int8: {
ptr := new(byte)
*ptr = byte(L.ToNumber(idx))
value = *ptr
}
case reflect.String: {
tos := L.ToString(idx)
ptr := new(string)
*ptr = tos
value = *ptr
}
case reflect.Bool: {
ptr := new(bool)
*ptr = bool(L.ToBoolean(idx))
value = *ptr
}
case reflect.Slice: {
// if we get a table, then copy its values to a new slice
if L.IsTable(idx) {
value = CopyTableToSlice(L,t,idx)
} else {
value = unwrapProxy(L,idx)
}
}
case reflect.Map: {
if L.IsTable(idx) {
value = CopyTableToMap(L,t,idx)
} else {
value = unwrapProxy(L,idx)
}
}
case reflect.Struct: {
if L.IsTable(idx) {
value = CopyTableToStruct(L,t,idx)
} else {
value = unwrapProxy(L,idx)
}
}
case reflect.Interface: {
if L.IsTable(idx) {
// have to make an executive decision here: tables with non-zero
// length are assumed to be slices!
if L.ObjLen(idx) > 0 {
value = CopyTableToSlice(L,nil,idx)
} else {
value = CopyTableToMap(L,nil,idx)
}
} else
if L.IsNumber(idx) {
value = L.ToNumber(idx)
} else
if L.IsString(idx) {
value = L.ToString(idx)
} else
if L.IsBoolean(idx) {
value = L.ToBoolean(idx)
} else {
value = unwrapProxy(L,idx)
}
}
default: {
fmt.Println("unhandled type",t)
value = 20
}
}
return value
}
func functionArgRetTypes(funt reflect.Type) (targs, tout []reflect.Type) {
targs = make([]reflect.Type,funt.NumIn())
for i, _ := range targs {
targs[i] = funt.In(i)
}
tout = make([]reflect.Type,funt.NumOut())
for i,_ := range tout {
tout[i] = funt.Out(i)
}
return
}
// GoLuaFunc converts an arbitrary Go function into a Lua-compatible GoFunction.
// There are special optimized cases for functions that go from strings to strings,
// and doubles to doubles, but otherwise Go
// reflection is used to provide a generic wrapper function
func GoLuaFunc (L *lua.State, fun interface{}) lua.GoFunction {
switch f := fun.(type) {
case func(*lua.State) int: return f
case func(string) string: return func(L *lua.State) int {
L.PushString(f(L.ToString(1)))
return 1
}
case func(float64) float64: return func(L *lua.State) int {
L.PushNumber(f(L.ToNumber(1)))
return 1
}
default:
}
var funv reflect.Value
switch ff := fun.(type) {
case reflect.Value:
funv = ff
default:
funv = valueOf(fun)
}
funt := funv.Type()
targs, tout := functionArgRetTypes(funt)
return func (L *lua.State) int {
var lastT reflect.Type
isVariadic := funt.IsVariadic()
if isVariadic {
lastT = targs[len(targs)-1].Elem()
targs = targs[0:len(targs)-1]
}
args := make([]reflect.Value,len(targs))
for i,t := range targs {
val := LuaToGo(L,t,i+1)
args[i] = valueOf(val)
}
if isVariadic {
n := L.GetTop();
for i := len(targs)+1; i <= n; i++ {
val := valueOf(LuaToGo(L,lastT,i))
args = append(args,val)
}
}
resv := funv.Call(args)
for i, val := range resv {
GoToLua(L,tout[i],val)
}
return len(resv)
}
}
type Map map[string]interface{}
func register(L *lua.State, table string, values Map, convertFun bool) {
pop := true
if table == "*" {
pop = false
} else
if len(table) > 0 {
L.GetGlobal(table)
if L.IsNil(-1) {
L.NewTable()
L.SetGlobal(table)
L.GetGlobal(table)
}
} else {
L.GetGlobal("_G")
}
for name, val := range values {
t := reflect.TypeOf(val)
if t.Kind() == reflect.Func {
if convertFun {
L.PushGoFunction(GoLuaFunc(L,val))
} else {
lf := val.(func(*lua.State)int)
L.PushGoFunction(lf)
}
} else {
GoToLua(L,t,valueOf(val))
}
L.SetField(-2,name)
}
if pop {
L.Pop(1)
}
}
func RawRegister(L *lua.State, table string, values Map) {
register(L,table,values,false);
}
// make a number of Go functions or values available in Lua code. If table is non-nil,
// then create or reuse a global table of that name and put the values
// in it. If table is '*' then assume that the table is already on the stack.
// values is a map of strings to Go values.
func Register(L *lua.State, table string, values Map) {
register(L,table,values,true);
}
type LuaObject struct {
L *lua.State
Ref int
Type string
}
// index the Lua object using the key 'idx'
func (lo *LuaObject) Get(idx interface{}) interface{} {
L := lo.L
lo.Push() // the table
GoToLua(L,nil,valueOf(idx))
L.GetTable(-2)
val := LuaToGo(L,nil,-1)
L.Pop(1) // the table
return val
}
// set the value at a given idx
func (lo *LuaObject) Set(idx interface{}, val interface{}) interface{} {
L := lo.L
lo.Push() // the table
GoToLua(L,nil,valueOf(idx))
GoToLua(L,nil,valueOf(val))
L.SetTable(-3)
L.Pop(1) // the table
return val
}
// call a Lua object
func (lo *LuaObject) Call(args ...interface{}) (res interface{}, err error) {
L := lo.L
lo.Push() // the function...
for _,arg := range args { // push the args
GoToLua(L,nil,valueOf(arg))
}
ret := L.PCall(len(args),1,0)
if ret == 0 { // cool
res = LuaToGo(L,nil,-1)
} else {
err = errors.New(L.ToString(-1))
}
L.Pop(1)
return
}
// push this Lua object
func (lo *LuaObject) Push() {
lo.L.RawGeti(lua.LUA_REGISTRYINDEX, lo.Ref)
}
// free the Lua reference of this object
func (lo *LuaObject) Close() {
lua.Unref(lo.L,lua.LUA_REGISTRYINDEX, lo.Ref)
}
type LuaTableIter struct {
lo *LuaObject
first bool
Key interface{}
Value interface{}
}
// create a Lua table iterator
func (lo *LuaObject) Iter() *LuaTableIter{
return &LuaTableIter{lo,true,nil,nil}
}
func (ti *LuaTableIter) Next() bool {
L := ti.lo.L
if ti.first {
ti.lo.Push() // the table
L.PushNil() // the key
ti.first = false
}
// table is under the key
if L.Next(-2) == 0 {
return false
} else {
ti.Key = LuaToGo(L,nil,-2)
ti.Value = LuaToGo(L,nil,-1)
L.Pop(1) // drop value, key is now on top
}
return true
}
// create a new LuaObject using the given state and stack index.
func NewLuaObject(L *lua.State, idx int) *LuaObject {
tp := lua.LTypename(L,idx)
L.PushValue(idx)
ref := L.Ref(lua.LUA_REGISTRYINDEX)
return &LuaObject{L,ref,tp}
}
// create a new LuaObject using the given state and global qualified name
func NewLuaObjectFromName(L *lua.State, path string) *LuaObject {
Lookup(L,path,0)
return NewLuaObject(L,-1)
}
// look up a Lua value by its full name. If idx is 0, then this name
// is assumed to start in the global table, e.g. "string.gsub".
// With non-zero idx, can be used to look up subfields of a table
func Lookup(L *lua.State, path string, idx int) {
parts := strings.Split(path,".")
if idx != 0 {
L.PushValue(idx)
} else {
L.GetGlobal("_G")
}
for _,field := range parts {
L.GetField(-1,field)
L.Remove(-2) // remove table
}
}
func map2table(L *lua.State) int {
return CopyMapToTable(L,valueOf(unwrapProxy(L,1)))
}
func slice2table(L *lua.State) int {
return CopySliceToTable(L,valueOf(unwrapProxy(L,1)))
}
// make and initialize a new Lua state
func Init() *lua.State {
var L = lua.NewState()
L.OpenLibs();
initializeProxies(L)
RawRegister(L,"luar",Map{
"map2table":map2table,
"slice2table":slice2table,
})
return L
}