|
| 1 | +package pool |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/yuin/gopher-lua" |
| 5 | + "layeh.com/gopher-luar" |
| 6 | + "reflect" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +type module struct { |
| 11 | + state *lua.LState |
| 12 | + table *lua.LTable |
| 13 | + method sync.Map |
| 14 | +} |
| 15 | + |
| 16 | +func (m *module) To(val interface{}, name ...string) bool { |
| 17 | + if len(name) > 0 { |
| 18 | + return __toValue(m.state.GetField(m.table, name[0]), val) |
| 19 | + } |
| 20 | + return __toValue(m.table, val) |
| 21 | +} |
| 22 | + |
| 23 | +func (m *module) Call(name string, args ...interface{}) { |
| 24 | + m.__callByName(name, 0, nil, args...) |
| 25 | +} |
| 26 | + |
| 27 | +func (m *module) Method(name string, val interface{}) bool { |
| 28 | + method := m.__method(name) |
| 29 | + if nil == method { |
| 30 | + return false |
| 31 | + } |
| 32 | + typ := reflect.TypeOf(val).Elem() |
| 33 | + build := func(*lua.LState) []reflect.Value { |
| 34 | + return nil |
| 35 | + } |
| 36 | + if typ.NumOut() > 0 { |
| 37 | + build = func(state *lua.LState) []reflect.Value { |
| 38 | + result := make([]reflect.Value, typ.NumOut()) |
| 39 | + top := state.GetTop() |
| 40 | + for i, l := 0, typ.NumOut(); i < l; i++ { |
| 41 | + v := reflect.New(typ.Out(i)) |
| 42 | + result[i] = v.Elem() |
| 43 | + if i < top { |
| 44 | + __toValue(state.Get(-(i + 1)), v.Interface()) |
| 45 | + } |
| 46 | + } |
| 47 | + return result |
| 48 | + } |
| 49 | + } |
| 50 | + reflect.ValueOf(val).Elem().Set( |
| 51 | + reflect.MakeFunc( |
| 52 | + typ, |
| 53 | + func(args []reflect.Value) (results []reflect.Value) { |
| 54 | + var params []interface{} |
| 55 | + if len(args) > 0 { |
| 56 | + params = make([]interface{}, len(args)) |
| 57 | + for i := range args { |
| 58 | + if args[i].IsValid() { |
| 59 | + params[i] = args[i].Interface() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return m.__call(method, typ.NumOut(), build, params...) |
| 64 | + }), |
| 65 | + ) |
| 66 | + return true |
| 67 | +} |
| 68 | + |
| 69 | +func (m *module) __method(name string) (method *lua.LFunction) { |
| 70 | + if it, ok := m.method.Load(name); ok { |
| 71 | + method = it.(*lua.LFunction) |
| 72 | + } else if f := m.state.GetField(m.table, name); lua.LTFunction == f.Type() { |
| 73 | + method = f.(*lua.LFunction) |
| 74 | + m.method.Store(name, method) |
| 75 | + } |
| 76 | + return |
| 77 | +} |
| 78 | + |
| 79 | +func (m *module) __callByName(name string, nret int, result func(*lua.LState) []reflect.Value, args ...interface{}) []reflect.Value { |
| 80 | + return m.__call(m.__method(name), nret, result, args...) |
| 81 | +} |
| 82 | + |
| 83 | +func (m *module) __call(method *lua.LFunction, nret int, result func(*lua.LState) []reflect.Value, args ...interface{}) []reflect.Value { |
| 84 | + thread, cancelFunc := m.state.NewThread() |
| 85 | + defer thread.Close() |
| 86 | + if cancelFunc != nil { |
| 87 | + defer cancelFunc() |
| 88 | + } |
| 89 | + defer thread.SetTop(0) |
| 90 | + thread.Push(method) |
| 91 | + thread.Push(m.table) |
| 92 | + for _, v := range args { |
| 93 | + thread.Push(luar.New(thread, v)) |
| 94 | + } |
| 95 | + thread.Call(len(args)+1, nret) |
| 96 | + if nil != result { |
| 97 | + return result(thread) |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
0 commit comments