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
|
|
|
type (
|
|
|
|
rcInc interface {
|
|
|
|
IncRC() int
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
2021-08-11 12:16:29 +00:00
|
|
|
rcDec interface {
|
|
|
|
DecRC() int
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
irc, ok := item.(rcInc)
|
|
|
|
if !ok || irc.IncRC() > 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch t := item.(type) {
|
|
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
|
|
for _, it := range item.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:
|
|
|
|
for i := range t.Value().([]stackitem.MapElement) {
|
|
|
|
r.Add(t.Value().([]stackitem.MapElement)[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
|
|
|
irc, ok := item.(rcDec)
|
|
|
|
if !ok || irc.DecRC() > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch t := item.(type) {
|
|
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
|
|
for _, it := range item.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:
|
|
|
|
for i := range t.Value().([]stackitem.MapElement) {
|
|
|
|
r.Remove(t.Value().([]stackitem.MapElement)[i].Value)
|
2020-05-12 13:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|