From c9ef7425ac7dbe2ceef43ae68062ae19d7744ca6 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 21 Jul 2020 13:14:16 +0300 Subject: [PATCH] core: adjust System.Iterator.Create interop Closes #1201. It should be able to iterate over primitive byte-array-like types also. --- pkg/interop/iterator/iterator.go | 9 +++++---- pkg/vm/interop.go | 9 ++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/interop/iterator/iterator.go b/pkg/interop/iterator/iterator.go index 9e295f11e..80be62165 100644 --- a/pkg/interop/iterator/iterator.go +++ b/pkg/interop/iterator/iterator.go @@ -11,10 +11,11 @@ import "github.com/nspcc-dev/neo-go/pkg/interop/enumerator" // structure is similar in function to Neo .net framework's Iterator. type Iterator struct{} -// Create creates an iterator from the given items (array, struct or map). A new -// iterator is set to point at element -1, so to access its first element you -// need to call Next first. This function uses `System.Iterator.Create` syscall. -func Create(items []interface{}) Iterator { +// Create creates an iterator from the given items (array, struct, map, byte +// array or integer and boolean converted to byte array). A new iterator is set +// to point at element -1, so to access its first element you need to call Next +// first. This function uses `System.Iterator.Create` syscall. +func Create(items interface{}) Iterator { return Iterator{} } diff --git a/pkg/vm/interop.go b/pkg/vm/interop.go index 78e1db225..7caa9b114 100644 --- a/pkg/vm/interop.go +++ b/pkg/vm/interop.go @@ -198,7 +198,14 @@ func IteratorCreate(v *VM) error { case *stackitem.Map: item = NewMapIterator(t) default: - return errors.New("non-iterable type") + data, err := t.TryBytes() + if err != nil { + return fmt.Errorf("non-iterable type %s", t.Type()) + } + item = stackitem.NewInterop(&byteArrayWrapper{ + index: -1, + value: data, + }) } v.Estack().Push(&Element{value: item})