2019-03-15 22:42:35 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/stack"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VM represents an instance of a Neo Virtual Machine
|
|
|
|
type VM struct {
|
|
|
|
InvocationStack stack.Invocation
|
|
|
|
state vmstate
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewVM loads in a script
|
|
|
|
// uses the script to initiate a Context object
|
|
|
|
// pushes the context to the invocation stack
|
|
|
|
func NewVM(script []byte) *VM {
|
|
|
|
ctx := stack.NewContext(script)
|
|
|
|
v := &VM{
|
|
|
|
state: NONE,
|
|
|
|
}
|
|
|
|
v.InvocationStack.Push(ctx)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteOp will execute one opcode for a given context
|
|
|
|
func (v *VM) ExecuteOp(op stack.Instruction, ctx *stack.Context) error {
|
2019-03-15 23:37:54 +00:00
|
|
|
|
2019-03-15 22:42:35 +00:00
|
|
|
handleOp, ok := opFunc[op]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown opcode entered %v", op)
|
|
|
|
}
|
2019-03-15 23:37:54 +00:00
|
|
|
err := handleOp(op, ctx, &v.InvocationStack)
|
2019-03-15 22:42:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|