forked from stevedonovan/luar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
297 lines (273 loc) · 7.69 KB
/
proxy.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
package luar
import (
"fmt"
"reflect"
"strconv"
"sync"
"github.com/vxcontrol/golua/lua"
)
// Lua proxy objects for Go slices, maps and structs
// TODO: Replace by interface{}?
type valueProxy struct {
v reflect.Value
t reflect.Type
}
const (
cNumberMeta = "numberMT"
cComplexMeta = "complexMT"
cStringMeta = "stringMT"
cSliceMeta = "sliceMT"
cMapMeta = "mapMT"
cStructMeta = "structMT"
cInterfaceMeta = "interfaceMT"
cChannelMeta = "channelMT"
)
var (
proxyIdCounter uintptr
proxyMap = map[uintptr]*valueProxy{}
proxymu sync.RWMutex
)
// commonKind returns the kind to which v1 and v2 can be converted with the
// least information loss.
func commonKind(v1, v2 reflect.Value) reflect.Kind {
k1 := unsizedKind(v1)
k2 := unsizedKind(v2)
if k1 == k2 && (k1 == reflect.Uint64 || k1 == reflect.Int64) {
return k1
}
if k1 == reflect.Complex128 || k2 == reflect.Complex128 {
return reflect.Complex128
}
return reflect.Float64
}
func isPointerToPrimitive(v reflect.Value) bool {
return v.Kind() == reflect.Ptr && v.Elem().IsValid() && v.Elem().Type() != nil
}
func isPredeclaredType(t reflect.Type) bool {
return t == reflect.TypeOf(0.0) || t == reflect.TypeOf("")
}
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 luaToGoValue(L *lua.State, idx int) (reflect.Value, reflect.Type) {
var a interface{}
err := LuaToGo(L, idx, &a)
if err != nil {
L.RaiseError(err.Error())
}
return reflect.ValueOf(a), reflect.TypeOf(a)
}
func makeValueProxy(L *lua.State, v reflect.Value, proxyMT string) {
// The metatable needs be set up in the Lua state before the proxy is created,
// otherwise closing the state will fail on calling the garbage collector. Not
// really sure why this happens though...
L.LGetMetaTable(proxyMT)
if L.IsNil(-1) {
flagValue := func() {
L.SetMetaMethod("__tostring", proxy__tostring)
L.SetMetaMethod("__gc", proxy__gc)
L.SetMetaMethod("__eq", proxy__eq)
L.PushBoolean(true)
L.SetField(-2, "luago.value")
L.Pop(1)
}
switch proxyMT {
case cNumberMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", interface__index)
L.SetMetaMethod("__lt", number__lt)
L.SetMetaMethod("__add", number__add)
L.SetMetaMethod("__sub", number__sub)
L.SetMetaMethod("__mul", number__mul)
L.SetMetaMethod("__div", number__div)
L.SetMetaMethod("__mod", number__mod)
L.SetMetaMethod("__pow", number__pow)
L.SetMetaMethod("__unm", number__unm)
flagValue()
case cComplexMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", complex__index)
L.SetMetaMethod("__add", number__add)
L.SetMetaMethod("__sub", number__sub)
L.SetMetaMethod("__mul", number__mul)
L.SetMetaMethod("__div", number__div)
L.SetMetaMethod("__pow", number__pow)
L.SetMetaMethod("__unm", number__unm)
flagValue()
case cStringMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", string__index)
L.SetMetaMethod("__len", string__len)
L.SetMetaMethod("__lt", string__lt)
L.SetMetaMethod("__concat", string__concat)
L.SetMetaMethod("__ipairs", string__ipairs)
L.SetMetaMethod("__pairs", string__ipairs)
flagValue()
case cSliceMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", slice__index)
L.SetMetaMethod("__newindex", slice__newindex)
L.SetMetaMethod("__len", slicemap__len)
L.SetMetaMethod("__ipairs", slice__ipairs)
L.SetMetaMethod("__pairs", slice__ipairs)
flagValue()
case cMapMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", map__index)
L.SetMetaMethod("__newindex", map__newindex)
L.SetMetaMethod("__len", slicemap__len)
L.SetMetaMethod("__ipairs", map__ipairs)
L.SetMetaMethod("__pairs", map__pairs)
flagValue()
case cStructMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", struct__index)
L.SetMetaMethod("__newindex", struct__newindex)
flagValue()
case cInterfaceMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", interface__index)
flagValue()
case cChannelMeta:
L.NewMetaTable(proxyMT)
L.SetMetaMethod("__index", channel__index)
flagValue()
}
}
proxymu.Lock()
id := proxyIdCounter
proxyIdCounter++
proxyMap[id] = &valueProxy{v: v, t: v.Type()}
proxymu.Unlock()
L.Pop(1)
rawptr := L.NewUserdata(reflect.TypeOf(id).Size())
*(*uintptr)(rawptr) = id
L.LGetMetaTable(proxyMT)
L.SetMetaTable(-2)
}
func pushGoMethod(L *lua.State, name string, v reflect.Value) {
method := v.MethodByName(name)
if !method.IsValid() {
t := v.Type()
// Could not resolve this method. Perhaps it's defined on the pointer?
if t.Kind() != reflect.Ptr {
if v.CanAddr() {
// If we can get a pointer directly.
v = v.Addr()
} else {
// Otherwise create and initialize one.
vp := reflect.New(t)
vp.Elem().Set(v)
v = vp
}
}
method = v.MethodByName(name)
if !method.IsValid() {
L.PushNil()
return
}
}
GoToLua(L, method)
}
// pushNumberValue pushes the number resulting from an arithmetic operation.
//
// At least one operand must be a proxy for this function to be called. See the
// main documentation for the conversion rules.
func pushNumberValue(L *lua.State, a interface{}, t1, t2 reflect.Type) {
v := reflect.ValueOf(a)
isComplex := unsizedKind(v) == reflect.Complex128
mt := cNumberMeta
if isComplex {
mt = cComplexMeta
}
if t1 == t2 || isPredeclaredType(t2) {
makeValueProxy(L, v.Convert(t1), mt)
} else if isPredeclaredType(t1) {
makeValueProxy(L, v.Convert(t2), mt)
} else if isComplex {
complexType := reflect.TypeOf(0i)
makeValueProxy(L, v.Convert(complexType), cComplexMeta)
} else {
L.PushNumber(valueToNumber(L, v))
}
}
func slicer(L *lua.State, v reflect.Value, metatable string) lua.LuaGoFunction {
return func(L *lua.State) int {
L.CheckInteger(1)
L.CheckInteger(2)
i := L.ToInteger(1) - 1
j := L.ToInteger(2) - 1
if i < 0 || i >= v.Len() || i > j || j > v.Len() {
L.RaiseError("slice bounds out of range")
}
vn := v.Slice(i, j)
makeValueProxy(L, vn, metatable)
return 1
}
}
// Shorthand for kind-switches.
func unsizedKind(v reflect.Value) reflect.Kind {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect.Int64
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return reflect.Uint64
case reflect.Float64, reflect.Float32:
return reflect.Float64
case reflect.Complex128, reflect.Complex64:
return reflect.Complex128
}
return v.Kind()
}
func valueOfProxy(L *lua.State, idx int) (reflect.Value, reflect.Type) {
proxyId := *(*uintptr)(L.ToUserdata(idx))
proxymu.RLock()
val, ok := proxyMap[proxyId]
proxymu.RUnlock()
if !ok {
L.RaiseError(fmt.Sprintf("No value proxy in arg #%d", idx))
}
return val.v, val.t
}
func valueToComplex(L *lua.State, v reflect.Value) complex128 {
if unsizedKind(v) == reflect.Complex128 {
return v.Complex()
}
return complex(valueToNumber(L, v), 0)
}
func valueToNumber(L *lua.State, v reflect.Value) float64 {
switch unsizedKind(v) {
case reflect.Int64:
return float64(v.Int())
case reflect.Uint64:
return float64(v.Uint())
case reflect.Float64:
return v.Float()
case reflect.String:
if f, err := strconv.ParseFloat(v.String(), 64); err == nil {
return f
}
}
L.RaiseError(fmt.Sprintf("cannot convert %#v to number", v))
return 0
}
func valueToString(L *lua.State, v reflect.Value) string {
switch unsizedKind(v) {
case reflect.Uint64, reflect.Int64, reflect.Float64:
return fmt.Sprintf("%v", valueToNumber(L, v))
case reflect.String:
return v.String()
}
L.RaiseError("cannot convert to string")
return ""
}