Make memory an object
This commit is contained in:
39
memory.go
Normal file
39
memory.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package vm
|
||||
|
||||
import "fmt"
|
||||
|
||||
type memory struct {
|
||||
entries []uint64
|
||||
}
|
||||
|
||||
func newMemory(size uint64) *memory {
|
||||
return &memory{
|
||||
entries: make([]uint64, size),
|
||||
}
|
||||
}
|
||||
|
||||
func (mem *memory) readUnsigned(index uint64) (uint64, error) {
|
||||
if index >= uint64(len(mem.entries)) {
|
||||
return 0, fmt.Errorf("Invalid memory index: %016x", index)
|
||||
}
|
||||
|
||||
return mem.entries[index], nil
|
||||
}
|
||||
|
||||
func (mem *memory) mustReadUnsigned(index uint64) uint64 {
|
||||
value, err := mem.readUnsigned(index)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (mem *memory) writeUnsigned(index uint64, value uint64) error {
|
||||
if index >= uint64(len(mem.entries)) {
|
||||
return fmt.Errorf("Invalid memory index: %016x", index)
|
||||
}
|
||||
|
||||
mem.entries[index] = value
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user