-
Notifications
You must be signed in to change notification settings - Fork 10
/
table.go
75 lines (60 loc) · 1.45 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
package runtime
import (
"unsafe"
"github.com/arnodel/golua/runtime/internal/luagc"
)
// Table implements a Lua table.
type Table struct {
// This is where the implementation details are.
*mixedTable
meta *Table
}
// NewTable returns a new Table.
func NewTable() *Table {
return &Table{mixedTable: &mixedTable{}}
}
// Metatable returns the table's metatable.
func (t *Table) Metatable() *Table {
return t.meta
}
// SetMetatable sets the table's metatable.
func (t *Table) SetMetatable(m *Table) {
t.meta = m
}
var _ luagc.Value = (*Table)(nil)
func (t *Table) Key() luagc.Key {
return unsafe.Pointer(t.mixedTable)
}
func (t *Table) Clone() luagc.Value {
clone := new(Table)
*clone = *t
return clone
}
// Get returns t[k].
func (t *Table) Get(k Value) Value {
return t.get(k)
}
// Set implements t[k] = v (doesn't check if k is nil).
func (t *Table) Set(k, v Value) uint64 {
if v.IsNil() {
t.mixedTable.remove(k)
return 0
}
t.mixedTable.insert(k, v)
return 16
}
// Reset implements t[k] = v only if t[k] was already non-nil.
func (t *Table) Reset(k, v Value) (wasSet bool) {
if v.IsNil() {
return t.mixedTable.remove(k)
}
return t.mixedTable.reset(k, v)
}
// Len returns a length for t (see lua docs for details).
func (t *Table) Len() int64 {
return int64(t.mixedTable.len())
}
// Next returns the key-value pair that comes after k in the table t.
func (t *Table) Next(k Value) (next Value, val Value, ok bool) {
return t.mixedTable.next(k)
}