From 7b5c47e7f54d6062ba06697dcfad67572cb743d1 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 26 Dec 2019 14:34:15 +0300 Subject: [PATCH] vm: add NewMapIterator() --- pkg/vm/interop.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/vm/interop.go b/pkg/vm/interop.go index 419c5f5d3..6247d5a70 100644 --- a/pkg/vm/interop.go +++ b/pkg/vm/interop.go @@ -177,32 +177,37 @@ func EnumeratorConcat(v *VM) error { // IteratorCreate handles syscall Neo.Iterator.Create. func IteratorCreate(v *VM) error { data := v.Estack().Pop() - var item interface{} + var item StackItem switch t := data.value.(type) { case *ArrayItem, *StructItem: - item = &arrayWrapper{ + item = NewInteropItem(&arrayWrapper{ index: -1, value: t.Value().([]StackItem), - } + }) case *MapItem: - keys := make([]interface{}, 0, len(t.value)) - for k := range t.value { - keys = append(keys, k) - } - - item = &mapWrapper{ - index: -1, - keys: keys, - m: t.value, - } + item = NewMapIterator(t.value) default: return errors.New("non-iterable type") } - v.Estack().Push(&Element{value: NewInteropItem(item)}) + v.Estack().Push(&Element{value: item}) return nil } +// NewMapIterator returns new interop item containing iterator over m. +func NewMapIterator(m map[interface{}]StackItem) *InteropItem { + keys := make([]interface{}, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + + return NewInteropItem(&mapWrapper{ + index: -1, + keys: keys, + m: m, + }) +} + // IteratorConcat handles syscall Neo.Iterator.Concat. func IteratorConcat(v *VM) error { iop1 := v.Estack().Pop().Interop()