neo-go/pkg/vm/slot.go

44 lines
803 B
Go
Raw Normal View History

2020-05-07 08:45:24 +00:00
package vm
// Slot is a fixed-size slice of stack items.
type Slot struct {
storage []StackItem
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, 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) {
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 {
if item := s.storage[i]; item != nil {
return item
}
return NullItem{}
}
// Size returns slot size.
func (s *Slot) Size() int { return len(s.storage) }