-
Notifications
You must be signed in to change notification settings - Fork 11
/
world_reflect.go
245 lines (228 loc) · 6.38 KB
/
world_reflect.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
package rbxmk
import (
"fmt"
"strings"
lua "github.com/anaminus/gopher-lua"
"github.com/anaminus/rbxmk/rtypes"
"github.com/robloxapi/types"
)
// Typeof returns the type of the given Lua value. If it is a userdata, Typeof
// attempts to get the type according to the value's metatable. Panics if v is
// nil (not if nil Lua value).
func (w *World) Typeof(v lua.LValue) string {
if v == nil {
panic("value expected")
}
u, ok := v.(*lua.LUserData)
if !ok {
return v.Type().String()
}
t, ok := w.l.GetMetaField(u, "__type").(lua.LString)
if !ok {
return u.Type().String()
}
return string(t)
}
// Push reflects v to lv.
func (w *World) Push(v types.Value) (lv lua.LValue, err error) {
push, err := w.PusherOf(v.Type())
if err != nil {
return nil, err
}
return push(w.Context(), v)
}
// Pull reflects lv to v using registered type t.
func (w *World) Pull(lv lua.LValue, t string) (v types.Value, err error) {
pull, err := w.PullerOf(t)
if err != nil {
return nil, err
}
return pull(w.Context(), lv)
}
// PullOpt reflects lv to v using registered type t. If lv is nil, then d is
// returned instead.
func (w *World) PullOpt(lv lua.LValue, d types.Value, t string) (v types.Value, err error) {
if lv == nil || lv.Type() == lua.LTNil {
return d, nil
}
return w.Pull(lv, t)
}
// ListTypes returns each type listed in a natural sentence.
func (w *World) ListTypes(types []string) string {
switch len(types) {
case 0:
return ""
case 1:
return types[0]
case 2:
return types[0] + " or " + types[1]
}
return strings.Join(types[:len(types)-2], ", ") + ", or " + types[len(types)-1]
}
// PullAnyOf reflects lv to the first successful type from t. Returns an error
// if none of the types were successful.
func (w *World) PullAnyOf(lv lua.LValue, t ...string) (v types.Value, err error) {
for _, t := range t {
pull, err := w.PullerOf(t)
if err != nil {
return nil, err
}
if v, err := pull(w.Context(), lv); err == nil {
return v, nil
}
}
return nil, TypeError{Want: w.ListTypes(t), Got: w.Typeof(lv)}
}
// PullAnyOfOpt reflects lv to the first successful type from t. Returns d if
// none of the types were successful.
func (w *World) PullAnyOfOpt(lv lua.LValue, d types.Value, t ...string) (v types.Value) {
for _, t := range t {
pull, err := w.PullerOf(t)
if err != nil {
return d
}
if v, err := pull(w.Context(), lv); err == nil {
return v
}
}
return d
}
// PushArrayOf reflect v to lv, ensuring that each element is reflected
// according to t.
func (w *World) PushArrayOf(v rtypes.Array, t string) (lv *lua.LTable, err error) {
s := w.Context()
if s.CycleGuard() {
defer s.CycleClear()
}
if s.CycleMark(&v) {
return nil, fmt.Errorf("arrays cannot be cyclic")
}
push, err := w.PusherOf(t)
if err != nil {
return nil, err
}
table := w.l.CreateTable(len(v), 0)
for _, v := range v {
lv, err := push(s, v)
if err != nil {
return nil, err
}
table.Append(lv)
}
return table, nil
}
// PullArrayOf reflects lv to v, ensuring that lv is a table, and that each
// element is reflected according to t.
func (w *World) PullArrayOf(lv lua.LValue, t string) (v rtypes.Array, err error) {
pull, err := w.PullerOf(t)
if err != nil {
return nil, err
}
s := w.Context()
if s.CycleGuard() {
defer s.CycleClear()
}
table, ok := lv.(*lua.LTable)
if !ok {
return nil, TypeError{Want: rtypes.T_LuaTable, Got: lv.Type().String()}
}
if s.CycleMark(table) {
return nil, fmt.Errorf("tables cannot be cyclic")
}
l := table.Len()
array := make(rtypes.Array, l)
for i := 1; i <= l; i++ {
var err error
if array[i-1], err = pull(s, table.RawGetInt(i)); err != nil {
return nil, err
}
}
return array, nil
}
// PushDictionaryOf reflect v to lv, ensuring that each field is reflected
// according to t.
func (w *World) PushDictionaryOf(v rtypes.Dictionary, t string) (lv *lua.LTable, err error) {
s := w.Context()
if s.CycleGuard() {
defer s.CycleClear()
}
if s.CycleMark(&v) {
return nil, fmt.Errorf("dictionaries cannot be cyclic")
}
push, err := w.PusherOf(t)
if err != nil {
return nil, err
}
table := w.l.CreateTable(0, len(v))
for k, v := range v {
lv, err := push(s, v)
if err != nil {
return nil, err
}
table.RawSetString(k, lv)
}
return table, nil
}
// PullDictionaryOf reflects lv to v, ensuring that lv is a table, and that each
// string field is reflected according to t.
func (w *World) PullDictionaryOf(lv lua.LValue, t string) (v rtypes.Dictionary, err error) {
pull, err := w.PullerOf(t)
if err != nil {
return nil, err
}
s := w.Context()
if s.CycleGuard() {
defer s.CycleClear()
}
table, ok := lv.(*lua.LTable)
if !ok {
return nil, TypeError{Want: rtypes.T_LuaTable, Got: lv.Type().String()}
}
if s.CycleMark(table) {
return nil, fmt.Errorf("tables cannot be cyclic")
}
dict := make(rtypes.Dictionary)
err = table.ForEach(func(k, lv lua.LValue) error {
v, err := pull(s, lv)
if err != nil {
return err
}
dict[k.String()] = v
return nil
})
if err != nil {
return nil, err
}
return dict, nil
}
// PullEncoded pulls a value to be encoded according to a FormatSelector. The
// referred format is acquired, then Format.EncodeTypes is used to reflect the
// value from lv. If fs.Format is empty, or if EncodeTypes is empty, then the
// value is pulled as a Variant.
func (w *World) PullEncoded(lv lua.LValue, fs rtypes.FormatSelector) (v types.Value, err error) {
if fs.Format == "" {
return w.Pull(lv, "Variant")
}
format := w.Format(fs.Format)
if format.Name == "" {
return nil, fmt.Errorf("unknown format %q", fs.Format)
}
return w.PullEncodedFormat(lv, w.Format(fs.Format))
}
// PullEncodedFormat pulls a value to be encoded according to f.
// Format.EncodeTypes is used to reflect the value from lv. If EncodeTypes is
// empty, then the value is pulled as a Variant.
func (w *World) PullEncodedFormat(lv lua.LValue, f Format) (v types.Value, err error) {
if len(f.EncodeTypes) == 0 {
return w.Pull(lv, "Variant")
}
return w.PullAnyOf(lv, f.EncodeTypes...)
}
// PullEncodedFromDict is like PullEncoded, but the value is pulled from a
// Dictionary.
func (w *World) PullEncodedFromDict(table *lua.LTable, field string, format rtypes.FormatSelector) (v types.Value, err error) {
if format := w.Format(format.Format); format.Name != "" && len(format.EncodeTypes) > 0 {
return w.PullAnyFromDictionaryOpt(table, field, nil, format.EncodeTypes...)
}
return w.PullFromDictionaryOpt(table, field, nil, "Variant")
}