github.com/ontio/ontology@v1.14.4/vm/neovm/execution_context.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package neovm 20 21 import ( 22 "io" 23 24 "github.com/ontio/ontology/vm/neovm/utils" 25 ) 26 27 type ExecutionContext struct { 28 Code []byte 29 OpReader *utils.VmReader 30 InstructionPointer int 31 vmFlags VmFeatureFlag 32 } 33 34 func NewExecutionContext(code []byte, flag VmFeatureFlag) *ExecutionContext { 35 var context ExecutionContext 36 context.Code = code 37 context.OpReader = utils.NewVmReader(code) 38 context.OpReader.AllowEOF = flag.AllowReaderEOF 39 context.vmFlags = flag 40 41 context.InstructionPointer = 0 42 return &context 43 } 44 45 func (ec *ExecutionContext) GetInstructionPointer() int { 46 return ec.OpReader.Position() 47 } 48 49 func (ec *ExecutionContext) SetInstructionPointer(offset int64) error { 50 _, err := ec.OpReader.Seek(offset, io.SeekStart) 51 return err 52 } 53 54 func (ec *ExecutionContext) NextInstruction() OpCode { 55 return OpCode(ec.Code[ec.OpReader.Position()]) 56 } 57 58 func (self *ExecutionContext) ReadOpCode() (val OpCode, eof bool) { 59 code, err := self.OpReader.ReadByte() 60 if err != nil { 61 eof = true 62 return 63 } 64 val = OpCode(code) 65 return val, false 66 } 67 68 func (ec *ExecutionContext) Clone() *ExecutionContext { 69 context := NewExecutionContext(ec.Code, ec.vmFlags) 70 context.InstructionPointer = ec.InstructionPointer 71 _ = context.SetInstructionPointer(int64(ec.GetInstructionPointer())) 72 return context 73 }