2020-05-07 08:45:24 +00:00
|
|
|
package vm
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
import (
|
2021-09-08 14:27:11 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
2021-11-30 12:03:01 +00:00
|
|
|
// slot is a fixed-size slice of stack items.
|
|
|
|
type slot []stackitem.Item
|
2020-05-07 08:45:24 +00:00
|
|
|
|
2020-07-24 08:54:12 +00:00
|
|
|
// init sets static slot size to n. It is intended to be used only by INITSSLOT.
|
2021-11-30 12:03:01 +00:00
|
|
|
func (s *slot) init(n int) {
|
|
|
|
if *s != nil {
|
2020-07-24 08:54:12 +00:00
|
|
|
panic("already initialized")
|
|
|
|
}
|
2021-11-30 12:03:01 +00:00
|
|
|
*s = make([]stackitem.Item, n)
|
2020-05-08 12:34:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:45:24 +00:00
|
|
|
// Set sets i-th storage slot.
|
2021-11-30 12:03:01 +00:00
|
|
|
func (s slot) Set(i int, item stackitem.Item, refs *refCounter) {
|
|
|
|
if s[i] == item {
|
2020-05-08 12:34:32 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-30 12:03:01 +00:00
|
|
|
old := s[i]
|
|
|
|
s[i] = item
|
2020-05-08 12:34:32 +00:00
|
|
|
if old != nil {
|
2021-11-30 12:03:01 +00:00
|
|
|
refs.Remove(old)
|
2020-05-08 12:34:32 +00:00
|
|
|
}
|
2021-11-30 12:03:01 +00:00
|
|
|
refs.Add(item)
|
2020-05-07 08:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns item contained in i-th slot.
|
2021-11-30 12:03:01 +00:00
|
|
|
func (s slot) Get(i int) stackitem.Item {
|
|
|
|
if item := s[i]; item != nil {
|
2020-05-07 08:45:24 +00:00
|
|
|
return item
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.Null{}
|
2020-05-07 08:45:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 08:11:28 +00:00
|
|
|
// Clear removes all slot variables from reference counter.
|
2021-11-30 12:03:01 +00:00
|
|
|
func (s slot) Clear(refs *refCounter) {
|
|
|
|
for _, item := range s {
|
|
|
|
refs.Remove(item)
|
2020-07-22 08:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 08:54:12 +00:00
|
|
|
|
|
|
|
// Size returns slot size.
|
2021-11-30 12:03:01 +00:00
|
|
|
func (s slot) Size() int {
|
|
|
|
if s == nil {
|
2020-07-24 08:54:12 +00:00
|
|
|
panic("not initialized")
|
|
|
|
}
|
2021-11-30 12:03:01 +00:00
|
|
|
return len(s)
|
2020-07-24 08:54:12 +00:00
|
|
|
}
|
2021-09-08 14:27:11 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements JSON marshalling interface.
|
2021-11-30 12:03:01 +00:00
|
|
|
func (s slot) MarshalJSON() ([]byte, error) {
|
|
|
|
arr := make([]json.RawMessage, len(s))
|
|
|
|
for i := range s {
|
|
|
|
data, err := stackitem.ToJSONWithTypes(s[i])
|
2021-09-08 14:27:11 +00:00
|
|
|
if err == nil {
|
|
|
|
arr[i] = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(arr)
|
|
|
|
}
|