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.
|
2022-05-16 14:42:57 +00:00
|
|
|
func (s *slot) init(n int, rc *refCounter) {
|
2021-11-30 12:03:01 +00:00
|
|
|
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)
|
2022-05-16 14:42:57 +00:00
|
|
|
*rc += refCounter(n) // Virtual "Null" elements.
|
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)
|
2022-05-16 14:42:57 +00:00
|
|
|
} else {
|
|
|
|
*refs-- // Not really existing, but counted Null element.
|
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
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Get returns the item contained in the 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
|
|
|
}
|
|
|
|
|
2022-05-16 14:42:57 +00:00
|
|
|
// ClearRefs removes all slot variables from the reference counter.
|
|
|
|
func (s slot) ClearRefs(refs *refCounter) {
|
2021-11-30 12:03:01 +00:00
|
|
|
for _, item := range s {
|
|
|
|
refs.Remove(item)
|
2020-07-22 08:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 08:54:12 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Size returns the 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
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the 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)
|
|
|
|
}
|