Skip to content

Commit

Permalink
Test, docs.
Browse files Browse the repository at this point in the history
Signed-off-by: Nuno Cruces <[email protected]>
  • Loading branch information
ncruces committed Sep 15, 2024
1 parent 8452e59 commit 20de8b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions experimental/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type LinearMemory interface {
// Notes:
// - To back a shared memory, Reallocate can't change the address of the
// backing []byte (only its length/capacity may change).
// - Reallocate may return nil if fails to grow the LinearMemory. This
// condition may or may not be handled gracefully by the Wasm module.
Reallocate(size uint64) []byte
// Free the backing memory buffer.
Free()
Expand Down
14 changes: 11 additions & 3 deletions internal/wasm/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ func TestMemoryBytesNumToPages(t *testing.T) {

func TestMemoryInstance_Grow_Size(t *testing.T) {
tests := []struct {
name string
capEqualsMax bool
expAllocator bool
name string
capEqualsMax bool
expAllocator bool
failAllocator bool
}{
{name: ""},
{name: "capEqualsMax", capEqualsMax: true},
{name: "expAllocator", expAllocator: true},
{name: "failAllocator", failAllocator: true},
}

for _, tt := range tests {
Expand All @@ -58,6 +60,9 @@ func TestMemoryInstance_Grow_Size(t *testing.T) {
case tc.expAllocator:
expBuffer := sliceAllocator(0, maxBytes)
m = &MemoryInstance{Max: max, Buffer: expBuffer.Reallocate(0), expBuffer: expBuffer}
case tc.failAllocator:
expBuffer := sliceAllocator(0, maxBytes)
m = &MemoryInstance{Max: max * 2, Buffer: expBuffer.Reallocate(0), expBuffer: expBuffer}
}
m.ownerModuleEngine = me

Expand Down Expand Up @@ -1023,6 +1028,9 @@ type sliceBuffer struct {
func (b *sliceBuffer) Free() {}

func (b *sliceBuffer) Reallocate(size uint64) []byte {
if size > b.max {
return nil
}
if cap := uint64(cap(b.buf)); size > cap {
b.buf = append(b.buf[:cap], make([]byte, size-cap)...)
} else {
Expand Down

0 comments on commit 20de8b0

Please sign in to comment.