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

Externref params #9

Merged
merged 2 commits into from
Feb 29, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
replace github.com/tetratelabs/wazero => github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4

require (
github.com/hybridgroup/wasman v0.0.0-20240223122031-5eaa03843b74
github.com/hybridgroup/wasman v0.0.0-20240228124029-ecd9fd3f900a
github.com/tetratelabs/wazero v1.6.0
github.com/urfave/cli/v2 v2.27.1
tinygo.org/x/tinyfs v0.3.1-0.20231212053859-32ae3f6bbad9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/hybridgroup/wasman v0.0.0-20240223122031-5eaa03843b74 h1:kTeT4KPA7SW0lyN0+a39F07Y6/F01pQc6/UWlXlLpd8=
github.com/hybridgroup/wasman v0.0.0-20240223122031-5eaa03843b74/go.mod h1:rLavUo4P0xVcDeDnViYEpPQPoACmp1py9UTLPY/R7Lg=
github.com/hybridgroup/wasman v0.0.0-20240228124029-ecd9fd3f900a h1:6q1Gn1MR643sBn4PnvgX40jCCXmAyjuNXFfk8wirpds=
github.com/hybridgroup/wasman v0.0.0-20240228124029-ecd9fd3f900a/go.mod h1:rLavUo4P0xVcDeDnViYEpPQPoACmp1py9UTLPY/R7Lg=
github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4 h1:MUh9e2izck9aROiwDsDm24UU7kHieYM2911U1t+NASs=
github.com/orsinium-forks/wazero v0.0.0-20240217173836-b12c024bcbe4/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down
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")
}