forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
387 lines (361 loc) · 8.09 KB
/
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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package lua
const defaultArrayCap = 32
const defaultHashCap = 32
type lValueArraySorter struct {
L *LState
Fn *LFunction
Values []LValue
}
func (lv lValueArraySorter) Len() int {
return len(lv.Values)
}
func (lv lValueArraySorter) Swap(i, j int) {
lv.Values[i], lv.Values[j] = lv.Values[j], lv.Values[i]
}
func (lv lValueArraySorter) Less(i, j int) bool {
if lv.Fn != nil {
lv.L.Push(lv.Fn)
lv.L.Push(lv.Values[i])
lv.L.Push(lv.Values[j])
lv.L.Call(2, 1)
return LVAsBool(lv.L.reg.Pop())
}
return lessThan(lv.L, lv.Values[i], lv.Values[j])
}
func newLTable(acap int, hcap int) *LTable {
if acap < 0 {
acap = 0
}
if hcap < 0 {
hcap = 0
}
tb := <able{}
tb.Metatable = LNil
if acap != 0 {
tb.array = make([]LValue, 0, acap)
}
if hcap != 0 {
tb.strdict = make(map[string]LValue, hcap)
}
return tb
}
// Len returns length of this LTable without using __len.
func (tb *LTable) Len() int {
if tb.array == nil {
return 0
}
var prev LValue = LNil
for i := len(tb.array) - 1; i >= 0; i-- {
v := tb.array[i]
if prev == LNil && v != LNil {
return i + 1
}
prev = v
}
return 0
}
// Append appends a given LValue to this LTable.
func (tb *LTable) Append(value LValue) {
if value == LNil {
return
}
if tb.array == nil {
tb.array = make([]LValue, 0, defaultArrayCap)
}
if len(tb.array) == 0 || tb.array[len(tb.array)-1] != LNil {
tb.array = append(tb.array, value)
} else {
i := len(tb.array) - 2
for ; i >= 0; i-- {
if tb.array[i] != LNil {
break
}
}
tb.array[i+1] = value
}
}
// Insert inserts a given LValue at position `i` in this table.
func (tb *LTable) Insert(i int, value LValue) {
if tb.array == nil {
tb.array = make([]LValue, 0, defaultArrayCap)
}
if i > len(tb.array) {
tb.RawSetInt(i, value)
return
}
if i <= 0 {
tb.RawSet(LNumber(i), value)
return
}
i -= 1
tb.array = append(tb.array, LNil)
copy(tb.array[i+1:], tb.array[i:])
tb.array[i] = value
}
// MaxN returns a maximum number key that nil value does not exist before it.
func (tb *LTable) MaxN() int {
if tb.array == nil {
return 0
}
for i := len(tb.array) - 1; i >= 0; i-- {
if tb.array[i] != LNil {
return i + 1
}
}
return 0
}
// Remove removes from this table the element at a given position.
func (tb *LTable) Remove(pos int) LValue {
if tb.array == nil {
return LNil
}
larray := len(tb.array)
if larray == 0 {
return LNil
}
i := pos - 1
oldval := LNil
switch {
case i >= larray:
// nothing to do
case i == larray-1 || i < 0:
oldval = tb.array[larray-1]
tb.array = tb.array[:larray-1]
default:
oldval = tb.array[i]
copy(tb.array[i:], tb.array[i+1:])
tb.array[larray-1] = nil
tb.array = tb.array[:larray-1]
}
return oldval
}
// RawSet sets a given LValue to a given index without the __newindex metamethod.
// It is recommended to use `RawSetString` or `RawSetInt` for performance
// if you already know the given LValue is a string or number.
func (tb *LTable) RawSet(key LValue, value LValue) {
switch v := key.(type) {
case LNumber:
if isArrayKey(v) {
if tb.array == nil {
tb.array = make([]LValue, 0, defaultArrayCap)
}
index := int(v) - 1
alen := len(tb.array)
switch {
case index == alen:
tb.array = append(tb.array, value)
case index > alen:
for i := 0; i < (index - alen); i++ {
tb.array = append(tb.array, LNil)
}
tb.array = append(tb.array, value)
case index < alen:
tb.array[index] = value
}
return
}
case LString:
tb.RawSetString(string(v), value)
return
}
tb.RawSetH(key, value)
}
// RawSetInt sets a given LValue at a position `key` without the __newindex metamethod.
func (tb *LTable) RawSetInt(key int, value LValue) {
if key < 1 || key >= MaxArrayIndex {
tb.RawSetH(LNumber(key), value)
return
}
if tb.array == nil {
tb.array = make([]LValue, 0, 32)
}
index := key - 1
alen := len(tb.array)
switch {
case index == alen:
tb.array = append(tb.array, value)
case index > alen:
for i := 0; i < (index - alen); i++ {
tb.array = append(tb.array, LNil)
}
tb.array = append(tb.array, value)
case index < alen:
tb.array[index] = value
}
}
// RawSetString sets a given LValue to a given string index without the __newindex metamethod.
func (tb *LTable) RawSetString(key string, value LValue) {
if tb.strdict == nil {
tb.strdict = make(map[string]LValue, defaultHashCap)
}
if tb.keys == nil {
tb.keys = []LValue{}
tb.k2i = map[LValue]int{}
}
if value == LNil {
// TODO tb.keys and tb.k2i should also be removed
delete(tb.strdict, key)
} else {
tb.strdict[key] = value
lkey := LString(key)
if _, ok := tb.k2i[lkey]; !ok {
tb.k2i[lkey] = len(tb.keys)
tb.keys = append(tb.keys, lkey)
}
}
}
// RawSetH sets a given LValue to a given index without the __newindex metamethod.
func (tb *LTable) RawSetH(key LValue, value LValue) {
if s, ok := key.(LString); ok {
tb.RawSetString(string(s), value)
return
}
if tb.dict == nil {
tb.dict = make(map[LValue]LValue, len(tb.strdict))
}
if tb.keys == nil {
tb.keys = []LValue{}
tb.k2i = map[LValue]int{}
}
if value == LNil {
// TODO tb.keys and tb.k2i should also be removed
delete(tb.dict, key)
} else {
tb.dict[key] = value
if _, ok := tb.k2i[key]; !ok {
tb.k2i[key] = len(tb.keys)
tb.keys = append(tb.keys, key)
}
}
}
// RawGet returns an LValue associated with a given key without __index metamethod.
func (tb *LTable) RawGet(key LValue) LValue {
switch v := key.(type) {
case LNumber:
if isArrayKey(v) {
if tb.array == nil {
return LNil
}
index := int(v) - 1
if index >= len(tb.array) {
return LNil
}
return tb.array[index]
}
case LString:
if tb.strdict == nil {
return LNil
}
if ret, ok := tb.strdict[string(v)]; ok {
return ret
}
return LNil
}
if tb.dict == nil {
return LNil
}
if v, ok := tb.dict[key]; ok {
return v
}
return LNil
}
// RawGetInt returns an LValue at position `key` without __index metamethod.
func (tb *LTable) RawGetInt(key int) LValue {
if tb.array == nil {
return LNil
}
index := int(key) - 1
if index >= len(tb.array) || index < 0 {
return LNil
}
return tb.array[index]
}
// RawGet returns an LValue associated with a given key without __index metamethod.
func (tb *LTable) RawGetH(key LValue) LValue {
if s, sok := key.(LString); sok {
if tb.strdict == nil {
return LNil
}
if v, vok := tb.strdict[string(s)]; vok {
return v
}
return LNil
}
if tb.dict == nil {
return LNil
}
if v, ok := tb.dict[key]; ok {
return v
}
return LNil
}
// RawGetString returns an LValue associated with a given key without __index metamethod.
func (tb *LTable) RawGetString(key string) LValue {
if tb.strdict == nil {
return LNil
}
if v, vok := tb.strdict[string(key)]; vok {
return v
}
return LNil
}
// ForEach iterates over this table of elements, yielding each in turn to a given function.
func (tb *LTable) ForEach(cb func(LValue, LValue)) {
if tb.array != nil {
for i, v := range tb.array {
if v != LNil {
cb(LNumber(i+1), v)
}
}
}
if tb.strdict != nil {
for k, v := range tb.strdict {
if v != LNil {
cb(LString(k), v)
}
}
}
if tb.dict != nil {
for k, v := range tb.dict {
if v != LNil {
cb(k, v)
}
}
}
}
// This function is equivalent to lua_next ( http://www.lua.org/manual/5.1/manual.html#lua_next ).
func (tb *LTable) Next(key LValue) (LValue, LValue) {
init := false
if key == LNil {
key = LNumber(0)
init = true
}
if init || key != LNumber(0) {
if kv, ok := key.(LNumber); ok && isInteger(kv) && int(kv) >= 0 && kv < LNumber(MaxArrayIndex) {
index := int(kv)
if tb.array != nil {
for ; index < len(tb.array); index++ {
if v := tb.array[index]; v != LNil {
return LNumber(index + 1), v
}
}
}
if tb.array == nil || index == len(tb.array) {
if (tb.dict == nil || len(tb.dict) == 0) && (tb.strdict == nil || len(tb.strdict) == 0) {
return LNil, LNil
}
key = tb.keys[0]
if v := tb.RawGetH(key); v != LNil {
return key, v
}
}
}
}
for i := tb.k2i[key] + 1; i < len(tb.keys); i++ {
key := tb.keys[i]
if v := tb.RawGetH(key); v != LNil {
return key, v
}
}
return LNil, LNil
}