From 18eeb857833c3453a1237ebfd84b4da346465a35 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 28 Apr 2023 13:50:50 +0100 Subject: [PATCH] Improve mock. --- internal/util/mock.go | 49 +++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/internal/util/mock.go b/internal/util/mock.go index 32250d9..0503575 100644 --- a/internal/util/mock.go +++ b/internal/util/mock.go @@ -1,7 +1,6 @@ package util import ( - "context" "encoding/binary" "math" @@ -9,51 +8,47 @@ import ( ) func NewMockModule(size uint32) api.Module { - mem := make(mockMemory, size) - return mockModule{&mem} + mem := mockMemory{buf: make([]byte, size)} + return mockModule{&mem, nil} } type mockModule struct { memory api.Memory + api.Module } func (m mockModule) Memory() api.Memory { return m.memory } func (m mockModule) String() string { return "mockModule" } func (m mockModule) Name() string { return "mockModule" } -func (m mockModule) ExportedGlobal(name string) api.Global { return nil } -func (m mockModule) ExportedMemory(name string) api.Memory { return nil } -func (m mockModule) ExportedFunction(name string) api.Function { return nil } -func (m mockModule) ExportedMemoryDefinitions() map[string]api.MemoryDefinition { return nil } -func (m mockModule) ExportedFunctionDefinitions() map[string]api.FunctionDefinition { return nil } -func (m mockModule) CloseWithExitCode(ctx context.Context, exitCode uint32) error { return nil } -func (m mockModule) Close(context.Context) error { return nil } - -type mockMemory []byte +type mockMemory struct { + buf []byte + api.Memory +} func (m mockMemory) Definition() api.MemoryDefinition { return nil } -func (m mockMemory) Size() uint32 { return uint32(len(m)) } +func (m mockMemory) Size() uint32 { return uint32(len(m.buf)) } func (m mockMemory) ReadByte(offset uint32) (byte, bool) { if offset >= m.Size() { return 0, false } - return m[offset], true + return m.buf[offset], true } func (m mockMemory) ReadUint16Le(offset uint32) (uint16, bool) { if !m.hasSize(offset, 2) { return 0, false } - return binary.LittleEndian.Uint16(m[offset : offset+2]), true + return binary.LittleEndian.Uint16(m.buf[offset : offset+2]), true } func (m mockMemory) ReadUint32Le(offset uint32) (uint32, bool) { if !m.hasSize(offset, 4) { return 0, false } - return binary.LittleEndian.Uint32(m[offset : offset+4]), true + return binary.LittleEndian.Uint32(m.buf[offset : offset+4]), true } func (m mockMemory) ReadFloat32Le(offset uint32) (float32, bool) { @@ -68,7 +63,7 @@ func (m mockMemory) ReadUint64Le(offset uint32) (uint64, bool) { if !m.hasSize(offset, 8) { return 0, false } - return binary.LittleEndian.Uint64(m[offset : offset+8]), true + return binary.LittleEndian.Uint64(m.buf[offset : offset+8]), true } func (m mockMemory) ReadFloat64Le(offset uint32) (float64, bool) { @@ -83,14 +78,14 @@ func (m mockMemory) Read(offset, byteCount uint32) ([]byte, bool) { if !m.hasSize(offset, byteCount) { return nil, false } - return m[offset : offset+byteCount : offset+byteCount], true + return m.buf[offset : offset+byteCount : offset+byteCount], true } func (m mockMemory) WriteByte(offset uint32, v byte) bool { if offset >= m.Size() { return false } - m[offset] = v + m.buf[offset] = v return true } @@ -98,7 +93,7 @@ func (m mockMemory) WriteUint16Le(offset uint32, v uint16) bool { if !m.hasSize(offset, 2) { return false } - binary.LittleEndian.PutUint16(m[offset:], v) + binary.LittleEndian.PutUint16(m.buf[offset:], v) return true } @@ -106,7 +101,7 @@ func (m mockMemory) WriteUint32Le(offset, v uint32) bool { if !m.hasSize(offset, 4) { return false } - binary.LittleEndian.PutUint32(m[offset:], v) + binary.LittleEndian.PutUint32(m.buf[offset:], v) return true } @@ -118,7 +113,7 @@ func (m mockMemory) WriteUint64Le(offset uint32, v uint64) bool { if !m.hasSize(offset, 8) { return false } - binary.LittleEndian.PutUint64(m[offset:], v) + binary.LittleEndian.PutUint64(m.buf[offset:], v) return true } @@ -130,7 +125,7 @@ func (m mockMemory) Write(offset uint32, val []byte) bool { if !m.hasSize(offset, uint32(len(val))) { return false } - copy(m[offset:], val) + copy(m.buf[offset:], val) return true } @@ -138,16 +133,16 @@ func (m mockMemory) WriteString(offset uint32, val string) bool { if !m.hasSize(offset, uint32(len(val))) { return false } - copy(m[offset:], val) + copy(m.buf[offset:], val) return true } func (m *mockMemory) Grow(delta uint32) (result uint32, ok bool) { - prev := (len(*m) + 65535) / 65536 - *m = append(*m, make([]byte, 65536*delta)...) + prev := (len(m.buf) + 65535) / 65536 + m.buf = append(m.buf, make([]byte, 65536*delta)...) return uint32(prev), true } func (m mockMemory) hasSize(offset uint32, byteCount uint32) bool { - return uint64(offset)+uint64(byteCount) <= uint64(len(m)) + return uint64(offset)+uint64(byteCount) <= uint64(len(m.buf)) }