vm: implement Neo.Iterator.* interops

This commit is contained in:
Evgenii Stratonikov 2019-11-13 15:29:27 +03:00
parent 3ff7fd5262
commit 5bc32b523a
5 changed files with 300 additions and 22 deletions

View file

@ -17,6 +17,32 @@ type (
}
)
type (
iterator interface {
enumerator
Key() StackItem
}
mapWrapper struct {
index int
keys []interface{}
m map[interface{}]StackItem
}
concatIter struct {
current iterator
second iterator
}
keysWrapper struct {
iter iterator
}
valuesWrapper struct {
iter iterator
}
)
func (a *arrayWrapper) Next() bool {
if next := a.index + 1; next < len(a.value) {
a.index = next
@ -30,6 +56,10 @@ func (a *arrayWrapper) Value() StackItem {
return a.value[a.index]
}
func (a *arrayWrapper) Key() StackItem {
return makeStackItem(a.index)
}
func (c *concatEnum) Next() bool {
if c.current.Next() {
return true
@ -42,3 +72,53 @@ func (c *concatEnum) Next() bool {
func (c *concatEnum) Value() StackItem {
return c.current.Value()
}
func (i *concatIter) Next() bool {
if i.current.Next() {
return true
}
i.current = i.second
return i.second.Next()
}
func (i *concatIter) Value() StackItem {
return i.current.Value()
}
func (i *concatIter) Key() StackItem {
return i.current.Key()
}
func (m *mapWrapper) Next() bool {
if next := m.index + 1; next < len(m.keys) {
m.index = next
return true
}
return false
}
func (m *mapWrapper) Value() StackItem {
return m.m[m.keys[m.index]]
}
func (m *mapWrapper) Key() StackItem {
return makeStackItem(m.keys[m.index])
}
func (e *keysWrapper) Next() bool {
return e.iter.Next()
}
func (e *keysWrapper) Value() StackItem {
return e.iter.Key()
}
func (e *valuesWrapper) Next() bool {
return e.iter.Next()
}
func (e *valuesWrapper) Value() StackItem {
return e.iter.Value()
}