-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostfunc.log.go
48 lines (39 loc) · 1.22 KB
/
hostfunc.log.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
package capsule
import (
"context"
"fmt"
"log"
"time"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// logString : print a string to the console
var logString = api.GoModuleFunc(func(ctx context.Context, module api.Module, params []uint64) {
// Extract the position and size of the returned value
// We use only one value, but we can choose another way
// position, length := UnPackPosSize(stack[0])
position := uint32(params[0])
length := uint32(params[1])
buffer, ok := module.Memory().Read(position, length)
if !ok {
log.Panicf("Memory.Read(%d, %d) out of range", position, length)
}
fmt.Println(time.Now(), ":", string(buffer))
params[0] = 0 // return 0
})
// DefineHostFuncLog defines and exports a host module function called hostLogString.
// This function takes two parameters:
// - string position (i32)
// - string length (i32)
// It returns an i32 value.
func DefineHostFuncLog(builder wazero.HostModuleBuilder) {
// hostLogString
builder.NewFunctionBuilder().
WithGoModuleFunction(logString,
[]api.ValueType{
api.ValueTypeI32, // string position
api.ValueTypeI32, // string length
},
[]api.ValueType{api.ValueTypeI32}).
Export("hostLogString")
}