Start hiding members
This commit is contained in:
70
state.go
70
state.go
@@ -4,19 +4,19 @@ import "fmt"
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
|
||||
const GlobalMemoryEntries = 16
|
||||
const globalMemoryEntries = 16
|
||||
|
||||
type State struct {
|
||||
Running bool
|
||||
Error error
|
||||
running bool
|
||||
err error
|
||||
|
||||
Functions [][]*Instruction
|
||||
FunctionIndex int64
|
||||
InstructionIndex int64
|
||||
functions [][]*Instruction
|
||||
functionIndex int64
|
||||
instructionIndex int64
|
||||
|
||||
ComparisonResult bool
|
||||
GlobalMemory [GlobalMemoryEntries]uint64
|
||||
Stack []*StackFrame
|
||||
comparisonResult bool
|
||||
globalMemory [globalMemoryEntries]uint64
|
||||
stack []*StackFrame
|
||||
}
|
||||
|
||||
func NewState(byteCodes [][]byte) (*State, error) {
|
||||
@@ -38,37 +38,37 @@ func NewState(byteCodes [][]byte) (*State, error) {
|
||||
OpCode: OpReturn,
|
||||
})
|
||||
|
||||
state.Functions = append(state.Functions, instrs)
|
||||
state.functions = append(state.functions, instrs)
|
||||
}
|
||||
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func (state *State) StackFrame() *StackFrame {
|
||||
return state.Stack[len(state.Stack)-1]
|
||||
return state.stack[len(state.stack)-1]
|
||||
}
|
||||
|
||||
func (state *State) Function() []*Instruction {
|
||||
return state.Functions[state.FunctionIndex]
|
||||
return state.functions[state.functionIndex]
|
||||
}
|
||||
|
||||
func (state *State) Execute() {
|
||||
state.setHandlers()
|
||||
state.call(0)
|
||||
state.Running = true
|
||||
state.running = true
|
||||
|
||||
for state.Running {
|
||||
for state.running {
|
||||
state.ProcessInstruction()
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) setError(err error) {
|
||||
state.Error = err
|
||||
state.Running = false
|
||||
state.err = err
|
||||
state.running = false
|
||||
}
|
||||
|
||||
func (state *State) setHandlers() {
|
||||
for _, fnc := range state.Functions {
|
||||
for _, fnc := range state.functions {
|
||||
for _, instr := range fnc {
|
||||
handler, found := OpHandlers[instr.OpCode]
|
||||
if !found {
|
||||
@@ -83,8 +83,8 @@ func (state *State) setHandlers() {
|
||||
|
||||
func (state *State) ProcessInstruction() {
|
||||
fnc := state.Function()
|
||||
instr := fnc[state.InstructionIndex]
|
||||
state.InstructionIndex += 1
|
||||
instr := fnc[state.instructionIndex]
|
||||
state.instructionIndex += 1
|
||||
instr.opHandler(state, instr)
|
||||
}
|
||||
|
||||
@@ -101,11 +101,11 @@ func (state *State) ReadUnsigned(op *Operand) uint64 {
|
||||
return state.StackFrame().FunctionMemory[op.Value]
|
||||
|
||||
case GlobalMemoryIndex:
|
||||
if op.Value >= GlobalMemoryEntries {
|
||||
if op.Value >= globalMemoryEntries {
|
||||
state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value))
|
||||
return 0
|
||||
}
|
||||
return state.GlobalMemory[op.Value]
|
||||
return state.globalMemory[op.Value]
|
||||
|
||||
default:
|
||||
state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type))
|
||||
@@ -130,11 +130,11 @@ func (state *State) WriteUnsigned(op *Operand, value uint64) {
|
||||
state.StackFrame().FunctionMemory[op.Value] = value
|
||||
|
||||
case GlobalMemoryIndex:
|
||||
if op.Value >= GlobalMemoryEntries {
|
||||
if op.Value >= globalMemoryEntries {
|
||||
state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value))
|
||||
return
|
||||
}
|
||||
state.GlobalMemory[op.Value] = value
|
||||
state.globalMemory[op.Value] = value
|
||||
|
||||
default:
|
||||
state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type))
|
||||
@@ -146,25 +146,25 @@ func (state *State) WriteSigned(op *Operand, value int64) {
|
||||
}
|
||||
|
||||
func (state *State) call(functionOffset int64) {
|
||||
if state.FunctionIndex+functionOffset >= int64(len(state.Functions)) {
|
||||
state.setError(fmt.Errorf("Invalid function call index: %d + %d = %d", state.FunctionIndex, functionOffset, state.FunctionIndex+functionOffset))
|
||||
if state.functionIndex+functionOffset >= int64(len(state.functions)) {
|
||||
state.setError(fmt.Errorf("Invalid function call index: %d + %d = %d", state.functionIndex, functionOffset, state.functionIndex+functionOffset))
|
||||
return
|
||||
}
|
||||
|
||||
stackFrame := &StackFrame{
|
||||
PreviousFunctionIndex: state.FunctionIndex,
|
||||
PreviousInstructionIndex: state.InstructionIndex,
|
||||
PreviousFunctionIndex: state.functionIndex,
|
||||
PreviousInstructionIndex: state.instructionIndex,
|
||||
}
|
||||
state.Stack = append(state.Stack, stackFrame)
|
||||
state.FunctionIndex += functionOffset
|
||||
state.InstructionIndex = 0
|
||||
state.stack = append(state.stack, stackFrame)
|
||||
state.functionIndex += functionOffset
|
||||
state.instructionIndex = 0
|
||||
}
|
||||
|
||||
func (state *State) ret() {
|
||||
state.FunctionIndex = state.StackFrame().PreviousFunctionIndex
|
||||
state.InstructionIndex = state.StackFrame().PreviousInstructionIndex
|
||||
state.Stack = state.Stack[:len(state.Stack)-1]
|
||||
if len(state.Stack) == 0 {
|
||||
state.Running = false
|
||||
state.functionIndex = state.StackFrame().PreviousFunctionIndex
|
||||
state.instructionIndex = state.StackFrame().PreviousInstructionIndex
|
||||
state.stack = state.stack[:len(state.stack)-1]
|
||||
if len(state.stack) == 0 {
|
||||
state.running = false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user