vm: add NewMapIterator()

This commit is contained in:
Evgenii Stratonikov 2019-12-26 14:34:15 +03:00
parent 9cc0fca9d2
commit 7b5c47e7f5

View file

@ -177,32 +177,37 @@ func EnumeratorConcat(v *VM) error {
// IteratorCreate handles syscall Neo.Iterator.Create. // IteratorCreate handles syscall Neo.Iterator.Create.
func IteratorCreate(v *VM) error { func IteratorCreate(v *VM) error {
data := v.Estack().Pop() data := v.Estack().Pop()
var item interface{} var item StackItem
switch t := data.value.(type) { switch t := data.value.(type) {
case *ArrayItem, *StructItem: case *ArrayItem, *StructItem:
item = &arrayWrapper{ item = NewInteropItem(&arrayWrapper{
index: -1, index: -1,
value: t.Value().([]StackItem), value: t.Value().([]StackItem),
} })
case *MapItem: case *MapItem:
keys := make([]interface{}, 0, len(t.value)) item = NewMapIterator(t.value)
for k := range t.value {
keys = append(keys, k)
}
item = &mapWrapper{
index: -1,
keys: keys,
m: t.value,
}
default: default:
return errors.New("non-iterable type") return errors.New("non-iterable type")
} }
v.Estack().Push(&Element{value: NewInteropItem(item)}) v.Estack().Push(&Element{value: item})
return nil 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. // IteratorConcat handles syscall Neo.Iterator.Concat.
func IteratorConcat(v *VM) error { func IteratorConcat(v *VM) error {
iop1 := v.Estack().Pop().Interop() iop1 := v.Estack().Pop().Interop()