Full test (which fails)
This commit is contained in:
20
vm/state.go
20
vm/state.go
@@ -13,14 +13,14 @@ type State struct {
|
||||
instructionIndex int64
|
||||
|
||||
comparisonResult bool
|
||||
globalMemory *memory
|
||||
globalMemory *Memory
|
||||
stack []*stackFrame
|
||||
}
|
||||
|
||||
func NewState(functions [][]*Instruction) (*State, error) {
|
||||
return &State{
|
||||
functions: functions,
|
||||
globalMemory: newMemory(16),
|
||||
globalMemory: NewMemory(16),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ func (state *State) Execute() error {
|
||||
return state.err
|
||||
}
|
||||
|
||||
func (state *State) GlobalMemory() *Memory {
|
||||
return state.globalMemory
|
||||
}
|
||||
|
||||
func (state *State) stackFrame() *stackFrame {
|
||||
return state.stack[len(state.stack)-1]
|
||||
}
|
||||
@@ -83,6 +87,10 @@ func (state *State) processInstruction() {
|
||||
instr := fnc[state.instructionIndex]
|
||||
state.instructionIndex += 1
|
||||
instr.opHandler(state, instr)
|
||||
|
||||
if state.instructionIndex >= int64(len(fnc)) {
|
||||
state.ret()
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) readUnsigned(op *Operand) uint64 {
|
||||
@@ -91,14 +99,14 @@ func (state *State) readUnsigned(op *Operand) uint64 {
|
||||
return op.Value
|
||||
|
||||
case FunctionMemoryIndex:
|
||||
value, err := state.stackFrame().functionMemory.readUnsigned(op.Value)
|
||||
value, err := state.stackFrame().functionMemory.ReadUnsigned(op.Value)
|
||||
if err != nil {
|
||||
state.setError(err)
|
||||
}
|
||||
return value
|
||||
|
||||
case GlobalMemoryIndex:
|
||||
value, err := state.globalMemory.readUnsigned(op.Value)
|
||||
value, err := state.globalMemory.ReadUnsigned(op.Value)
|
||||
if err != nil {
|
||||
state.setError(err)
|
||||
}
|
||||
@@ -120,13 +128,13 @@ func (state *State) writeUnsigned(op *Operand, value uint64) {
|
||||
state.setError(fmt.Errorf("Write to literal operand"))
|
||||
|
||||
case FunctionMemoryIndex:
|
||||
err := state.stackFrame().functionMemory.writeUnsigned(op.Value, value)
|
||||
err := state.stackFrame().functionMemory.WriteUnsigned(op.Value, value)
|
||||
if err != nil {
|
||||
state.setError(err)
|
||||
}
|
||||
|
||||
case GlobalMemoryIndex:
|
||||
err := state.globalMemory.writeUnsigned(op.Value, value)
|
||||
err := state.globalMemory.WriteUnsigned(op.Value, value)
|
||||
if err != nil {
|
||||
state.setError(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user