emit.Opcodes(script.BinWriter,opcode.OVER)// Load iterator from 1-st cell of estack.
emit.Syscall(script.BinWriter,interopnames.SystemIteratorNext)// Call System.Iterator.Next, it will pop the iterator from estack and push `true` or `false` to estack.
jmpIfNotOffset:=script.Len()
emit.Instruction(script.BinWriter,opcode.JMPIFNOT,// Pop boolean value (from the previous step) from estack, if `false`, then iterator has no more items => jump to the end of program.
[]byte{
0x00,// jump to loadResultOffset, but we'll fill this byte after script creation.
})
emit.Opcodes(script.BinWriter,opcode.DUP,// Duplicate the resulting array from 0-th cell of estack and push it to estack.
opcode.PUSH2,opcode.PICK)// Pick iterator from the 2-nd cell of estack.
emit.Syscall(script.BinWriter,interopnames.SystemIteratorValue)// Call System.Iterator.Value, it will pop the iterator from estack and push its current value to estack.
emit.Opcodes(script.BinWriter,opcode.APPEND)// Pop iterator value and the resulting array from estack. Append value to the resulting array. Array is a reference type, thus, value stored at the 1-th cell of local slot will also be updated.
emit.Opcodes(script.BinWriter,opcode.DUP,// Duplicate the resulting array from 0-th cell of estack and push it to estack.
opcode.SIZE,// Pop array from estack and push its size to estack.
opcode.PUSH3,opcode.PICK,// Pick maxIteratorResultItems from the 3-d cell of estack.
opcode.GE)// Compare len(arr) and maxIteratorResultItems
jmpIfMaxReachedOffset:=script.Len()
emit.Instruction(script.BinWriter,opcode.JMPIF,// Pop boolean value (from the previous step) from estack, if `false`, then max array elements is reached => jump to the end of program.
[]byte{
0x00,// jump to loadResultOffset, but we'll fill this byte after script creation.
})
jmpOffset:=script.Len()
emit.Instruction(script.BinWriter,opcode.JMP,// Jump to the start of iterator traverse cycle.
[]byte{
uint8(iteratorTraverseCycleStartOffset-jmpOffset),// jump to iteratorTraverseCycleStartOffset; offset is relative to JMP position.
})
// End of the program: push the result on stack and return.
loadResultOffset:=script.Len()
emit.Opcodes(script.BinWriter,opcode.NIP,// Remove iterator from the 1-st cell of estack
opcode.NIP)// Remove maxIteratorResultItems from the 1-st cell of estack, so that only resulting array is left on estack.