-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostfunc.print.go
48 lines (39 loc) · 1.2 KB
/
hostfunc.print.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"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// printString : print a string to the console
var printString = api.GoModuleFunc(func(ctx context.Context, module api.Module, params []uint64) {
// Extract the position and size of the returned value
// 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(string(buffer))
params[0] = 0 // return 0
})
// DefineHostFuncPrint defines the hostPrintString function which takes in a
// string position and string length as parameters, and returns an integer.
//
// builder: The HostModuleBuilder object.
//
// Returns: None.
func DefineHostFuncPrint(builder wazero.HostModuleBuilder) {
// hostPrintString
builder.NewFunctionBuilder().
WithGoModuleFunction(printString,
[]api.ValueType{
api.ValueTypeI32, // string position
api.ValueTypeI32, // string length
},
[]api.ValueType{api.ValueTypeI32}).
Export("hostPrintString")
}
// DefineHostFuncPrint defines a host function