Skip to content

Commit

Permalink
interp: allow externref params for calls to/from guest modules
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 28, 2024
1 parent 12ecfa1 commit 02e3226
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
27 changes: 25 additions & 2 deletions interp/tester/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package tester

import (
"testing"
"unsafe"

"github.com/hybridgroup/mechanoid/engine"
)

type testingType struct {
val1 string
val2 string
}

func InstanceTest(t *testing.T, i engine.Interpreter) {
if err := i.Init(); err != nil {
t.Errorf("Interpreter.Init() failed: %v", err)
Expand Down Expand Up @@ -45,7 +51,7 @@ func InstanceTest(t *testing.T, i engine.Interpreter) {
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if int64(results.(int32)) != int64(3) {
if int64(results.(int64)) != int64(3) {
t.Errorf("Instance.Call() failed: %v", results)
}
})
Expand All @@ -55,7 +61,7 @@ func InstanceTest(t *testing.T, i engine.Interpreter) {
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if uint64(results.(int32)) != uint64(3) {
if uint64(results.(int64)) != uint64(3) {
t.Errorf("Instance.Call() failed: %v", results)
}
})
Expand All @@ -79,4 +85,21 @@ func InstanceTest(t *testing.T, i engine.Interpreter) {
t.Errorf("Instance.Call() failed: %v", results)
}
})

t.Run("Call externref", func(t *testing.T) {
thing := testingType{val1: "hello", val2: "world"}

// This is a hack to get the pointer value as an int32
// Externelref is an opaque type, so we can't do anything with it
// We just want to make sure that the pointer value is passed through correctly
ptr := uintptr(unsafe.Pointer(&thing)) & 0xFFFFFFFF

results, err := inst.Call("test_externref", ptr)
if err != nil {
t.Errorf("Instance.Call() failed: %v", err)
}
if uintptr(results.(int32)) != ptr {
t.Errorf("Instance.Call() incorrect: %v %v", ptr, results)
}
})
}
Binary file modified interp/tester/tester.wasm
Binary file not shown.
6 changes: 5 additions & 1 deletion interp/wasman/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func encodeArg(arg any) uint64 {
return uint64(val)
case uint64:
return uint64(val)
case uintptr:
return uint64(val)
}
panic("bad arg type")
}
Expand All @@ -86,7 +88,9 @@ func decodeResult(result uint64, vtype types.ValueType) any {
case types.ValueTypeI32:
return int32(result)
case types.ValueTypeI64:
return int32(result)
return int64(result)
case types.ValueTypeExternref:
return uintptr(result)
}
panic("unreachable")
}
6 changes: 5 additions & 1 deletion interp/wazero/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func encodeArg(arg any) uint64 {
return api.EncodeU32(val)
case uint64:
return uint64(val)
case uintptr:
return api.EncodeExternref(val)
}
panic("bad arg type")
}
Expand All @@ -75,7 +77,9 @@ func decodeResult(result uint64, vtype api.ValueType) any {
case api.ValueTypeI32:
return api.DecodeI32(result)
case api.ValueTypeI64:
return api.DecodeI32(result)
return int64(result)
case api.ValueTypeExternref:
return api.DecodeExternref(result)
}
panic("unreachable")
}

0 comments on commit 02e3226

Please sign in to comment.