forked from TrueCloudLab/neoneo-go
a7e973030c
rename invocation to invocationstack export vmstate
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package stack
|
|
|
|
import "errors"
|
|
|
|
// Invocation embeds a Random Access stack
|
|
// Providing helper methods for the context object
|
|
type Invocation struct{ RandomAccess }
|
|
|
|
//NewInvocation will return a new
|
|
// Invocation stack
|
|
func NewInvocation() *Invocation {
|
|
return &Invocation{
|
|
RandomAccess{
|
|
vals: make([]Item, 0, StackAverageSize),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (i *Invocation) peekContext(n uint16) (*Context, error) {
|
|
item, err := i.Peek(n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return item.Context()
|
|
}
|
|
|
|
// CurrentContext returns the current context on the invocation stack
|
|
func (i *Invocation) CurrentContext() (*Context, error) {
|
|
return i.peekContext(0)
|
|
}
|
|
|
|
// RemoveCurrentContext removes the context on the top of the invocation stack
|
|
// This is a convenience method for Pop
|
|
func (i *Invocation) RemoveCurrentContext() error {
|
|
_, err := i.Pop()
|
|
return err
|
|
}
|
|
|
|
// CallingContext will return the cntext item
|
|
// that will be called next.
|
|
func (i *Invocation) CallingContext() (*Context, error) {
|
|
if i.Len() < 1 {
|
|
return nil, errors.New("Length of invocation stack is < 1, no calling context")
|
|
}
|
|
return i.peekContext(1)
|
|
}
|
|
|
|
// EntryContext will return the context item that
|
|
// started the program
|
|
func (i *Invocation) EntryContext() (*Context, error) {
|
|
|
|
// firstItemIndex refers to the first item
|
|
// that was popped on the stack
|
|
firstItemIndex := uint16(i.Len() - 1) // N.B. if this overflows because len is zero, then an error will be returned
|
|
return i.peekContext(firstItemIndex)
|
|
}
|