-
Notifications
You must be signed in to change notification settings - Fork 79
/
slot.go
68 lines (59 loc) · 1.26 KB
/
slot.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
package vm
import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// Slot is a fixed-size slice of stack items.
type Slot struct {
storage []stackitem.Item
refs *refCounter
}
// newSlot returns new slot with the provided reference counter.
func newSlot(refs *refCounter) *Slot {
return &Slot{
refs: refs,
}
}
// init sets static slot size to n. It is intended to be used only by INITSSLOT.
func (s *Slot) init(n int) {
if s.storage != nil {
panic("already initialized")
}
s.storage = make([]stackitem.Item, n)
}
func (v *VM) newSlot(n int) *Slot {
s := newSlot(&v.refs)
s.init(n)
return s
}
// Set sets i-th storage slot.
func (s *Slot) Set(i int, item stackitem.Item) {
if s.storage[i] == item {
return
}
old := s.storage[i]
s.storage[i] = item
if old != nil {
s.refs.Remove(old)
}
s.refs.Add(item)
}
// Get returns item contained in i-th slot.
func (s *Slot) Get(i int) stackitem.Item {
if item := s.storage[i]; item != nil {
return item
}
return stackitem.Null{}
}
// Clear removes all slot variables from reference counter.
func (s *Slot) Clear() {
for _, item := range s.storage {
s.refs.Remove(item)
}
}
// Size returns slot size.
func (s *Slot) Size() int {
if s.storage == nil {
panic("not initialized")
}
return len(s.storage)
}