-
Notifications
You must be signed in to change notification settings - Fork 11
/
state.go
336 lines (304 loc) · 7.87 KB
/
state.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
package rbxmk
import (
"fmt"
lua "github.com/anaminus/gopher-lua"
"github.com/anaminus/rbxmk/rtypes"
"github.com/robloxapi/types"
)
// FrameType indicates the kind of frame for a State.
type FrameType uint8
const (
// Frame is a regular function.
FunctionFrame FrameType = iota
// Frame is a method; exclude first argument.
MethodFrame
// Frame is an operator, avoid displaying arguments.
OperatorFrame
)
// State facilitates the reflection of values to a particular Lua state.
type State struct {
*World
L *lua.LState
// FrameType provides a hint to how errors should be produced.
FrameType FrameType
}
// Count returns the number of arguments in the stack frame.
func (s State) Count() int {
return s.L.GetTop()
}
// ReflectorError raises an error indicating that a reflector pushed or pulled
// an unexpected type. Under normal circumstances, this error should be
// unreachable.
func (s State) ReflectorError(n int) int {
if n <= 0 {
return s.RaiseError("unreachable error: reflector mismatch")
}
return s.ArgError(n, "unreachable error: reflector mismatch")
}
// PushTuple pushes each value.
func (s State) PushTuple(values ...types.Value) int {
lvs := make([]lua.LValue, len(values))
for i, value := range values {
rfl := s.MustReflector(value.Type())
lv, err := rfl.PushTo(s.Context(), value)
if err != nil {
return s.RaiseError("%s", err)
}
lvs[i] = lv
}
for _, lv := range lvs {
s.L.Push(lv)
}
return len(lvs)
}
// PullTuple pulls each value starting from n as a Variant.
func (s State) PullTuple(n int) rtypes.Tuple {
c := s.Count()
length := c - n + 1
if length <= 0 {
return nil
}
rfl := s.MustReflector("Variant")
vs := make(rtypes.Tuple, length)
for i := n; i <= c; i++ {
lv := s.L.Get(i)
v, err := rfl.PullFrom(s.Context(), lv)
if err != nil {
s.ArgError(i, err.Error())
return nil
}
vs[i-n] = v
}
return vs
}
// RaiseError is a shortcut for LState.RaiseError that returns 0.
func (s State) RaiseError(format string, args ...interface{}) int {
s.L.RaiseError(format, args...)
return 0
}
// ArgError raises an argument error depending on the state's frame type.
func (s State) ArgError(n int, msg string, v ...interface{}) int {
if len(v) > 0 {
msg = fmt.Sprintf(msg, v...)
}
switch s.FrameType {
case MethodFrame:
if n <= 1 {
s.RaiseError("bad method receiver: %s", msg)
} else {
s.L.ArgError(n-1, msg)
}
case OperatorFrame:
s.RaiseError(msg)
default:
s.L.ArgError(n, msg)
}
return 0
}
// TypeError raises an argument type error depending on the state's frame type.
func (s State) TypeError(n int, want, got string) int {
err := TypeError{Want: want, Got: got}
switch s.FrameType {
case MethodFrame:
if n <= 1 {
s.RaiseError("bad method receiver: %s", err)
} else {
s.L.ArgError(n-1, err.Error())
}
case OperatorFrame:
s.RaiseError("%s", err.Error())
default:
s.L.ArgError(n, err.Error())
}
return 0
}
// CheckAny returns the nth argument, which can be any type as long as the
// argument exists.
func (s State) CheckAny(n int) lua.LValue {
if n > s.Count() {
s.ArgError(n, "value expected")
return nil
}
return s.L.Get(n)
}
// CheckBool returns the nth argument, expecting a boolean.
func (s State) CheckBool(n int) bool {
v := s.L.Get(n)
if lv, ok := v.(lua.LBool); ok {
return bool(lv)
}
s.TypeError(n, lua.LTBool.String(), v.Type().String())
return false
}
// CheckInt returns the nth argument as an int, expecting a number.
func (s State) CheckInt(n int) int {
v := s.L.Get(n)
if lv, ok := v.(lua.LNumber); ok {
return int(lv)
}
s.TypeError(n, lua.LTNumber.String(), v.Type().String())
return 0
}
// CheckInt64 returns the nth argument as an int64, expecting a number.
func (s State) CheckInt64(n int) int64 {
v := s.L.Get(n)
if lv, ok := v.(lua.LNumber); ok {
return int64(lv)
}
s.TypeError(n, lua.LTNumber.String(), v.Type().String())
return 0
}
// CheckNumber returns the nth argument, expecting a number.
func (s State) CheckNumber(n int) lua.LNumber {
v := s.L.Get(n)
if lv, ok := v.(lua.LNumber); ok {
return lv
}
s.TypeError(n, lua.LTNumber.String(), v.Type().String())
return 0
}
// CheckString returns the nth argument, expecting a string. Unlike
// LState.CheckString, it does not try to convert non-string values into a
// string.
func (s State) CheckString(n int) string {
v := s.L.Get(n)
if lv, ok := v.(lua.LString); ok {
return string(lv)
}
s.TypeError(n, lua.LTString.String(), v.Type().String())
return ""
}
// CheckTable returns the nth argument, expecting a table.
func (s State) CheckTable(n int) *lua.LTable {
v := s.L.Get(n)
if lv, ok := v.(*lua.LTable); ok {
return lv
}
s.TypeError(n, lua.LTTable.String(), v.Type().String())
return nil
}
// CheckFunction returns the nth argument, expecting a function.
func (s State) CheckFunction(n int) *lua.LFunction {
v := s.L.Get(n)
if lv, ok := v.(*lua.LFunction); ok {
return lv
}
s.TypeError(n, lua.LTFunction.String(), v.Type().String())
return nil
}
// CheckUserData returns the nth argument, expecting a userdata.
func (s State) CheckUserData(n int) *lua.LUserData {
v := s.L.Get(n)
if lv, ok := v.(*lua.LUserData); ok {
return lv
}
s.TypeError(n, lua.LTUserData.String(), v.Type().String())
return nil
}
// CheckThread returns the nth argument, expecting a thread.
func (s State) CheckThread(n int) *lua.LState {
v := s.L.Get(n)
if lv, ok := v.(*lua.LState); ok {
return lv
}
s.TypeError(n, lua.LTThread.String(), v.Type().String())
return nil
}
// OptBool returns the nth argument as a bool, or d if the argument is nil.
func (s State) OptBool(n int, d bool) bool {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(lua.LBool); ok {
return bool(lv)
}
s.TypeError(n, lua.LTBool.String(), v.Type().String())
return false
}
// OptInt returns the nth argument as an int, or d if the argument is nil.
func (s State) OptInt(n int, d int) int {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(lua.LNumber); ok {
return int(lv)
}
s.TypeError(n, lua.LTNumber.String(), v.Type().String())
return 0
}
// OptInt64 returns the nth argument as an int64, or d if the argument is nil.
func (s State) OptInt64(n int, d int64) int64 {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(lua.LNumber); ok {
return int64(lv)
}
s.TypeError(n, lua.LTNumber.String(), v.Type().String())
return 0
}
// OptNumber returns the nth argument as a number, or d if the argument is nil.
func (s State) OptNumber(n int, d lua.LNumber) lua.LNumber {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(lua.LNumber); ok {
return lv
}
s.TypeError(n, lua.LTNumber.String(), v.Type().String())
return 0
}
// OptString returns the nth argument as a string, or d if the argument is nil.
func (s State) OptString(n int, d string) string {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(lua.LString); ok {
return string(lv)
}
s.TypeError(n, lua.LTString.String(), v.Type().String())
return ""
}
// OptTable returns the nth argument as a table, or d if the argument is nil.
func (s State) OptTable(n int, d *lua.LTable) *lua.LTable {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(*lua.LTable); ok {
return lv
}
s.TypeError(n, lua.LTTable.String(), v.Type().String())
return nil
}
// OptFunction returns the nth argument as a function, or d if the argument is
// nil.
func (s State) OptFunction(n int, d *lua.LFunction) *lua.LFunction {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(*lua.LFunction); ok {
return lv
}
s.TypeError(n, lua.LTFunction.String(), v.Type().String())
return nil
}
// OptUserData returns the nth argument as a userdata, or d if the argument is
// nil.
func (s State) OptUserData(n int, d *lua.LUserData) *lua.LUserData {
v := s.L.Get(n)
if v == lua.LNil {
return d
}
if lv, ok := v.(*lua.LUserData); ok {
return lv
}
s.TypeError(n, lua.LTUserData.String(), v.Type().String())
return nil
}