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"
|
|
|
|
)
|
|
|
|
|
2020-05-12 13:05:10 +00:00
|
|
|
// refCounter represents reference counter for the VM.
|
|
|
|
type refCounter struct {
|
2020-06-03 12:55:06 +00:00
|
|
|
items map[stackitem.Item]int
|
2020-05-12 13:05:10 +00:00
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRefCounter() *refCounter {
|
|
|
|
return &refCounter{
|
2020-06-03 12:55:06 +00:00
|
|
|
items: make(map[stackitem.Item]int),
|
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) {
|
2020-05-12 13:05:10 +00:00
|
|
|
r.size++
|
|
|
|
|
|
|
|
switch item.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array, *stackitem.Struct, *stackitem.Map:
|
2020-05-12 13:05:10 +00:00
|
|
|
if r.items[item]++; r.items[item] > 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := item.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
|
|
for _, it := range item.Value().([]stackitem.Item) {
|
2020-05-12 13:05:10 +00:00
|
|
|
r.Add(it)
|
|
|
|
}
|
2020-06-03 12:55:06 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes item from the reference counter.
|
2020-06-03 12:55:06 +00:00
|
|
|
func (r *refCounter) Remove(item stackitem.Item) {
|
2020-05-12 13:05:10 +00:00
|
|
|
r.size--
|
|
|
|
|
|
|
|
switch item.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array, *stackitem.Struct, *stackitem.Map:
|
2020-05-12 13:05:10 +00:00
|
|
|
if r.items[item] > 1 {
|
|
|
|
r.items[item]--
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(r.items, item)
|
|
|
|
|
|
|
|
switch t := item.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
|
|
for _, it := range item.Value().([]stackitem.Item) {
|
2020-05-12 13:05:10 +00:00
|
|
|
r.Remove(it)
|
|
|
|
}
|
2020-06-03 12:55:06 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|