1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00

chore(wasm): add Go SDK (#3059)

This commit is contained in:
Kostas Kyrimis 2024-05-24 10:54:23 +03:00 committed by GitHub
parent 1380986e5f
commit c3764d5d60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 0 deletions

View file

@ -0,0 +1,6 @@
# Dragonfly Go SDK for wasm
1. Download tiny go
2. Compile with wasi as target: `tinygo build -o go.wasm -target=wasi example.go`
3. Load the module at startup via `WASMPATH`
4. Call an exported function via `WASMCALL go.wasm go_hi`

View file

@ -0,0 +1,49 @@
package main
import "encoding/binary"
import "strings"
var buffer []byte
//export provide_buffer
func provide_buffer(bytes int) *byte {
buffer = []byte(strings.Repeat("x", bytes))
return &buffer[0]
}
//go:wasm-module dragonfly
//export call
func call(str *byte)
func toByteArray(i int) (arr []byte) {
arr = []byte("xxxx")
binary.LittleEndian.PutUint32(arr[0:4], uint32(i))
return
}
func send(args... string) string {
var data []byte
data = append(data, toByteArray(len(args))...)
data = data[0:4]
for _, element := range args {
var sz = toByteArray(len(element))
var payload = []byte(element)
data = append(data, sz...)
data = append(data, payload...)
}
call(&data[0])
return string(buffer)
}
//export go_hi
func go_hi() {
var res = send("set", "foo", "bar");
println("Result is ", res)
res = send("get", "foo");
println("Result is ", res)
}
// main is required for the `wasi` target, even if it isn't used.
func main() {}