forked from Shopify/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tables.go
265 lines (246 loc) · 5.02 KB
/
tables.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
package lua
import (
"math"
"strconv"
)
type table struct {
array []value
hash map[value]value
metaTable *table
flags byte
iterationKeys []value
}
func newTable() *table { return &table{hash: make(map[value]value)} }
func (t *table) invalidateTagMethodCache() { t.flags = 0 }
func (t *table) atString(k string) value { return t.hash[k] }
func newTableWithSize(arraySize, hashSize int) *table {
t := new(table)
if arraySize > 0 {
t.array = make([]value, arraySize)
}
if hashSize > 0 {
t.hash = make(map[value]value, hashSize)
} else {
t.hash = make(map[value]value)
}
return t
}
func (l *State) fastTagMethod(table *table, event tm) value {
if table == nil || table.flags&1<<event != 0 {
return nil
}
return table.tagMethod(event, l.global.tagMethodNames[event])
}
func (t *table) extendArray(last int) {
t.array = append(t.array, make([]value, last-len(t.array))...)
for k, v := range t.hash {
if f, ok := k.(float64); ok {
if i := int(f); float64(i) == f {
if 0 < i && i <= len(t.array) {
t.array[i-1] = v
delete(t.hash, k)
}
}
}
}
}
func (t *table) atInt(k int) value {
if 0 < k && k <= len(t.array) {
return t.array[k-1]
}
return t.hash[float64(k)]
}
func (t *table) maybeResizeArray(key int) bool {
// Precondition: key > len(t.array).
occupancy := 0
for _, v := range t.array {
if v != nil {
occupancy++
}
}
for k, v := range t.hash {
if f, ok := k.(float64); ok && v != nil {
if i := int(f); i <= key && float64(i) == f {
occupancy++
}
}
}
if occupancy >= key>>1 {
t.extendArray(max(occupancy*2, key)) // TODO Tune growth function.
return true
}
return false
}
func (t *table) putAtInt(k int, v value) {
if 0 < k && k <= len(t.array) {
t.array[k-1] = v
} else if k > 0 && v != nil && t.maybeResizeArray(k) {
t.array[k-1] = v
} else if v == nil {
delete(t.hash, float64(k))
} else {
t.hash[float64(k)] = v
}
}
func (t *table) at(k value) value {
switch k := k.(type) {
case nil:
return nil
case float64:
if i := int(k); float64(i) == k { // OPT: Inlined copy of atInt.
if 0 < i && i <= len(t.array) {
return t.array[i-1]
}
return t.hash[k]
}
case string:
return t.hash[k]
}
return t.hash[k]
}
func (t *table) put(l *State, k, v value) {
switch k := k.(type) {
case nil:
l.runtimeError("table index is nil")
case float64:
if i := int(k); float64(i) == k {
t.putAtInt(i, v)
} else if math.IsNaN(k) {
l.runtimeError("table index is NaN")
} else if v == nil {
delete(t.hash, k)
} else {
t.hash[k] = v
}
case string:
if v == nil {
delete(t.hash, k)
} else {
t.hash[k] = v
}
default:
if v == nil {
delete(t.hash, k)
} else {
t.hash[k] = v
}
}
}
// OPT: tryPut is an optimized variant of the at/put pair used by setTableAt to avoid hashing the key twice.
func (t *table) tryPut(l *State, k, v value) bool {
switch k := k.(type) {
case nil:
case float64:
if i := int(k); float64(i) == k && 0 < i && i <= len(t.array) && t.array[i-1] != nil {
t.array[i-1] = v
return true
} else if math.IsNaN(k) {
return false
} else if t.hash[k] != nil && v != nil {
t.hash[k] = v
return true
}
case string:
if t.hash[k] != nil && v != nil {
t.hash[k] = v
return true
}
default:
if t.hash[k] != nil && v != nil {
t.hash[k] = v
return true
}
}
return false
}
func (t *table) unboundSearch(j int) int {
i := j
for j++; nil != t.atInt(j); {
i = j
if j *= 2; j < 0 {
for i = 1; nil != t.atInt(i); i++ {
}
return i - 1
}
}
for j-i > 1 {
m := (i + j) / 2
if nil == t.atInt(m) {
j = m
} else {
i = m
}
}
return i
}
func (t *table) length() int {
j := len(t.array)
if j > 0 && t.array[j-1] == nil {
i := 0
for j-i > 1 {
m := (i + j) / 2
if t.array[m-1] == nil {
j = m
} else {
i = m
}
}
return i
} else if t.hash == nil {
return j
}
return t.unboundSearch(j)
}
func arrayIndex(k value) int {
switch k.(type) {
case float64:
if n, ok := k.(float64); ok {
if i := int(n); float64(i) == n {
return i
}
}
case string:
if n, err := strconv.Atoi(k.(string)); err == nil {
return n
}
}
return -1
}
func (l *State) next(t *table, key int) bool {
i, k := 0, l.stack[key]
if k == nil { // first iteration
t.iterationKeys = nil
} else if i = arrayIndex(k); 0 < i && i <= len(t.array) {
} else if _, ok := t.hash[k]; !ok {
l.runtimeError("invalid key to 'next'") // key not found
} else {
i = len(t.array)
}
for ; i < len(t.array); i++ {
if t.array[i] != nil {
l.stack[key] = float64(i + 1)
l.stack[key+1] = t.array[i]
return true
}
}
if t.iterationKeys == nil {
j, keys := 0, make([]value, len(t.hash))
for hk, _ := range t.hash {
keys[j] = hk
j++
}
t.iterationKeys = keys
}
found := k == nil
for _, hk := range t.iterationKeys {
if found {
l.stack[key] = hk
l.stack[key+1] = t.hash[hk]
return true
} else if l.equalObjects(hk, k) {
found = true
}
}
t.iterationKeys = nil
return false // no more elements
}