-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostfunc.talk.go
65 lines (52 loc) · 2.01 KB
/
hostfunc.talk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package capsule
// this hostfunction is a template for the other host functions
import (
"context"
"fmt"
"log"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// DefineHostFuncTalk defines a host function
func DefineHostFuncTalk(builder wazero.HostModuleBuilder) {
builder.NewFunctionBuilder().
WithGoModuleFunction(talk,
[]api.ValueType{
api.ValueTypeI32, // string position
api.ValueTypeI32, // string length
api.ValueTypeI32, // returned string position
api.ValueTypeI32, // returned string length
},
[]api.ValueType{api.ValueTypeI32}).
Export("hostTalk")
}
// talk : host function called by the wasm function
// and then returning data to the wasm module
var talk = api.GoModuleFunc(func(ctx context.Context, module api.Module, params []uint64) {
// Position and size of the message coming from the WASM module
position := uint32(params[0])
length := uint32(params[1])
// Read the buffer memory to retrieve the message
buffer, ok := module.Memory().Read(position, length)
if !ok {
log.Panicf("Memory.Read(%d, %d) out of range", position, length)
}
messageFromModule := string(buffer)
fmt.Println("🟣 message from the WASM module:", messageFromModule)
// Create a message from the host to reply the guest WASM module
messageFromHost := "Hello 😀 I'm the host"
messageFromHostLength := len(messageFromHost)
// This is a wasm function defined in the capsule-module-sdk
results, err := module.ExportedFunction("allocateBuffer").Call(ctx, uint64(messageFromHostLength))
if err != nil {
log.Panicln("Problem when calling allocateBuffer", err)
}
positionReturnBuffer := uint32(params[2])
lengthReturnBuffer := uint32(params[3])
allocatedPosition := uint32(results[0])
module.Memory().WriteUint32Le(positionReturnBuffer, allocatedPosition)
module.Memory().WriteUint32Le(lengthReturnBuffer, uint32(messageFromHostLength))
// add the message to the memory of the module
module.Memory().Write(allocatedPosition, []byte(messageFromHost))
params[0] = 0
})