diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-02-21 11:02:14 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2025-02-21 11:02:27 -0500 |
| commit | fe582b802c6a858fa0ac43ea7b1b64324ac51fe4 (patch) | |
| tree | f10ee87738f9f339ee8918c98e53398cc4a18f0f | |
| parent | ceff7a09a4ad41bb3b91369e73d93e67d7a78e28 (diff) | |
| download | x-fe582b802c6a858fa0ac43ea7b1b64324ac51fe4.tar.xz x-fe582b802c6a858fa0ac43ea7b1b64324ac51fe4.zip | |
wasm: introduce new package for host->guest buffer passing
Signed-off-by: Xe Iaso <me@xeiaso.net>
| -rw-r--r-- | go.mod | 6 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | wasm/malloc_wasip1.go | 6 | ||||
| -rw-r--r-- | wasm/wasm.go | 53 |
4 files changed, 65 insertions, 4 deletions
@@ -1,6 +1,6 @@ module within.website/x -go 1.24 +go 1.23 require ( al.essio.dev/pkg/shellescape v1.5.1 @@ -63,7 +63,7 @@ require ( github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a github.com/speps/go-hashids v2.0.0+incompatible github.com/stoewer/go-strcase v1.3.0 - github.com/tetratelabs/wazero v1.8.2 + github.com/tetratelabs/wazero v1.9.0 github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64 github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef github.com/twitchtv/twirp v8.1.3+incompatible @@ -267,5 +267,3 @@ require ( golang.org/x/tools v0.30.0 google.golang.org/protobuf v1.36.4 ) - -tool golang.org/x/tools/cmd/stringer @@ -843,6 +843,10 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4= github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= +github.com/tetratelabs/wazero v1.8.3-0.20250217185257-70a9688a9f40 h1:+8cbydI2qMGurXlnljiiqpDSIXdVxtWfB+uZPM/BAxs= +github.com/tetratelabs/wazero v1.8.3-0.20250217185257-70a9688a9f40/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= +github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I= +github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM= github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI= github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64 h1:l/T7dYuJEQZOwVOpjIXr1180aM9PZL/d1MnMVIxefX4= github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64/go.mod h1:Q1NAJOuRdQCqN/VIWdnaaEhV8LpeO2rtlBP7/iDJNII= diff --git a/wasm/malloc_wasip1.go b/wasm/malloc_wasip1.go new file mode 100644 index 0000000..d7bb0a6 --- /dev/null +++ b/wasm/malloc_wasip1.go @@ -0,0 +1,6 @@ +package wasm + +//go:wasmexport malloc +func Malloc(size uint32) Buffer { + return FromSlice(make([]byte, size)) +} diff --git a/wasm/wasm.go b/wasm/wasm.go new file mode 100644 index 0000000..5981c06 --- /dev/null +++ b/wasm/wasm.go @@ -0,0 +1,53 @@ +package wasm + +import ( + "unsafe" + + "github.com/tetratelabs/wazero/api" +) + +type String uint64 + +func (value String) Load(module api.Module) string { + return string(value.LoadBytes(module)) +} + +func (value String) LoadBytes(module api.Module) []byte { + data, ok := module.Memory().Read(uint32(value>>32), uint32(value)) + if !ok { + panic("memory read out of bounds") + } + return data +} + +func FromString(value string) String { + position := uint32(uintptr(unsafe.Pointer(unsafe.StringData(value)))) + bytes := uint32(len(value)) + return String(uint64(position)<<32 | uint64(bytes)) +} + +type Buffer uint64 + +func (buffer Buffer) Address() uint32 { + return uint32(buffer >> 32) +} + +func (buffer Buffer) Length() uint32 { + return uint32((buffer << 32) >> 32) +} + +func FromSlice(value []byte) Buffer { + if len(value) == 0 { + return 0 + } + ptr := uint64(uintptr(unsafe.Pointer(&value[0]))) + return Buffer(ptr<<32 | uint64(len(value))) +} + +func (buffer Buffer) Slice() []byte { + return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(buffer.Address()))), buffer.Length()) +} + +func (buffer Buffer) String() string { + return unsafe.String((*byte)(unsafe.Pointer(uintptr(buffer.Address()))), buffer.Length()) +} |
