Skip to content

Commit

Permalink
fix: make sure output is copied after a function call (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez authored Aug 6, 2023
1 parent 9151eaf commit 8b0bc35
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,12 @@ func (plugin *Plugin) GetOutput() ([]byte, error) {
return []byte{}, err
}
mem, _ := plugin.Memory().Read(uint32(outputOffs[0]), uint32(outputLen[0]))
return mem, nil

// Make sure output is copied, because `Read` returns a write-through view
buffer := make([]byte, len(mem))
copy(buffer, mem)

return buffer, nil
}

// Memory returns the plugin's WebAssembly memory interface.
Expand Down
23 changes: 23 additions & 0 deletions extism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,29 @@ func TestCountVowels(t *testing.T) {
}
}

func TestMultipleCallsOutput(t *testing.T) {
manifest := manifest("count_vowels.wasm")

if plugin, ok := plugin(t, manifest); ok {
defer plugin.Close()

exit, output1, err := plugin.Call("count_vowels", []byte("aaa"))

if !assertCall(t, err, exit) {
return
}

exit, output2, err := plugin.Call("count_vowels", []byte("bbb"))

if !assertCall(t, err, exit) {
return
}

assert.Equal(t, `{"count": 3}`, string(output1))
assert.Equal(t, `{"count": 0}`, string(output2))
}
}

func TestHelloHaskell(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
Expand Down

0 comments on commit 8b0bc35

Please sign in to comment.