Add conversions for bytearray and Int stack items
This commit is contained in:
parent
8d55ea12f0
commit
da0a56f922
3 changed files with 68 additions and 2 deletions
|
@ -72,3 +72,24 @@ func (i *Int) Lsh(n *Int) (*Int, error) {
|
|||
func (i *Int) Integer() (*Int, error) {
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// ByteArray override the default ByteArray method
|
||||
// to convert a Integer into a byte Array
|
||||
func (i *Int) ByteArray() (*ByteArray, error) {
|
||||
return &ByteArray{
|
||||
i.abstractItem,
|
||||
i.val.Bytes(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
//Boolean override the default Boolean method
|
||||
// to convert an Integer into a Boolean StackItem
|
||||
func (i *Int) Boolean() (*Boolean, error) {
|
||||
|
||||
boolean := (i.val.Int64() != 0)
|
||||
return &Boolean{
|
||||
i.abstractItem,
|
||||
boolean,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,59 @@
|
|||
package stack
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/big"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ByteArray represents a slice of bytes on the stack
|
||||
type ByteArray struct {
|
||||
*abstractItem
|
||||
val []byte
|
||||
}
|
||||
|
||||
//NewByteArray returns a ByteArray stack item
|
||||
// given a byte slice
|
||||
func NewByteArray(val []byte) *ByteArray {
|
||||
return &ByteArray{
|
||||
&abstractItem{},
|
||||
val,
|
||||
}
|
||||
}
|
||||
|
||||
//ByteArray overrides the default abstractItem Bytes array method
|
||||
func (ba *ByteArray) ByteArray() (*ByteArray, error) {
|
||||
return ba, nil
|
||||
}
|
||||
|
||||
//Integer overrides the default Integer method to convert an
|
||||
// ByteArray Into an integer
|
||||
func (ba *ByteArray) Integer() (*Int, error) {
|
||||
|
||||
dest := make([]byte, 0)
|
||||
|
||||
for i, j := 0, len(ba.val)-1; i < j+1; i, j = i+1, j-1 {
|
||||
dest[i], dest[j] = ba.val[j], ba.val[i]
|
||||
}
|
||||
|
||||
integerVal := new(big.Int).SetBytes(dest)
|
||||
|
||||
return &Int{
|
||||
ba.abstractItem,
|
||||
integerVal,
|
||||
}, nil
|
||||
|
||||
// return ba, nil
|
||||
}
|
||||
|
||||
// Boolean will convert
|
||||
func (ba *ByteArray) Boolean() (*Boolean, error) {
|
||||
boolean, err := strconv.ParseBool(string(ba.val))
|
||||
if err != nil {
|
||||
return nil, errors.New("cannot convert byte array to a boolean")
|
||||
}
|
||||
return &Boolean{
|
||||
ba.abstractItem,
|
||||
boolean,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -70,7 +70,6 @@ func (ras *RandomAccess) Push(item Item) *RandomAccess {
|
|||
|
||||
// Insert will push a stackItem onto the stack at position `n`
|
||||
// Note; index 0 is the top of the stack, which is the end of slice
|
||||
// REDO:
|
||||
func (ras *RandomAccess) Insert(n uint16, item Item) (*RandomAccess, error) {
|
||||
|
||||
if n == 0 {
|
||||
|
@ -112,7 +111,6 @@ func (ras *RandomAccess) Peek(n uint16) (Item, error) {
|
|||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue