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:
parent
1380986e5f
commit
c3764d5d60
2 changed files with 55 additions and 0 deletions
6
src/wasm_sdk/go/README.md
Normal file
6
src/wasm_sdk/go/README.md
Normal 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`
|
49
src/wasm_sdk/go/example.go
Normal file
49
src/wasm_sdk/go/example.go
Normal 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() {}
|
Loading…
Reference in a new issue