-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable-tools.go
More file actions
90 lines (78 loc) · 2.33 KB
/
table-tools.go
File metadata and controls
90 lines (78 loc) · 2.33 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
package luajit
import (
"log"
)
// beware : ToString() could modify value, which can confuses next call to lua_next().
func (L *State) _table_iterate(tableIndex Index, fn func()) {
tableIndex = L.abs_index(tableIndex)
L.pushNil()
for L.Y_lua_next(tableIndex) != 0 {
fn()
L.Pop(1) // remove value
}
return
}
// TableGet pushes (or create) _G[tableName] on the stack, which must be a table.
func (L *State) TableGet(tableName any) {
L.GetGlobal(tableName)
switch L.Type(-1) {
case LUA_TTABLE:
return
case LUA_TNIL:
L.Pop(1)
L.NewTable()
L.pushvalue(-1) // keep a backup to return it
L.SetGlobal(tableName) // will Pop the table
return
default:
log.Fatalf("TableGet() : already not a table")
}
}
func (L *State) _table_get_fatal(tableName any) {
L.GetGlobal(tableName) // table
if L.IsTable(-1) == false {
log.Fatalf("_table_get_fatal(%s) : not a table", tableName)
}
}
// TableGetField pushes `_G[tableName][k]` onto the stack.
func (L *State) TableGetField(tableName any, k any) {
L._table_get_fatal(tableName) // table
L.GetField(-1, k) // table,value
L.remove(-2) // value
}
// TableSetAny does `_G[tableName][k] = v` where `k` is at top.
// Does not exist in Lua.
// As it would in Lua, pops the top of the stack (`k`).
func (L *State) TableSetAny(tableName, v any) {
L._table_get_fatal(tableName) // table
L.insert(-2) // table,key
L.SetTableAny(-2, v) // table
L.Pop(1) // <empty>
}
// TableSetField does `_G[tableName][k] = v` where `v` is at top.
// As in Lua, pops the top of the stack (`v`).
func (L *State) TableSetField(tableName, k any) {
L._table_get_fatal(tableName) // table
L.insert(-2) // table,value
L.SetField(-2, k) // table
L.Pop(1) // <empty>
}
// TableSetFieldAny does `_G[tableName][k] = v`
// Does not modify the stack.
func (L *State) TableSetFieldAny(tableName, k any, v any) {
L._table_get_fatal(tableName) // table
L.SetFieldAny(-1, k, v) // table
L.Pop(1) // <empty>
}
func (L *State) TableGetKeysAny() (ret []any) {
L._table_iterate(-1, func() {
ret = append(ret, L.ToAny(-1))
})
return
}
func (L *State) TableGetKeysString() (ret []string) {
L._table_iterate(-1, func() {
ret = append(ret, L.ToStringSafe(-1))
})
return
}