neoneo-go/pkg/vm/slot.go

48 lines
889 B
Go
Raw Normal View History

2020-05-07 08:45:24 +00:00
package vm
import (
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
2020-05-07 08:45:24 +00:00
// Slot is a fixed-size slice of stack items.
type Slot struct {
storage []stackitem.Item
2020-05-08 12:34:32 +00:00
refs *refCounter
2020-05-07 08:45:24 +00:00
}
// newSlot returns new slot of n items.
2020-05-08 12:34:32 +00:00
func newSlot(n int, refs *refCounter) *Slot {
2020-05-07 08:45:24 +00:00
return &Slot{
storage: make([]stackitem.Item, n),
2020-05-08 12:34:32 +00:00
refs: refs,
2020-05-07 08:45:24 +00:00
}
}
2020-05-08 12:34:32 +00:00
func (v *VM) newSlot(n int) *Slot {
return newSlot(n, v.refs)
}
2020-05-07 08:45:24 +00:00
// Set sets i-th storage slot.
func (s *Slot) Set(i int, item stackitem.Item) {
2020-05-08 12:34:32 +00:00
if s.storage[i] == item {
return
}
old := s.storage[i]
2020-05-07 08:45:24 +00:00
s.storage[i] = item
2020-05-08 12:34:32 +00:00
if old != nil {
s.refs.Remove(old)
}
s.refs.Add(item)
2020-05-07 08:45:24 +00:00
}
// Get returns item contained in i-th slot.
func (s *Slot) Get(i int) stackitem.Item {
2020-05-07 08:45:24 +00:00
if item := s.storage[i]; item != nil {
return item
}
return stackitem.Null{}
2020-05-07 08:45:24 +00:00
}
// Size returns slot size.
func (s *Slot) Size() int { return len(s.storage) }