mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-03 23:02:27 +00:00
Make Next() method on Context failable
refactor peekContext and Peek
This commit is contained in:
parent
48413900ca
commit
9eb11d2822
3 changed files with 23 additions and 11 deletions
|
@ -106,15 +106,14 @@ func (ras *RandomAccess) Peek(n uint16) (Item, error) {
|
|||
return ras.vals[index], nil
|
||||
}
|
||||
|
||||
if ras.vals == nil {
|
||||
return nil, errors.New("Cannot peak at a nil stack")
|
||||
if ras.Len() < 1 {
|
||||
return nil, fmt.Errorf("cannot peak at a stack with no item, length of stack is %d", ras.Len())
|
||||
}
|
||||
|
||||
// Check that we are not peeking out of the bounds
|
||||
if n > stackSize-1 {
|
||||
return nil, fmt.Errorf("Tried to peek at index %d when length of stack is %d", n, len(ras.vals))
|
||||
}
|
||||
|
||||
index := stackSize - n - 1
|
||||
|
||||
return ras.vals[index], nil
|
||||
|
@ -131,3 +130,13 @@ func (ras *RandomAccess) PopInt() (*Int, error) {
|
|||
}
|
||||
return item.Integer()
|
||||
}
|
||||
|
||||
// PopByteArray will remove the last stack item that was added
|
||||
// And cast it to an ByteArray
|
||||
func (ras *RandomAccess) PopByteArray() (*ByteArray, error) {
|
||||
item, err := ras.Pop()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return item.ByteArray()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue