| 1 |
package luar |
| 2 |
|
| 3 |
import ( |
| 4 |
"container/list" |
| 5 |
"reflect" |
| 6 |
|
| 7 |
"github.com/yuin/gopher-lua" |
| 8 |
) |
| 9 |
|
| 10 |
func addMethods(L *lua.LState, c *Config, vtype reflect.Type, tbl *lua.LTable, ptrReceiver bool) { |
| 11 |
for i := 0; i < vtype.NumMethod(); i++ { |
| 12 |
method := vtype.Method(i) |
| 13 |
if method.PkgPath != "" { |
| 14 |
continue |
| 15 |
} |
| 16 |
namesFn := c.MethodNames |
| 17 |
if namesFn == nil { |
| 18 |
namesFn = defaultMethodNames |
| 19 |
} |
| 20 |
fn := funcWrapper(L, method.Func, ptrReceiver) |
| 21 |
for _, name := range namesFn(vtype, method) { |
| 22 |
tbl.RawSetString(name, fn) |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func addFields(L *lua.LState, c *Config, vtype reflect.Type, tbl *lua.LTable) { |
| 28 |
type element struct { |
| 29 |
Type reflect.Type |
| 30 |
Index []int |
| 31 |
} |
| 32 |
|
| 33 |
queue := list.New() |
| 34 |
queue.PushFront(element{ |
| 35 |
Type: vtype, |
| 36 |
}) |
| 37 |
|
| 38 |
namesFn := c.FieldNames |
| 39 |
if namesFn == nil { |
| 40 |
namesFn = defaultFieldNames |
| 41 |
} |
| 42 |
|
| 43 |
for queue.Len() > 0 { |
| 44 |
e := queue.Back() |
| 45 |
elem := e.Value.(element) |
| 46 |
vtype := elem.Type |
| 47 |
fields: |
| 48 |
for i := 0; i < vtype.NumField(); i++ { |
| 49 |
field := vtype.Field(i) |
| 50 |
if field.PkgPath != "" && !field.Anonymous { |
| 51 |
continue |
| 52 |
} |
| 53 |
names := namesFn(vtype, field) |
| 54 |
for _, key := range names { |
| 55 |
if tbl.RawGetString(key) != lua.LNil { |
| 56 |
continue fields |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
ud := L.NewUserData() |
| 61 |
ud.Value = append(elem.Index[:len(elem.Index):len(elem.Index)], i) |
| 62 |
for _, key := range names { |
| 63 |
tbl.RawSetString(key, ud) |
| 64 |
} |
| 65 |
if field.Anonymous { |
| 66 |
t := field.Type |
| 67 |
if field.Type.Kind() != reflect.Struct { |
| 68 |
if field.Type.Kind() != reflect.Ptr || field.Type.Elem().Kind() != reflect.Struct { |
| 69 |
continue |
| 70 |
} |
| 71 |
t = field.Type.Elem() |
| 72 |
} |
| 73 |
queue.PushFront(element{ |
| 74 |
Type: t, |
| 75 |
Index: append(elem.Index[:len(elem.Index):len(elem.Index)], i), |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
queue.Remove(e) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func getMetatable(L *lua.LState, vtype reflect.Type) *lua.LTable { |
| 85 |
config := GetConfig(L) |
| 86 |
|
| 87 |
if v := config.regular[vtype]; v != nil { |
| 88 |
return v |
| 89 |
} |
| 90 |
|
| 91 |
var ( |
| 92 |
mt *lua.LTable |
| 93 |
methods = L.CreateTable(0, vtype.NumMethod()) |
| 94 |
) |
| 95 |
|
| 96 |
switch vtype.Kind() { |
| 97 |
case reflect.Array: |
| 98 |
mt = L.CreateTable(0, 7) |
| 99 |
|
| 100 |
mt.RawSetString("__index", L.NewFunction(arrayIndex)) |
| 101 |
mt.RawSetString("__len", L.NewFunction(arrayLen)) |
| 102 |
mt.RawSetString("__call", L.NewFunction(arrayCall)) |
| 103 |
mt.RawSetString("__eq", L.NewFunction(arrayEq)) |
| 104 |
|
| 105 |
addMethods(L, config, vtype, methods, false) |
| 106 |
case reflect.Chan: |
| 107 |
mt = L.CreateTable(0, 8) |
| 108 |
|
| 109 |
mt.RawSetString("__index", L.NewFunction(chanIndex)) |
| 110 |
mt.RawSetString("__len", L.NewFunction(chanLen)) |
| 111 |
mt.RawSetString("__eq", L.NewFunction(chanEq)) |
| 112 |
mt.RawSetString("__call", L.NewFunction(chanCall)) |
| 113 |
mt.RawSetString("__unm", L.NewFunction(chanUnm)) |
| 114 |
|
| 115 |
addMethods(L, config, vtype, methods, false) |
| 116 |
case reflect.Map: |
| 117 |
mt = L.CreateTable(0, 7) |
| 118 |
|
| 119 |
mt.RawSetString("__index", L.NewFunction(mapIndex)) |
| 120 |
mt.RawSetString("__newindex", L.NewFunction(mapNewIndex)) |
| 121 |
mt.RawSetString("__len", L.NewFunction(mapLen)) |
| 122 |
mt.RawSetString("__call", L.NewFunction(mapCall)) |
| 123 |
|
| 124 |
addMethods(L, config, vtype, methods, false) |
| 125 |
case reflect.Slice: |
| 126 |
mt = L.CreateTable(0, 8) |
| 127 |
|
| 128 |
mt.RawSetString("__index", L.NewFunction(sliceIndex)) |
| 129 |
mt.RawSetString("__newindex", L.NewFunction(sliceNewIndex)) |
| 130 |
mt.RawSetString("__len", L.NewFunction(sliceLen)) |
| 131 |
mt.RawSetString("__call", L.NewFunction(sliceCall)) |
| 132 |
mt.RawSetString("__add", L.NewFunction(sliceAdd)) |
| 133 |
|
| 134 |
addMethods(L, config, vtype, methods, false) |
| 135 |
case reflect.Struct: |
| 136 |
mt = L.CreateTable(0, 6) |
| 137 |
|
| 138 |
fields := L.CreateTable(0, vtype.NumField()) |
| 139 |
addFields(L, config, vtype, fields) |
| 140 |
mt.RawSetString("fields", fields) |
| 141 |
|
| 142 |
mt.RawSetString("__index", L.NewFunction(structIndex)) |
| 143 |
mt.RawSetString("__eq", L.NewFunction(structEq)) |
| 144 |
|
| 145 |
addMethods(L, config, vtype, methods, false) |
| 146 |
case reflect.Ptr: |
| 147 |
switch vtype.Elem().Kind() { |
| 148 |
case reflect.Array: |
| 149 |
mt = L.CreateTable(0, 10) |
| 150 |
|
| 151 |
mt.RawSetString("__index", L.NewFunction(arrayPtrIndex)) |
| 152 |
mt.RawSetString("__newindex", L.NewFunction(arrayPtrNewIndex)) |
| 153 |
mt.RawSetString("__call", L.NewFunction(arrayCall)) // same as non-pointer |
| 154 |
mt.RawSetString("__len", L.NewFunction(arrayLen)) // same as non-pointer |
| 155 |
case reflect.Struct: |
| 156 |
mt = L.CreateTable(0, 8) |
| 157 |
|
| 158 |
mt.RawSetString("__index", L.NewFunction(structPtrIndex)) |
| 159 |
mt.RawSetString("__newindex", L.NewFunction(structPtrNewIndex)) |
| 160 |
default: |
| 161 |
mt = L.CreateTable(0, 7) |
| 162 |
|
| 163 |
mt.RawSetString("__index", L.NewFunction(ptrIndex)) |
| 164 |
} |
| 165 |
|
| 166 |
mt.RawSetString("__eq", L.NewFunction(ptrEq)) |
| 167 |
mt.RawSetString("__pow", L.NewFunction(ptrPow)) |
| 168 |
mt.RawSetString("__unm", L.NewFunction(ptrUnm)) |
| 169 |
|
| 170 |
addMethods(L, config, vtype, methods, true) |
| 171 |
default: |
| 172 |
panic("unexpected kind " + vtype.Kind().String()) |
| 173 |
} |
| 174 |
|
| 175 |
mt.RawSetString("__tostring", L.NewFunction(tostring)) |
| 176 |
mt.RawSetString("__metatable", lua.LString("gopher-luar")) |
| 177 |
mt.RawSetString("methods", methods) |
| 178 |
|
| 179 |
config.regular[vtype] = mt |
| 180 |
return mt |
| 181 |
} |
| 182 |
|
| 183 |
func getTypeMetatable(L *lua.LState, t reflect.Type) *lua.LTable { |
| 184 |
config := GetConfig(L) |
| 185 |
|
| 186 |
if v := config.types; v != nil { |
| 187 |
return v |
| 188 |
} |
| 189 |
|
| 190 |
mt := L.CreateTable(0, 2) |
| 191 |
mt.RawSetString("__call", L.NewFunction(typeCall)) |
| 192 |
mt.RawSetString("__eq", L.NewFunction(typeEq)) |
| 193 |
|
| 194 |
config.types = mt |
| 195 |
return mt |
| 196 |
} |