2020-05-12 13:05:10 +00:00
|
|
|
package vm
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// refCounter represents a reference counter for the VM.
|
2021-08-11 12:16:29 +00:00
|
|
|
type refCounter int
|
2020-05-12 13:05:10 +00:00
|
|
|
|
2021-08-11 12:16:29 +00:00
|
|
|
func newRefCounter() *refCounter {
|
|
|
|
return new(refCounter)
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds an item to the reference counter.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (r *refCounter) Add(item stackitem.Item) {
|
2021-08-02 19:38:41 +00:00
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-08-11 12:16:29 +00:00
|
|
|
*r++
|
2020-05-12 13:05:10 +00:00
|
|
|
|
2021-08-11 12:16:29 +00:00
|
|
|
switch t := item.(type) {
|
2022-08-23 06:19:09 +00:00
|
|
|
case *stackitem.Array:
|
|
|
|
if t.IncRC() == 1 {
|
|
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
|
|
r.Add(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *stackitem.Struct:
|
|
|
|
if t.IncRC() == 1 {
|
|
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
|
|
r.Add(it)
|
|
|
|
}
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
2021-08-11 12:16:29 +00:00
|
|
|
case *stackitem.Map:
|
2022-08-23 06:19:09 +00:00
|
|
|
if t.IncRC() == 1 {
|
|
|
|
elems := t.Value().([]stackitem.MapElement)
|
|
|
|
for i := range elems {
|
|
|
|
r.Add(elems[i].Key)
|
|
|
|
r.Add(elems[i].Value)
|
|
|
|
}
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Remove removes an item from the reference counter.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (r *refCounter) Remove(item stackitem.Item) {
|
2021-08-02 19:38:41 +00:00
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-08-11 12:16:29 +00:00
|
|
|
*r--
|
2020-05-12 13:05:10 +00:00
|
|
|
|
2021-08-11 12:16:29 +00:00
|
|
|
switch t := item.(type) {
|
2022-08-23 06:19:09 +00:00
|
|
|
case *stackitem.Array:
|
|
|
|
if t.DecRC() == 0 {
|
|
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
|
|
r.Remove(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *stackitem.Struct:
|
|
|
|
if t.DecRC() == 0 {
|
|
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
|
|
r.Remove(it)
|
|
|
|
}
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
2021-08-11 12:16:29 +00:00
|
|
|
case *stackitem.Map:
|
2022-08-23 06:19:09 +00:00
|
|
|
if t.DecRC() == 0 {
|
|
|
|
elems := t.Value().([]stackitem.MapElement)
|
|
|
|
for i := range elems {
|
|
|
|
r.Remove(elems[i].Key)
|
|
|
|
r.Remove(elems[i].Value)
|
|
|
|
}
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|