2019-03-16 22:09:04 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/stack"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flow control
|
|
|
|
|
|
|
|
// RET Returns from the current context
|
|
|
|
// Returns HALT if there are nomore context's to run
|
2019-03-18 21:14:03 +00:00
|
|
|
func RET(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
2019-08-12 15:15:48 +00:00
|
|
|
_ = ctx // fix SA4009 warning
|
2019-03-16 22:09:04 +00:00
|
|
|
|
|
|
|
// Pop current context from the Inovation stack
|
2019-03-18 21:14:03 +00:00
|
|
|
ctx, err := istack.PopCurrentContext()
|
2019-03-16 22:09:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
2019-03-18 21:14:03 +00:00
|
|
|
// If this was the last context, then we copy over the evaluation stack to the resultstack
|
|
|
|
// As the program is about to terminate, once we remove the context
|
2019-03-16 22:09:04 +00:00
|
|
|
if istack.Len() == 0 {
|
2019-03-18 21:14:03 +00:00
|
|
|
|
|
|
|
err = ctx.Estack.CopyTo(rstack)
|
|
|
|
return HALT, err
|
2019-03-16 22:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
2019-04-08 23:07:15 +00:00
|
|
|
|
|
|
|
// NOP Returns NONE VMState.
|
|
|
|
func NOP(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JMP moves the instruction pointer to an offset which is
|
|
|
|
// calculated base on the instructionPointerOffset method.
|
|
|
|
// Returns and error if the offset is out of range.
|
|
|
|
func JMP(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
offset := instructionPointerOffset(ctx)
|
|
|
|
if err := ctx.SetIP(offset); err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JMPIF pops a boolean off of the stack and,
|
|
|
|
// if the the boolean's value is true, it
|
|
|
|
// moves the instruction pointer to an offset which is
|
|
|
|
// calculated base on the instructionPointerOffset method.
|
|
|
|
// Returns and error if the offset is out of range or
|
|
|
|
// the popped item is not a boolean.
|
|
|
|
func JMPIF(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
b, err := ctx.Estack.PopBoolean()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Value() {
|
|
|
|
offset := instructionPointerOffset(ctx)
|
|
|
|
if err := ctx.SetIP(offset); err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JMPIFNOT pops a boolean off of the stack and,
|
|
|
|
// if the the boolean's value is false, it
|
|
|
|
// moves the instruction pointer to an offset which is
|
|
|
|
// calculated base on the instructionPointerOffset method.
|
|
|
|
// Returns and error if the offset is out of range or
|
|
|
|
// the popped item is not a boolean.
|
|
|
|
func JMPIFNOT(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation, rstack *stack.RandomAccess) (Vmstate, error) {
|
|
|
|
b, err := ctx.Estack.PopBoolean()
|
|
|
|
if err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.Value() {
|
|
|
|
offset := instructionPointerOffset(ctx)
|
|
|
|
if err := ctx.SetIP(offset); err != nil {
|
|
|
|
return FAULT, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NONE, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func instructionPointerOffset(ctx *stack.Context) int {
|
|
|
|
return ctx.IP() + int(ctx.ReadInt16()) - 3
|
|
|
|
}
|