Skip to content

Commit

Permalink
Removing CallFunctionContext in favor of passing context in CallFunct…
Browse files Browse the repository at this point in the history
…ion (breaking change)
  • Loading branch information
pkedy committed Feb 7, 2022
1 parent a5f3527 commit 8dff7ae
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 70 deletions.
4 changes: 3 additions & 1 deletion examples/add_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples

import (
"context"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -14,6 +15,7 @@ import (
// Test_AddInt shows how you can define a function in text format and have it compiled inline.
// See https://github.com/summerwind/the-art-of-webassembly-go/blob/main/chapter1/addint/addint.wat
func Test_AddInt(t *testing.T) {
ctx := context.Background()
mod, err := text.DecodeModule([]byte(`(module
(func $addInt ;; TODO: function exports (export "AddInt")
(param $value_1 i32) (param $value_2 i32)
Expand Down Expand Up @@ -41,7 +43,7 @@ func Test_AddInt(t *testing.T) {
{value1: 1, value2: 2, result: 3},
{value1: 5, value2: 5, result: 10},
} {
ret, retTypes, err := store.CallFunction("test", "AddInt", c.value1, c.value2)
ret, retTypes, err := store.CallFunction(ctx, "test", "AddInt", c.value1, c.value2)
require.NoError(t, err)
require.Len(t, ret, len(retTypes))
require.Equal(t, wasm.ValueTypeI32, retTypes[0])
Expand Down
4 changes: 3 additions & 1 deletion examples/fibonacci_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples

import (
"context"
_ "embed"
"testing"

Expand All @@ -16,6 +17,7 @@ import (
var fibWasm []byte

func Test_fibonacci(t *testing.T) {
ctx := context.Background()
mod, err := binary.DecodeModule(fibWasm)
require.NoError(t, err)

Expand All @@ -35,7 +37,7 @@ func Test_fibonacci(t *testing.T) {
{in: 10, exp: 55},
{in: 5, exp: 5},
} {
ret, retTypes, err := store.CallFunction("test", "fibonacci", uint64(c.in))
ret, retTypes, err := store.CallFunction(ctx, "test", "fibonacci", uint64(c.in))
require.NoError(t, err)
require.Len(t, ret, len(retTypes))
require.Equal(t, wasm.ValueTypeI32, retTypes[0])
Expand Down
4 changes: 3 additions & 1 deletion examples/file_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples

import (
"bytes"
"context"
_ "embed"
"io"
"testing"
Expand Down Expand Up @@ -46,6 +47,7 @@ func readFile(fs wasi.FS, path string) ([]byte, error) {
}

func Test_file_system(t *testing.T) {
ctx := context.Background()
mod, err := binary.DecodeModule(filesystemWasm)
require.NoError(t, err)

Expand All @@ -63,7 +65,7 @@ func Test_file_system(t *testing.T) {
err = store.Instantiate(mod, "test")
require.NoError(t, err)

_, _, err = store.CallFunction("test", "_start")
_, _, err = store.CallFunction(ctx, "test", "_start")
require.NoError(t, err)

out, err := readFile(memFS, "output.txt")
Expand Down
12 changes: 6 additions & 6 deletions examples/host_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tetratelabs/wazero/wasi"
Expand All @@ -31,14 +30,14 @@ func Test_hostFunc(t *testing.T) {
getRandomString := func(ctx *wasm.HostFunctionCallContext, retBufPtr uint32, retBufSize uint32) {
// Assert that context values passed in from CallFunctionContext are accessible.
contextValue := ctx.Value(testKey{}).(int64)
assert.Equal(t, int64(12345), contextValue)
require.Equal(t, int64(12345), contextValue)

const bufferSize = 10000 // force memory space grow to ensure eager failures on missing setup
// Allocate the in-Wasm memory region so we can store the generated string.
// Note that this is recursive call. That means that this is the VM function call during the VM function call.
// More precisely, we call test.base64 (in Wasm), and the function in turn calls this get_random_string function,
// and we call test.allocate_buffer (in Wasm) here: host->vm->host->vm.
ret, _, err := store.CallFunction("test", "allocate_buffer", bufferSize)
ret, _, err := store.CallFunction(ctx, "test", "allocate_buffer", bufferSize)
require.NoError(t, err)
require.Len(t, ret, 1)
bufAddr := ret[0]
Expand All @@ -62,17 +61,18 @@ func Test_hostFunc(t *testing.T) {
err = store.Instantiate(mod, "test")
require.NoError(t, err)

ctx := context.Background()

// We assume that TinyGo binary expose "_start" symbol
// to initialize the memory state.
// Meaning that TinyGo binary is "WASI command":
// https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
_, _, err = store.CallFunction("test", "_start")
_, _, err = store.CallFunction(ctx, "test", "_start")
require.NoError(t, err)

// Set a context variable that should be available in HostFunctionCallContext.
ctx := context.Background()
ctx = context.WithValue(ctx, testKey{}, int64(12345))

_, _, err = store.CallFunctionContext(ctx, "test", "base64", 5)
_, _, err = store.CallFunction(ctx, "test", "base64", 5)
require.NoError(t, err)
}
4 changes: 3 additions & 1 deletion examples/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples

import (
"bytes"
"context"
_ "embed"
"strings"
"testing"
Expand All @@ -18,6 +19,7 @@ import (
var stdioWasm []byte

func Test_stdio(t *testing.T) {
ctx := context.Background()
mod, err := binary.DecodeModule(stdioWasm)
require.NoError(t, err)
stdinBuf := bytes.NewBuffer([]byte("WASI\n"))
Expand All @@ -33,7 +35,7 @@ func Test_stdio(t *testing.T) {
require.NoError(t, err)
err = store.Instantiate(mod, "test")
require.NoError(t, err)
_, _, err = store.CallFunction("test", "_start")
_, _, err = store.CallFunction(ctx, "test", "_start")
require.NoError(t, err)
require.Equal(t, "Hello, WASI!", strings.TrimSpace(stdoutBuf.String()))
require.Equal(t, "Error Message", strings.TrimSpace(stderrBuf.String()))
Expand Down
11 changes: 7 additions & 4 deletions tests/bench/bench_fac_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package bench

import (
"context"
_ "embed"
"errors"
"testing"
Expand All @@ -25,14 +26,15 @@ var facWasm []byte

// TestFacIter ensures that the code in BenchmarkFacIter works as expected.
func TestFacIter(t *testing.T) {
ctx := context.Background()
const in = 30
expValue := uint64(0x865df5dd54000000)
t.Run("iter", func(t *testing.T) {
store, err := newStoreForFacIterBench(jit.NewEngine())
require.NoError(t, err)

for i := 0; i < 10000; i++ {
res, _, err := store.CallFunction("test", "fac-iter", in)
res, _, err := store.CallFunction(ctx, "test", "fac-iter", in)
require.NoError(t, err)
require.Equal(t, expValue, res[0])
}
Expand All @@ -43,7 +45,7 @@ func TestFacIter(t *testing.T) {
require.NoError(t, err)

for i := 0; i < 10000; i++ {
res, _, err := store.CallFunction("test", "fac-iter", in)
res, _, err := store.CallFunction(ctx, "test", "fac-iter", in)
require.NoError(t, err)
require.Equal(t, expValue, res[0])
}
Expand Down Expand Up @@ -116,6 +118,7 @@ func BenchmarkFacIter_Init(b *testing.B) {

// BenchmarkFacIter_Invoke benchmarks the time spent invoking a factorial calculation.
func BenchmarkFacIter_Invoke(b *testing.B) {
ctx := context.Background()
const in = 30
b.Run("interpreter", func(b *testing.B) {
store, err := newStoreForFacIterBench(interpreter.NewEngine())
Expand All @@ -124,7 +127,7 @@ func BenchmarkFacIter_Invoke(b *testing.B) {
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, _, err = store.CallFunction("test", "fac-iter", in); err != nil {
if _, _, err = store.CallFunction(ctx, "test", "fac-iter", in); err != nil {
b.Fatal(err)
}
}
Expand All @@ -136,7 +139,7 @@ func BenchmarkFacIter_Invoke(b *testing.B) {
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, _, err = store.CallFunction("test", "fac-iter", in); err != nil {
if _, _, err = store.CallFunction(ctx, "test", "fac-iter", in); err != nil {
b.Fatal(err)
}
}
Expand Down
20 changes: 13 additions & 7 deletions tests/bench/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bench

import (
"context"
_ "embed"
"encoding/binary"
"fmt"
Expand Down Expand Up @@ -48,7 +49,7 @@ func setUpStore(store *wasm.Store) {
// to initialize the memory state.
// Meaning that TinyGo binary is "WASI command":
// https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
_, _, err = store.CallFunction("test", "_start")
_, _, err = store.CallFunction(context.Background(), "test", "_start")
if err != nil {
panic(err)
}
Expand All @@ -63,11 +64,12 @@ func runAllBenches(b *testing.B, store *wasm.Store) {
}

func runBase64Benches(b *testing.B, store *wasm.Store) {
ctx := context.Background()
for _, numPerExec := range []int{5, 100, 10000} {
numPerExec := numPerExec
b.ResetTimer()
b.Run(fmt.Sprintf("base64_%d_per_exec", numPerExec), func(b *testing.B) {
_, _, err := store.CallFunction("test", "base64", uint64(numPerExec))
_, _, err := store.CallFunction(ctx, "test", "base64", uint64(numPerExec))
if err != nil {
panic(err)
}
Expand All @@ -76,12 +78,13 @@ func runBase64Benches(b *testing.B, store *wasm.Store) {
}

func runFibBenches(b *testing.B, store *wasm.Store) {
ctx := context.Background()
for _, num := range []int{5, 10, 20, 30} {
num := num
b.ResetTimer()
b.Run(fmt.Sprintf("fib_for_%d", num), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := store.CallFunction("test", "fibonacci", uint64(num))
_, _, err := store.CallFunction(ctx, "test", "fibonacci", uint64(num))
if err != nil {
panic(err)
}
Expand All @@ -91,12 +94,13 @@ func runFibBenches(b *testing.B, store *wasm.Store) {
}

func runStringsManipulationBenches(b *testing.B, store *wasm.Store) {
ctx := context.Background()
for _, initialSize := range []int{50, 100, 1000} {
initialSize := initialSize
b.ResetTimer()
b.Run(fmt.Sprintf("string_manipulation_size_%d", initialSize), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := store.CallFunction("test", "string_manipulation", uint64(initialSize))
_, _, err := store.CallFunction(ctx, "test", "string_manipulation", uint64(initialSize))
if err != nil {
panic(err)
}
Expand All @@ -106,12 +110,13 @@ func runStringsManipulationBenches(b *testing.B, store *wasm.Store) {
}

func runReverseArrayBenches(b *testing.B, store *wasm.Store) {
ctx := context.Background()
for _, arraySize := range []int{500, 1000, 10000} {
arraySize := arraySize
b.ResetTimer()
b.Run(fmt.Sprintf("reverse_array_size_%d", arraySize), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := store.CallFunction("test", "reverse_array", uint64(arraySize))
_, _, err := store.CallFunction(ctx, "test", "reverse_array", uint64(arraySize))
if err != nil {
panic(err)
}
Expand All @@ -121,12 +126,13 @@ func runReverseArrayBenches(b *testing.B, store *wasm.Store) {
}

func runRandomMatMul(b *testing.B, store *wasm.Store) {
ctx := context.Background()
for _, matrixSize := range []int{5, 10, 20} {
matrixSize := matrixSize
b.ResetTimer()
b.Run(fmt.Sprintf("random_mat_mul_size_%d", matrixSize), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := store.CallFunction("test", "random_mat_mul", uint64(matrixSize))
_, _, err := store.CallFunction(ctx, "test", "random_mat_mul", uint64(matrixSize))
if err != nil {
panic(err)
}
Expand All @@ -138,7 +144,7 @@ func runRandomMatMul(b *testing.B, store *wasm.Store) {
func newStore(engine wasm.Engine) *wasm.Store {
store := wasm.NewStore(engine)
getRandomString := func(ctx *wasm.HostFunctionCallContext, retBufPtr uint32, retBufSize uint32) {
ret, _, _ := store.CallFunction("test", "allocate_buffer", 10)
ret, _, _ := store.CallFunction(ctx, "test", "allocate_buffer", 10)
bufAddr := ret[0]
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[retBufPtr:], uint32(bufAddr))
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[retBufSize:], 10)
Expand Down
4 changes: 3 additions & 1 deletion tests/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package example

import (
"bytes"
"context"
_ "embed"
"os"
"testing"
Expand Down Expand Up @@ -91,6 +92,7 @@ func newExample() *wasm.Module {
}

func TestExampleUpToDate(t *testing.T) {
ctx := context.Background()
encoded := binary.EncodeModule(example)
// This means we changed something. Overwrite the example wasm file rather than force maintainers to use hex editor!
if !bytes.Equal(encoded, exampleBinary) {
Expand Down Expand Up @@ -130,7 +132,7 @@ func TestExampleUpToDate(t *testing.T) {
require.NoError(t, err)

// Call the add function as a smoke test
res, _, err := store.CallFunction("example", "AddInt", 1, 2)
res, _, err := store.CallFunction(ctx, "example", "AddInt", 1, 2)
require.NoError(t, err)
require.Equal(t, []uint64{3}, res)
})
Expand Down
Loading

0 comments on commit 8dff7ae

Please sign in to comment.