Private members
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
package vm
|
package vm
|
||||||
|
|
||||||
const FunctionMemoryEntries = 16
|
const functionMemoryEntries = 16
|
||||||
|
|
||||||
type StackFrame struct {
|
type stackFrame struct {
|
||||||
PreviousFunctionIndex int64
|
previousFunctionIndex int64
|
||||||
PreviousInstructionIndex int64
|
previousInstructionIndex int64
|
||||||
FunctionMemory [FunctionMemoryEntries]uint64
|
functionMemory [functionMemoryEntries]uint64
|
||||||
}
|
}
|
||||||
|
|||||||
22
state.go
22
state.go
@@ -16,7 +16,7 @@ type State struct {
|
|||||||
|
|
||||||
comparisonResult bool
|
comparisonResult bool
|
||||||
globalMemory [globalMemoryEntries]uint64
|
globalMemory [globalMemoryEntries]uint64
|
||||||
stack []*StackFrame
|
stack []*stackFrame
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewState(functions [][]*Instruction) (*State, error) {
|
func NewState(functions [][]*Instruction) (*State, error) {
|
||||||
@@ -50,7 +50,7 @@ func (state *State) Execute() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) stackFrame() *StackFrame {
|
func (state *State) stackFrame() *stackFrame {
|
||||||
return state.stack[len(state.stack)-1]
|
return state.stack[len(state.stack)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,11 +90,11 @@ func (state *State) readUnsigned(op *Operand) uint64 {
|
|||||||
return op.Value
|
return op.Value
|
||||||
|
|
||||||
case FunctionMemoryIndex:
|
case FunctionMemoryIndex:
|
||||||
if op.Value >= FunctionMemoryEntries {
|
if op.Value >= functionMemoryEntries {
|
||||||
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return state.stackFrame().FunctionMemory[op.Value]
|
return state.stackFrame().functionMemory[op.Value]
|
||||||
|
|
||||||
case GlobalMemoryIndex:
|
case GlobalMemoryIndex:
|
||||||
if op.Value >= globalMemoryEntries {
|
if op.Value >= globalMemoryEntries {
|
||||||
@@ -119,11 +119,11 @@ func (state *State) writeUnsigned(op *Operand, value uint64) {
|
|||||||
state.setError(fmt.Errorf("Write to literal operand"))
|
state.setError(fmt.Errorf("Write to literal operand"))
|
||||||
|
|
||||||
case FunctionMemoryIndex:
|
case FunctionMemoryIndex:
|
||||||
if op.Value >= FunctionMemoryEntries {
|
if op.Value >= functionMemoryEntries {
|
||||||
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
state.stackFrame().FunctionMemory[op.Value] = value
|
state.stackFrame().functionMemory[op.Value] = value
|
||||||
|
|
||||||
case GlobalMemoryIndex:
|
case GlobalMemoryIndex:
|
||||||
if op.Value >= globalMemoryEntries {
|
if op.Value >= globalMemoryEntries {
|
||||||
@@ -147,9 +147,9 @@ func (state *State) call(functionOffset int64) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
stackFrame := &StackFrame{
|
stackFrame := &stackFrame{
|
||||||
PreviousFunctionIndex: state.functionIndex,
|
previousFunctionIndex: state.functionIndex,
|
||||||
PreviousInstructionIndex: state.instructionIndex,
|
previousInstructionIndex: state.instructionIndex,
|
||||||
}
|
}
|
||||||
state.stack = append(state.stack, stackFrame)
|
state.stack = append(state.stack, stackFrame)
|
||||||
state.functionIndex += functionOffset
|
state.functionIndex += functionOffset
|
||||||
@@ -157,8 +157,8 @@ func (state *State) call(functionOffset int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) ret() {
|
func (state *State) ret() {
|
||||||
state.functionIndex = state.stackFrame().PreviousFunctionIndex
|
state.functionIndex = state.stackFrame().previousFunctionIndex
|
||||||
state.instructionIndex = state.stackFrame().PreviousInstructionIndex
|
state.instructionIndex = state.stackFrame().previousInstructionIndex
|
||||||
state.stack = state.stack[:len(state.stack)-1]
|
state.stack = state.stack[:len(state.stack)-1]
|
||||||
if len(state.stack) == 0 {
|
if len(state.stack) == 0 {
|
||||||
state.running = false
|
state.running = false
|
||||||
|
|||||||
Reference in New Issue
Block a user