-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstate_reflect_table.go
165 lines (149 loc) · 5.4 KB
/
state_reflect_table.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
package rbxmk
import (
lua "github.com/anaminus/gopher-lua"
"github.com/robloxapi/types"
)
// PushToTable reflects v according to its type as registered with s.World, then
// sets the result to table[field]. The type must be single-value. Does nothing
// if v is nil.
func (s State) PushToTable(table *lua.LTable, field lua.LValue, v types.Value) {
if err := s.World.PushToTable(table, field, v); err != nil {
s.RaiseError("field %s: %s", field, err.Error())
}
}
// PullFromTable gets a value from table[field], and reflects a value from it to
// type t registered with s.World.
func (s State) PullFromTable(table *lua.LTable, field lua.LValue, t string) (v types.Value) {
v, err := s.World.PullFromTable(table, field, t)
if err != nil {
s.RaiseError("field %s: %s", field, err.Error())
return nil
}
return v
}
// PullFromTableOpt gets a value from table[field], and reflects a value from it
// to type t registered with s.World. If the value is nil, d is returned
// instead.
func (s State) PullFromTableOpt(table *lua.LTable, field lua.LValue, d types.Value, t string) (v types.Value) {
v, err := s.World.PullFromTableOpt(table, field, d, t)
if err != nil {
s.RaiseError("field %s: %s", field, err.Error())
return nil
}
return v
}
// PullAnyFromTable gets a value from table[field], and reflects a value from it
// according to the first successful type from t registered with s.World.
func (s State) PullAnyFromTable(table *lua.LTable, field lua.LValue, t ...string) (v types.Value) {
v, err := s.World.PullAnyFromTable(table, field, t...)
if err != nil {
s.RaiseError("field %s: %s", field, err.Error())
return nil
}
return v
}
// PullAnyFromTableOpt gets a value from table[field], and reflects a value from
// it according to the first successful type from t registered with s.World. If
// the field is nil, then d is returned instead.
func (s State) PullAnyFromTableOpt(table *lua.LTable, field lua.LValue, d types.Value, t ...string) (v types.Value) {
v, err := s.World.PullAnyFromTableOpt(table, field, d, t...)
if err != nil {
s.RaiseError("field %s: %s", field, err.Error())
return nil
}
return v
}
// PushToArray is like PushToTable, but receives an int as the index of the
// table.
func (s State) PushToArray(table *lua.LTable, index int, v types.Value) {
if err := s.World.PushToArray(table, index, v); err != nil {
s.RaiseError("index %d: %s", index, err.Error())
}
}
// PullFromArray is like PullFromTable, but receives an int as the index of the
// table.
func (s State) PullFromArray(table *lua.LTable, index int, t string) (v types.Value) {
v, err := s.World.PullFromArray(table, index, t)
if err != nil {
s.RaiseError("index %d: %s", index, err.Error())
return nil
}
return v
}
// PullFromArrayOpt is like PullFromTableOpt, but receives an int as the index
// of the table.
func (s State) PullFromArrayOpt(table *lua.LTable, index int, d types.Value, t string) (v types.Value) {
v, err := s.World.PullFromArrayOpt(table, index, d, t)
if err != nil {
s.RaiseError("index %d: %s", index, err.Error())
return nil
}
return v
}
// PullAnyFromArray is like PullAnyFromTable, but receives an int as the index
// of the table.
func (s State) PullAnyFromArray(table *lua.LTable, index int, t ...string) (v types.Value) {
v, err := s.World.PullAnyFromArray(table, index, t...)
if err != nil {
s.RaiseError("index %d: %s", index, err.Error())
return nil
}
return v
}
// PullAnyFromArrayOpt is like PullAnyFromTableOpt, but receives an int as the
// index of the table.
func (s State) PullAnyFromArrayOpt(table *lua.LTable, index int, d types.Value, t ...string) (v types.Value) {
v, err := s.World.PullAnyFromArrayOpt(table, index, v)
if err != nil {
s.RaiseError("index %d: %s", index, err.Error())
return nil
}
return v
}
// PushToDictionary is like PushToTable, but receives a string as the key of the
// table.
func (s State) PushToDictionary(table *lua.LTable, key string, v types.Value) {
if err := s.World.PushToDictionary(table, key, v); err != nil {
s.RaiseError("key %s: %s", key, err.Error())
}
}
// PullFromDictionary is like PullFromTable, but receives a string as the key of
// the table.
func (s State) PullFromDictionary(table *lua.LTable, key string, t string) (v types.Value) {
v, err := s.World.PullFromDictionary(table, key, t)
if err != nil {
s.RaiseError("key %s: %s", key, err.Error())
return nil
}
return v
}
// PullFromDictionaryOpt is like PullFromTableOpt, but receives a string as the
// key of the table.
func (s State) PullFromDictionaryOpt(table *lua.LTable, key string, d types.Value, t string) (v types.Value) {
v, err := s.World.PullFromDictionaryOpt(table, key, d, t)
if err != nil {
s.RaiseError("key %s: %s", key, err.Error())
return nil
}
return v
}
// PullAnyFromDictionary is like PullAnyFromTable, but receives a string as the
// key of the table.
func (s State) PullAnyFromDictionary(table *lua.LTable, key string, t ...string) (v types.Value) {
v, err := s.World.PullAnyFromDictionary(table, key, t...)
if err != nil {
s.RaiseError("key %s: %s", key, err.Error())
return nil
}
return v
}
// PullAnyFromDictionaryOpt is like PullAnyFromTableOpt, but receives a string
// as the key of the table.
func (s State) PullAnyFromDictionaryOpt(table *lua.LTable, key string, d types.Value, t ...string) (v types.Value) {
v, err := s.World.PullAnyFromDictionaryOpt(table, key, v)
if err != nil {
s.RaiseError("key %s: %s", key, err.Error())
return nil
}
return v
}