-
Notifications
You must be signed in to change notification settings - Fork 79
/
slot.go
66 lines (57 loc) · 1.25 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
package vm
import (
"encoding/json"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// slot is a fixed-size slice of stack items.
type slot []stackitem.Item
// init sets static slot size to n. It is intended to be used only by INITSSLOT.
func (s *slot) init(n int) {
if *s != nil {
panic("already initialized")
}
*s = make([]stackitem.Item, n)
}
// Set sets i-th storage slot.
func (s slot) Set(i int, item stackitem.Item, refs *refCounter) {
if s[i] == item {
return
}
old := s[i]
s[i] = item
if old != nil {
refs.Remove(old)
}
refs.Add(item)
}
// Get returns item contained in i-th slot.
func (s slot) Get(i int) stackitem.Item {
if item := s[i]; item != nil {
return item
}
return stackitem.Null{}
}
// Clear removes all slot variables from reference counter.
func (s slot) Clear(refs *refCounter) {
for _, item := range s {
refs.Remove(item)
}
}
// Size returns slot size.
func (s slot) Size() int {
if s == nil {
panic("not initialized")
}
return len(s)
}
// MarshalJSON implements JSON marshalling interface.
func (s slot) MarshalJSON() ([]byte, error) {
arr := make([]json.RawMessage, len(s))
for i := range s {
data, err := stackitem.ToJSONWithTypes(s[i])
if err == nil {
arr[i] = data
}
}
return json.Marshal(arr)
}