forked from stevedonovan/luar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyfuncs.go
258 lines (233 loc) · 5.93 KB
/
proxyfuncs.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
package luar
// Those functions are meant to be registered in Lua to manipulate proxies.
import (
"reflect"
"github.com/dnestorov/golua/lua"
)
// ArrayToTable defines 'luar.array2table' when 'Init' is called.
//
// WARNING: Deprecated, use luar.unproxify instead.
func ArrayToTable(L *lua.State) int {
return CopyArrayToTable(L, reflect.ValueOf(mustUnwrapProxy(L, 1)))
}
// Complex pushes a proxy to a Go complex on the stack.
//
// Arguments: real (number), imag (number)
//
// Returns: proxy (complex128)
func Complex(L *lua.State) int {
v1, _ := luaToGoValue(L, 1)
v2, _ := luaToGoValue(L, 2)
result := complex(valueToNumber(L, v1), valueToNumber(L, v2))
makeValueProxy(L, reflect.ValueOf(result), cComplexMeta)
return 1
}
// ComplexReal defines 'luar.real' when 'Init' is called.
// It is the equivalent of Go's 'real' function.
//
// WARNING: Deprecated, use the 'real' index instead.
func ComplexReal(L *lua.State) int {
v := mustUnwrapProxy(L, 1)
val := reflect.ValueOf(v)
if unsizedKind(val) != reflect.Complex128 {
RaiseError(L, "not a complex")
}
L.PushNumber(real(val.Complex()))
return 1
}
// ComplexImag defines 'luar.imag' when 'Init' is called.
// It is the equivalent of Go's 'imag' function.
//
// WARNING: Deprecated, use the 'imag' index instead.
func ComplexImag(L *lua.State) int {
v := mustUnwrapProxy(L, 1)
val := reflect.ValueOf(v)
if unsizedKind(val) != reflect.Complex128 {
RaiseError(L, "not a complex")
}
L.PushNumber(imag(val.Complex()))
return 1
}
// MakeChan creates a 'chan interface{}' proxy and pushes it on the stack.
//
// Optional argument: size (number)
//
// Returns: proxy (chan interface{})
func MakeChan(L *lua.State) int {
n := L.OptInteger(1, 0)
ch := make(chan interface{}, n)
makeValueProxy(L, reflect.ValueOf(ch), cChannelMeta)
return 1
}
// MakeMap creates a 'map[string]interface{}' proxy and pushes it on the stack.
//
// Returns: proxy (map[string]interface{})
func MakeMap(L *lua.State) int {
m := reflect.MakeMap(tmap)
makeValueProxy(L, m, cMapMeta)
return 1
}
// MakeSlice creates a '[]interface{}' proxy and pushes it on the stack.
//
// Optional argument: size (number)
//
// Returns: proxy ([]interface{})
func MakeSlice(L *lua.State) int {
n := L.OptInteger(1, 0)
s := reflect.MakeSlice(tslice, n, n+1)
makeValueProxy(L, s, cSliceMeta)
return 1
}
// MapToTable defines 'luar.map2table' when 'Init' is called.
//
// WARNING: Deprecated, use luar.unproxify instead.
func MapToTable(L *lua.State) int {
return CopyMapToTable(L, reflect.ValueOf(mustUnwrapProxy(L, 1)))
}
func ipairsAux(L *lua.State) int {
i := L.CheckInteger(2) + 1
L.PushInteger(int64(i))
L.PushInteger(int64(i))
L.GetTable(1)
if L.Type(-1) == lua.LUA_TNIL {
return 1
}
return 2
}
// ProxyIpairs implements Lua 5.2 'ipairs' functions.
// It respects the __ipairs metamethod.
//
// It is only useful for compatibility with Lua 5.1.
func ProxyIpairs(L *lua.State) int {
// See Lua >=5.2 source code.
if L.GetMetaField(1, "__ipairs") {
L.PushValue(1)
L.Call(1, 3)
return 3
}
L.CheckType(1, lua.LUA_TTABLE)
L.PushGoFunction(ipairsAux)
L.PushValue(1)
L.PushInteger(0)
return 3
}
// ProxyMethod pushes the proxy method on the stack.
//
// Argument: proxy
//
// Returns: method (function)
func ProxyMethod(L *lua.State) int {
if !isValueProxy(L, 1) {
L.PushNil()
return 1
}
v, _ := valueOfProxy(L, 1)
name := L.ToString(2)
pushGoMethod(L, name, v)
return 1
}
func pairsAux(L *lua.State) int {
L.CheckType(1, lua.LUA_TTABLE)
L.SetTop(2) // Create a 2nd argument if there isn't one.
if L.Next(1) != 0 {
return 2
}
L.PushNil()
return 1
}
// ProxyPairs implements Lua 5.2 'pairs' functions.
// It respects the __pairs metamethod.
//
// It is only useful for compatibility with Lua 5.1.
func ProxyPairs(L *lua.State) int {
// See Lua >=5.2 source code.
if L.GetMetaField(1, "__pairs") {
L.PushValue(1)
L.Call(1, 3)
return 3
}
L.CheckType(1, lua.LUA_TTABLE)
L.PushGoFunction(pairsAux)
L.PushValue(1)
L.PushNil()
return 3
}
// ProxyRaw unproxifies a value.
//
// WARNING: Deprecated, use luar.unproxify instead.
func ProxyRaw(L *lua.State) int {
v := mustUnwrapProxy(L, 1)
val := reflect.ValueOf(v)
tp := predeclaredScalarType(val.Type())
if tp != nil {
val = val.Convert(tp)
GoToLua(L, nil, val, false)
} else {
L.PushNil()
}
return 1
}
// ProxyType pushes the proxy type on the stack.
//
// Argument: proxy
//
// Returns: type (string)
func ProxyType(L *lua.State) int {
if !isValueProxy(L, 1) {
L.PushNil()
return 1
}
v, _ := valueOfProxy(L, 1)
if v.Interface() == nil {
L.PushNil()
return 1
}
GoToLua(L, nil, reflect.ValueOf(v.Type()), false)
return 1
}
// SliceAppend defines 'luar.append' when 'Init' is called.
//
// WARNING: Deprecated, use the 'append' method instead.
func SliceAppend(L *lua.State) int {
slice, _ := valueOfProxy(L, 1)
val := reflect.ValueOf(LuaToGo(L, nil, 2))
newslice := reflect.Append(slice, val)
makeValueProxy(L, newslice, cSliceMeta)
return 1
}
// SliceSub defines 'luar.sub' when 'Init' is called.
//
// WARNING: Deprecated, use the 'sub' method instead.
func SliceSub(L *lua.State) int {
slice, _ := valueOfProxy(L, 1)
i1, i2 := L.ToInteger(2), L.ToInteger(3)
newslice := slice.Slice(i1-1, i2)
makeValueProxy(L, newslice, cSliceMeta)
return 1
}
// SliceToTable defines 'luar.slice2table' when 'Init' is called.
//
// WARNING: Deprecated, use luar.unproxify instead.
func SliceToTable(L *lua.State) int {
return CopySliceToTable(L, reflect.ValueOf(mustUnwrapProxy(L, 1)))
}
// StructToTable defines 'luar.struct2table' when 'Init' is called.
//
// WARNING: Deprecated, use luar.unproxify instead.
func StructToTable(L *lua.State) int {
return CopyStructToTable(L, reflect.ValueOf(mustUnwrapProxy(L, 1)))
}
// Unproxify converts a proxy to an unproxified Lua value.
//
// Argument: proxy
//
// Returns: value (Lua value)
func Unproxify(L *lua.State) int {
if !isValueProxy(L, 1) {
L.PushNil()
return 1
}
v, _ := valueOfProxy(L, 1)
GoToLua(L, nil, v, true)
return 1
}