forked from Shopify/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.go
More file actions
345 lines (328 loc) · 6.91 KB
/
tables.go
File metadata and controls
345 lines (328 loc) · 6.91 KB
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
337
338
339
340
341
342
343
344
345
package lua
import (
"math"
)
type table struct {
array []value
hash map[value]value
metaTable *table
flags byte
iterationKeys []value
iterationKeyIndex map[value]int // key -> index in iterationKeys for O(1) lookup
}
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 {
var i int
switch n := k.(type) {
case int64:
i = int(n)
case float64:
if float64(int(n)) != n {
continue
}
i = int(n)
default:
continue
}
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]
}
// Try int64 key first (Lua 5.3 style), then float64 for backwards compat
if v := t.hash[int64(k)]; v != nil {
return v
}
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 v == nil {
continue
}
var i int
switch n := k.(type) {
case int64:
i = int(n)
case float64:
if float64(int(n)) != n {
continue
}
i = int(n)
default:
continue
}
if i <= key {
occupancy++
}
}
if occupancy >= key>>1 {
t.extendArray(max(occupancy*2, key)) // TODO Tune growth function.
return true
}
return false
}
func (t *table) addOrInsertHash(k, v value) {
if _, ok := t.hash[k]; !ok {
t.iterationKeys = nil // invalidate iterations when adding an entry
t.iterationKeyIndex = nil
}
t.hash[k] = v
}
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 both int64 and float64 keys for backwards compat
delete(t.hash, int64(k))
delete(t.hash, float64(k))
} else {
t.addOrInsertHash(int64(k), v)
}
}
func (t *table) at(k value) value {
switch k := k.(type) {
case nil:
return nil
case int64:
i := int(k)
if 0 < i && i <= len(t.array) {
return t.array[i-1]
}
// Try int64 first, then float64 for backwards compat
if v := t.hash[k]; v != nil {
return v
}
return t.hash[float64(k)]
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]
}
// Try int64 first, then float64 for backwards compat
if v := t.hash[int64(i)]; v != nil {
return v
}
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 int64:
t.putAtInt(int(k), v)
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.addOrInsertHash(k, v)
}
case string:
if v == nil {
delete(t.hash, k)
} else {
t.addOrInsertHash(k, v)
}
default:
if v == nil {
delete(t.hash, k)
} else {
t.addOrInsertHash(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 int64:
i := int(k)
if 0 < i && i <= len(t.array) && t.array[i-1] != nil {
t.array[i-1] = v
return true
} else if t.hash[k] != nil && v != nil {
t.hash[k] = v
return true
}
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 n := k.(type) {
case int64:
return int(n)
case float64:
if i := int(n); float64(i) == n {
return i
}
}
return -1
}
func (l *State) next(t *table, key int) bool {
i, k := 0, l.stack[key]
keyInHash := false
if k == nil { // first iteration
} else if i = arrayIndex(k); 0 < i && i <= len(t.array) {
k = nil
} else if _, ok := t.hash[k]; ok {
keyInHash = true
i = len(t.array)
} else {
// Key not in hash - might have been deleted during iteration
// We'll check iterationKeys below; if not found there, error
i = len(t.array)
}
for ; i < len(t.array); i++ {
if t.array[i] != nil {
l.stack[key] = int64(i + 1)
l.stack[key+1] = t.array[i]
return true
}
}
if t.iterationKeys == nil {
keys := make([]value, len(t.hash))
idx := make(map[value]int, len(t.hash))
j := 0
for hk := range t.hash {
keys[j] = hk
idx[hk] = j
j++
}
t.iterationKeys = keys
t.iterationKeyIndex = idx
}
// Determine starting position in iterationKeys
startPos := 0
if k != nil {
// Look up current key's position via O(1) index map
if pos, ok := t.iterationKeyIndex[k]; ok {
startPos = pos + 1
} else {
// Key not found in index — invalid key
if !keyInHash {
l.runtimeError("invalid key to 'next'")
}
return false
}
}
// Find next valid entry starting from startPos
for j := startPos; j < len(t.iterationKeys); j++ {
hk := t.iterationKeys[j]
if hk == nil {
continue
}
// Check if key was deleted from hash
if _, present := t.hash[hk]; !present {
t.iterationKeys[j] = nil
delete(t.iterationKeyIndex, hk)
continue
}
l.stack[key] = hk
l.stack[key+1] = t.hash[hk]
return true
}
return false // no more elements
}