c1b6738bdb
* VM:Add abstract stack item * VM: Add stackItems; Array, Boolean, Int and ByteArray * VM: Add tests for stack item * VM: first pass at Random Access Stack object * VM: Add Sub, Mul, Mod LSH, RSH * VM: moved test helper functions into separate file * VM: removed helper functions from stack_test.go * Add conversions for bytearray and Int stack items * Add instructions file for vm * - Add guide to stack readme - Add testReadInt64 * Add Builder * Refactor Int, Boolean, ByteArray conversion * Add Context stack Item * Add Invocation stack - convenience RAS * rename testhelper to test_helper * Move opcode file * - Add `Add` OpCode - Add Opcode Function map * - Add test for math `Add` opcode - basic opcode execution * Add popTwoIntegers convenience func * Add `SUB` Opcode * Export Context Read methods - Return errors where failable * - Add `Op` to handleOP func signature - Add PushNBytes OPcode * remove error on NewBoolean - Expose underlying with Getter on Boolean StackItem - Add Equals method for ByteArray * Make Next() method on Context failable, refactor peekContext and Peek * Add ExecuteOp, Step and Run methods on the VM * Add Equal Opcode * Add THROWIFNOT Opcode * Add RET Opcode * Refactor PushNBytes Opcode * refactor Add, Sub to return VMSTATE add popTwoByteArrays helper function * Add basic tests for vm * clarify vm states * Add astack * [VM] Pass ResultStack to the opcode handlers * [VM] refactor handlers to have rstack as argument * [Stack] - Change RemoveCurrentContext for PopCurrentContext - Add CopTo method to stack * [VM] Add Result stack Len check in simple run test * [VM] fix typo * [Peer/Stall] Change seconds to milliseconds in test
150 lines
3.5 KiB
Go
150 lines
3.5 KiB
Go
package stack
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
// StackAverageSize is used to set the capacity of the stack
|
|
// setting this number too low, will cause extra allocations
|
|
StackAverageSize = 20
|
|
)
|
|
|
|
// RandomAccess represents a Random Access Stack
|
|
type RandomAccess struct {
|
|
vals []Item
|
|
}
|
|
|
|
// New will return a new random access stack
|
|
func New() *RandomAccess {
|
|
return &RandomAccess{
|
|
vals: make([]Item, 0, StackAverageSize),
|
|
}
|
|
}
|
|
|
|
// Items will return all items in the stack
|
|
func (ras *RandomAccess) items() []Item {
|
|
return ras.vals
|
|
}
|
|
|
|
//Len will return the length of the stack
|
|
func (ras *RandomAccess) Len() int {
|
|
if ras.vals == nil {
|
|
return -1
|
|
}
|
|
return len(ras.vals)
|
|
}
|
|
|
|
// Clear will remove all items in the stack
|
|
func (ras *RandomAccess) Clear() {
|
|
ras.vals = make([]Item, 0, StackAverageSize)
|
|
}
|
|
|
|
// Pop will remove the last stack item that was added
|
|
func (ras *RandomAccess) Pop() (Item, error) {
|
|
if len(ras.vals) == 0 {
|
|
return nil, errors.New("There are no items on the stack to pop")
|
|
}
|
|
if ras.vals == nil {
|
|
return nil, errors.New("Cannot pop from a nil stack")
|
|
}
|
|
|
|
l := len(ras.vals)
|
|
item := ras.vals[l-1]
|
|
ras.vals = ras.vals[:l-1]
|
|
|
|
return item, nil
|
|
}
|
|
|
|
// Push will put a stack item onto the top of the stack
|
|
func (ras *RandomAccess) Push(item Item) *RandomAccess {
|
|
if ras.vals == nil {
|
|
ras.vals = make([]Item, 0, StackAverageSize)
|
|
}
|
|
|
|
ras.vals = append(ras.vals, item)
|
|
|
|
return ras
|
|
}
|
|
|
|
// 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
|
|
func (ras *RandomAccess) Insert(n uint16, item Item) (*RandomAccess, error) {
|
|
|
|
if n == 0 {
|
|
return ras.Push(item), nil
|
|
}
|
|
|
|
if ras.vals == nil {
|
|
ras.vals = make([]Item, 0, StackAverageSize)
|
|
}
|
|
|
|
// Check that we are not inserting out of the bounds
|
|
stackSize := uint16(len(ras.vals))
|
|
if n > stackSize-1 {
|
|
return nil, fmt.Errorf("Tried to insert at index %d when length of stack is %d", n, len(ras.vals))
|
|
}
|
|
|
|
index := stackSize - n
|
|
|
|
ras.vals = append(ras.vals, item)
|
|
copy(ras.vals[index:], ras.vals[index-1:])
|
|
ras.vals[index] = item
|
|
|
|
return ras, nil
|
|
}
|
|
|
|
// Peek will check an element at a given index
|
|
// Note: 0 is the top of the stack, which is the end of the slice
|
|
func (ras *RandomAccess) Peek(n uint16) (Item, error) {
|
|
|
|
stackSize := uint16(len(ras.vals))
|
|
|
|
if n == 0 {
|
|
index := stackSize - 1
|
|
return ras.vals[index], nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// CopyTo will copy all of the stack items from `ras` into the stack that is passed as an argument
|
|
// XXX: once maxstacksize is implemented, we will return error if size goes over
|
|
// There will also be additional checks needed once stack isolation is added
|
|
func (ras *RandomAccess) CopyTo(stack *RandomAccess) error {
|
|
stack.vals = append(stack.vals, ras.vals...)
|
|
return nil
|
|
}
|
|
|
|
// Convenience Functions
|
|
|
|
// PopInt will remove the last stack item that was added
|
|
// And cast it to an integer
|
|
func (ras *RandomAccess) PopInt() (*Int, error) {
|
|
item, err := ras.Pop()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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()
|
|
}
|