2020-04-08 11:08:43 +00:00
|
|
|
package iterator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2021-04-29 14:19:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-04-08 11:08:43 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 14:19:30 +00:00
|
|
|
type iterator interface {
|
|
|
|
Next() bool
|
|
|
|
Value() stackitem.Item
|
2020-04-08 11:08:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:21 +00:00
|
|
|
// Next advances the iterator, pushes true on success and false otherwise.
|
|
|
|
func Next(ic *interop.Context) error {
|
2021-04-29 14:19:30 +00:00
|
|
|
iop := ic.VM.Estack().Pop().Interop()
|
|
|
|
arr := iop.Value().(iterator)
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.Bool(arr.Next()))
|
2021-04-29 14:19:30 +00:00
|
|
|
|
|
|
|
return nil
|
2020-04-08 11:08:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 09:30:21 +00:00
|
|
|
// Value returns current iterator value and depends on iterator type:
|
|
|
|
// For slices the result is just value.
|
|
|
|
// For maps the result is key-value pair packed in a struct.
|
|
|
|
func Value(ic *interop.Context) error {
|
2021-04-29 14:19:30 +00:00
|
|
|
iop := ic.VM.Estack().Pop().Interop()
|
|
|
|
arr := iop.Value().(iterator)
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(arr.Value())
|
2021-04-29 14:19:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsIterator returns whether stackitem implements iterator interface.
|
|
|
|
func IsIterator(item stackitem.Item) bool {
|
|
|
|
_, ok := item.Value().(iterator)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2024-08-14 09:36:15 +00:00
|
|
|
// ValuesTruncated returns an array of up to `maxNum` iterator values. The second
|
2022-06-15 18:23:29 +00:00
|
|
|
// return parameter denotes whether iterator is truncated, i.e. has more values.
|
|
|
|
// The provided iterator CAN NOT be reused in the subsequent calls to Values and
|
|
|
|
// to ValuesTruncated.
|
2024-08-14 09:36:15 +00:00
|
|
|
func ValuesTruncated(item stackitem.Item, maxNum int) ([]stackitem.Item, bool) {
|
|
|
|
result := Values(item, maxNum)
|
2022-06-15 18:23:29 +00:00
|
|
|
arr := item.Value().(iterator)
|
|
|
|
return result, arr.Next()
|
|
|
|
}
|
|
|
|
|
2024-08-14 09:36:15 +00:00
|
|
|
// Values returns an array of up to `maxNum` iterator values. The provided
|
2022-06-15 18:23:29 +00:00
|
|
|
// iterator can safely be reused to retrieve the rest of its values in the
|
|
|
|
// subsequent calls to Values and to ValuesTruncated.
|
2024-08-14 09:36:15 +00:00
|
|
|
func Values(item stackitem.Item, maxNum int) []stackitem.Item {
|
2021-04-29 14:19:30 +00:00
|
|
|
var result []stackitem.Item
|
|
|
|
arr := item.Value().(iterator)
|
2024-08-14 09:36:15 +00:00
|
|
|
for maxNum > 0 && arr.Next() {
|
2021-04-29 14:19:30 +00:00
|
|
|
result = append(result, arr.Value())
|
2024-08-14 09:36:15 +00:00
|
|
|
maxNum--
|
2021-04-29 14:19:30 +00:00
|
|
|
}
|
2022-06-15 18:23:29 +00:00
|
|
|
return result
|
2020-04-08 11:08:43 +00:00
|
|
|
}
|