core: adjust System.Iterator.Create interop

Closes #1201.

It should be able to iterate over primitive byte-array-like types also.
This commit is contained in:
Anna Shaleva 2020-07-21 13:14:16 +03:00
parent 459ac34839
commit c9ef7425ac
2 changed files with 13 additions and 5 deletions

View file

@ -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{}
}

View file

@ -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})