Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds host function interfaces #246

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions examples/host_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_hostFunc(t *testing.T) {
store := wasm.NewStore(interpreter.NewEngine())

// Host-side implementation of get_random_string on Wasm import.
getRandomString := func(ctx *wasm.HostFunctionCallContext, retBufPtr uint32, retBufSize uint32) {
getRandomString := func(ctx wasm.HostFunctionCallContext, retBufPtr uint32, retBufSize uint32) {
// Assert that context values passed in from CallFunctionContext are accessible.
contextValue := ctx.Context().Value(testKey{}).(int64)
require.Equal(t, int64(12345), contextValue)
Expand All @@ -43,11 +43,14 @@ func Test_hostFunc(t *testing.T) {
bufAddr := ret[0]

// Store the address info to the memory.
ctx.Memory.PutUint32(retBufPtr, uint32(bufAddr))
ctx.Memory.PutUint32(retBufSize, uint32(bufferSize))
require.True(t, ctx.Memory().WriteUint32Le(retBufPtr, uint32(bufAddr)))
require.True(t, ctx.Memory().WriteUint32Le(retBufSize, uint32(bufferSize)))

// Now store the random values in the region.
n, err := rand.Read(ctx.Memory.Buffer[bufAddr : bufAddr+bufferSize])
b, ok := ctx.Memory().Read(uint32(bufAddr), bufferSize)
require.True(t, ok)

n, err := rand.Read(b)
require.NoError(t, err)
require.Equal(t, bufferSize, n)
}
Expand All @@ -70,7 +73,7 @@ func Test_hostFunc(t *testing.T) {
_, _, err = store.CallFunction(ctx, "test", "_start")
require.NoError(t, err)

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

_, _, err = store.CallFunction(ctx, "test", "base64", 5)
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_Simple(t *testing.T) {
store := wasm.NewStore(interpreter.NewEngine())

stdout := new(bytes.Buffer)
hostFunction := func(_ *wasm.HostFunctionCallContext) {
hostFunction := func(wasm.HostFunctionCallContext) {
_, _ = fmt.Fprintln(stdout, "hello!")
}

Expand Down
13 changes: 7 additions & 6 deletions tests/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bench
import (
"context"
_ "embed"
"encoding/binary"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -143,12 +142,14 @@ 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) {
getRandomString := func(ctx wasm.HostFunctionCallContext, retBufPtr uint32, retBufSize uint32) {
ret, _, _ := store.CallFunction(ctx.Context(), "test", "allocate_buffer", 10)
bufAddr := ret[0]
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[retBufPtr:], uint32(bufAddr))
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[retBufSize:], 10)
_, _ = rand.Read(ctx.Memory.Buffer[bufAddr : bufAddr+10])
bufAddr := uint32(ret[0])
ctx.Memory().WriteUint32Le(retBufPtr, bufAddr)
ctx.Memory().WriteUint32Le(retBufSize, 10)
b := make([]byte, 10)
_, _ = rand.Read(b)
ctx.Memory().Write(bufAddr, b)
}

_ = store.AddHostFunction("env", "get_random_string", reflect.ValueOf(getRandomString))
Expand Down
14 changes: 7 additions & 7 deletions tests/engine/adhoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func unreachable(t *testing.T, newEngine func() wasm.Engine) {

const moduleName = "test"

callUnreachable := func(ctx *wasm.HostFunctionCallContext) {
callUnreachable := func(ctx wasm.HostFunctionCallContext) {
_, _, err := store.CallFunction(ctx.Context(), moduleName, "unreachable_func")
require.NoError(t, err)
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func recursiveEntry(t *testing.T, newEngine func() wasm.Engine) {

store := wasm.NewStore(newEngine())

hostfunc := func(ctx *wasm.HostFunctionCallContext) {
hostfunc := func(ctx wasm.HostFunctionCallContext) {
_, _, err := store.CallFunction(ctx.Context(), "test", "called_by_host_func")
require.NoError(t, err)
}
Expand All @@ -201,7 +201,7 @@ func recursiveEntry(t *testing.T, newEngine func() wasm.Engine) {
}

// importedAndExportedFunc fails if the engine cannot call an "imported-and-then-exported-back" function
// Notably, this uses memory, which ensures wasm.HostFunctionCallContext is valid in both interpreter and JIT engines.
// Notably, this uses memory, which ensures api.HostFunctionCallContext is valid in both interpreter and JIT engines.
func importedAndExportedFunc(t *testing.T, newEngine func() wasm.Engine) {
ctx := context.Background()
mod, err := text.DecodeModule([]byte(`(module $test
Expand All @@ -216,8 +216,8 @@ func importedAndExportedFunc(t *testing.T, newEngine func() wasm.Engine) {

store := wasm.NewStore(newEngine())

storeInt := func(ctx *wasm.HostFunctionCallContext, offset uint32, val uint64) uint32 {
if !ctx.Memory.PutUint64(offset, val) {
storeInt := func(ctx wasm.HostFunctionCallContext, offset uint32, val uint64) uint32 {
if !ctx.Memory().WriteUint64Le(offset, val) {
return 1
}
return 0
Expand Down Expand Up @@ -267,7 +267,7 @@ func hostFuncWithFloatParam(t *testing.T, newEngine func() wasm.Engine) {
{
testName: "host function with f32 param",
floatType: wasm.ValueTypeF32,
identityFloatFunc: reflect.ValueOf(func(ctx *wasm.HostFunctionCallContext, value float32) float32 {
identityFloatFunc: reflect.ValueOf(func(ctx wasm.HostFunctionCallContext, value float32) float32 {
return value
}),
floatParam: uint64(math.Float32bits(math.MaxFloat32)), // float bits as a uint32 value, but casted to uint64 to be passed to CallFunction
Expand All @@ -276,7 +276,7 @@ func hostFuncWithFloatParam(t *testing.T, newEngine func() wasm.Engine) {
{
testName: "host function with f64 param",
floatType: wasm.ValueTypeF64,
identityFloatFunc: reflect.ValueOf(func(ctx *wasm.HostFunctionCallContext, value float64) float64 {
identityFloatFunc: reflect.ValueOf(func(ctx wasm.HostFunctionCallContext, value float64) float64 {
return value
}),
floatParam: math.Float64bits(math.MaxFloat64),
Expand Down
14 changes: 7 additions & 7 deletions tests/spectest/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ func (c command) expectedError() (err error) {

func addSpectestModule(t *testing.T, store *wasm.Store) {
for n, v := range map[string]reflect.Value{
"print": reflect.ValueOf(func(*wasm.HostFunctionCallContext) {}),
"print_i32": reflect.ValueOf(func(*wasm.HostFunctionCallContext, uint32) {}),
"print_f32": reflect.ValueOf(func(*wasm.HostFunctionCallContext, float32) {}),
"print_i64": reflect.ValueOf(func(*wasm.HostFunctionCallContext, uint64) {}),
"print_f64": reflect.ValueOf(func(*wasm.HostFunctionCallContext, float64) {}),
"print_i32_f32": reflect.ValueOf(func(*wasm.HostFunctionCallContext, uint32, float32) {}),
"print_f64_f64": reflect.ValueOf(func(*wasm.HostFunctionCallContext, float64, float64) {}),
"print": reflect.ValueOf(func(wasm.HostFunctionCallContext) {}),
"print_i32": reflect.ValueOf(func(wasm.HostFunctionCallContext, uint32) {}),
"print_f32": reflect.ValueOf(func(wasm.HostFunctionCallContext, float32) {}),
"print_i64": reflect.ValueOf(func(wasm.HostFunctionCallContext, uint64) {}),
"print_f64": reflect.ValueOf(func(wasm.HostFunctionCallContext, float64) {}),
"print_i32_f32": reflect.ValueOf(func(wasm.HostFunctionCallContext, uint32, float32) {}),
"print_f64_f64": reflect.ValueOf(func(wasm.HostFunctionCallContext, float64, float64) {}),
} {
require.NoError(t, store.AddHostFunction("spectest", n, v), "AddHostFunction(%s)", n)
}
Expand Down
Loading