github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/vm/slot.go (about) 1 package vm 2 3 import ( 4 "encoding/json" 5 6 "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" 7 ) 8 9 // slot is a fixed-size slice of stack items. 10 type slot []stackitem.Item 11 12 // init sets static slot size to n. It is intended to be used only by INITSSLOT. 13 func (s *slot) init(n int, rc *refCounter) { 14 if *s != nil { 15 panic("already initialized") 16 } 17 *s = make([]stackitem.Item, n) 18 *rc += refCounter(n) // Virtual "Null" elements. 19 } 20 21 // Set sets i-th storage slot. 22 func (s slot) Set(i int, item stackitem.Item, refs *refCounter) { 23 if s[i] == item { 24 return 25 } 26 refs.Remove(s[i]) 27 s[i] = item 28 refs.Add(item) 29 } 30 31 // Get returns the item contained in the i-th slot. 32 func (s slot) Get(i int) stackitem.Item { 33 if item := s[i]; item != nil { 34 return item 35 } 36 return stackitem.Null{} 37 } 38 39 // ClearRefs removes all slot variables from the reference counter. 40 func (s slot) ClearRefs(refs *refCounter) { 41 for _, item := range s { 42 refs.Remove(item) 43 } 44 } 45 46 // Size returns the slot size. 47 func (s slot) Size() int { 48 if s == nil { 49 panic("not initialized") 50 } 51 return len(s) 52 } 53 54 // MarshalJSON implements the JSON marshalling interface. 55 func (s slot) MarshalJSON() ([]byte, error) { 56 arr := make([]json.RawMessage, len(s)) 57 for i := range s { 58 data, err := stackitem.ToJSONWithTypes(s[i]) 59 if err == nil { 60 arr[i] = data 61 } 62 } 63 return json.Marshal(arr) 64 }