rpc: allow to marshal Iterators for invoke* results

This commit is contained in:
Anna Shaleva 2021-04-28 17:46:34 +03:00
parent 4e55b1a9ed
commit 9eeebf481c
5 changed files with 108 additions and 21 deletions

View file

@ -71,6 +71,12 @@ func init() {
})
}
// IsIterator returns whether stackitem implements iterator interface.
func IsIterator(item stackitem.Item) bool {
_, ok := item.Value().(iterator)
return ok
}
// IteratorNext handles syscall System.Enumerator.Next.
func IteratorNext(v *VM) error {
iop := v.Estack().Pop().Interop()
@ -89,6 +95,18 @@ func IteratorValue(v *VM) error {
return nil
}
// IteratorValues returns an array of up to `max` iterator values. The second
// return parameter denotes whether iterator is truncated.
func IteratorValues(item stackitem.Item, max int) ([]stackitem.Item, bool) {
var result []stackitem.Item
arr := item.Value().(iterator)
for arr.Next() && max > 0 {
result = append(result, arr.Value())
max--
}
return result, arr.Next()
}
// NewIterator creates new iterator from the provided stack item.
func NewIterator(item stackitem.Item) (stackitem.Item, error) {
switch t := item.(type) {