Skip to content

Commit

Permalink
interp/wazero: corrections to encode/decode params
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 27, 2024
1 parent 2ff9b09 commit 40dbf00
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
6 changes: 4 additions & 2 deletions interp/wazero/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (i *Instance) Call(name string, args ...any) (any, error) {
}

func encodeArgs(args []any) []uint64 {
encoded := make([]uint64, len(args))
encoded := make([]uint64, 0, len(args))
for _, arg := range args {
encoded = append(encoded, encodeArg(arg))
}
Expand All @@ -51,12 +51,14 @@ func encodeArg(arg any) uint64 {
return api.EncodeF64(val)
case uint32:
return api.EncodeU32(val)
case uint64:
return uint64(val)
}
panic("bad arg type")
}

func decodeResults(results []uint64, vtypes []api.ValueType) []any {
decoded := make([]any, len(results))
decoded := make([]any, 0, len(results))
for i, result := range results {
vtype := vtypes[i]
decoded = append(decoded, decodeResult(result, vtype))
Expand Down
85 changes: 85 additions & 0 deletions interp/wazero/instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package wazero

import (
_ "embed"
"testing"
)

//go:embed tester.wasm
var wasmData []byte

func TestInstance(t *testing.T) {
i := Interpreter{}
if err := i.Init(); err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
}

if err := i.Load(wasmData); err != nil {
t.Errorf("Interpreter.Load() failed: %v", err)
}

inst, err := i.Run()
if err != nil {
t.Errorf("Interpreter.Run() failed: %v", err)
}

t.Run("Call int32", func(t *testing.T) {
results, err := inst.Call("test_int32", int32(1), int32(2))
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if results != int32(3) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call uint32", func(t *testing.T) {
results, err := inst.Call("test_uint32", uint32(1), uint32(2))
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if uint32(results.(int32)) != uint32(3) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call int64", func(t *testing.T) {
results, err := inst.Call("test_int64", int64(1), int64(2))
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if int64(results.(int32)) != int64(3) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call uint64", func(t *testing.T) {
results, err := inst.Call("test_uint64", uint64(1), uint64(2))
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if uint64(results.(int32)) != uint64(3) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call float32", func(t *testing.T) {
results, err := inst.Call("test_float32", float32(100.2), float32(300.8))
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if results != float32(401.0) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call float64", func(t *testing.T) {
results, err := inst.Call("test_float64", float64(111.2), float64(333.8))
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if results != float64(445.0) {
t.Errorf("Instance.Call() failed: %v", results)
}
})
}
36 changes: 36 additions & 0 deletions interp/wazero/interp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wazero

import (
"testing"
)

func TestName(t *testing.T) {
i := Interpreter{}
if i.Name() != "wazero" {
t.Errorf("Interpreter.Name() failed: %v", i.Name())
}
}

func TestInit(t *testing.T) {
i := Interpreter{}
err := i.Init()
if err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
}
}

func TestLoad(t *testing.T) {
t.Skip("TODO: implement TestLoad")
}

func TestRun(t *testing.T) {
t.Skip("TODO: implement TestRun")
}

func TestHalt(t *testing.T) {
t.Skip("TODO: implement TestHalt")
}

func TestDefineFunc(t *testing.T) {
t.Skip("TODO: implement TestDefineFunc")
}
Binary file added interp/wazero/tester.wasm
Binary file not shown.

0 comments on commit 40dbf00

Please sign in to comment.