2020-05-07 08:45:24 +00:00
|
|
|
package vm
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
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 {
|
2020-06-03 12:55:06 +00:00
|
|
|
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{
|
2020-06-03 12:55:06 +00:00
|
|
|
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.
|
2020-06-03 12:55:06 +00:00
|
|
|
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.
|
2020-06-03 12:55:06 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.Null{}
|
2020-05-07 08:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns slot size.
|
|
|
|
func (s *Slot) Size() int { return len(s.storage) }
|
2020-07-22 08:11:28 +00:00
|
|
|
|
|
|
|
// Clear removes all slot variables from reference counter.
|
|
|
|
func (s *Slot) Clear() {
|
|
|
|
for _, item := range s.storage {
|
|
|
|
s.refs.Remove(item)
|
|
|
|
}
|
|
|
|
}
|