Make memory an object

This commit is contained in:
Ian Gulliver
2021-11-16 16:15:46 -10:00
parent 1fdeaaf4cc
commit 012d5faecf
3 changed files with 51 additions and 12 deletions

View File

@@ -15,13 +15,14 @@ type State struct {
instructionIndex int64
comparisonResult bool
globalMemory [globalMemoryEntries]uint64
globalMemory *memory
stack []*stackFrame
}
func NewState(functions [][]*Instruction) (*State, error) {
return &State{
functions: functions,
functions: functions,
globalMemory: newMemory(globalMemoryEntries),
}, nil
}
@@ -97,11 +98,11 @@ func (state *State) readUnsigned(op *Operand) uint64 {
return state.stackFrame().functionMemory[op.Value]
case GlobalMemoryIndex:
if op.Value >= globalMemoryEntries {
state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value))
return 0
value, err := state.globalMemory.readUnsigned(op.Value)
if err != nil {
state.setError(err)
}
return state.globalMemory[op.Value]
return value
default:
state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type))
@@ -126,11 +127,10 @@ func (state *State) writeUnsigned(op *Operand, value uint64) {
state.stackFrame().functionMemory[op.Value] = value
case GlobalMemoryIndex:
if op.Value >= globalMemoryEntries {
state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value))
return
err := state.globalMemory.writeUnsigned(op.Value, value)
if err != nil {
state.setError(err)
}
state.globalMemory[op.Value] = value
default:
state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type))