-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
67 lines (54 loc) · 1.3 KB
/
main.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
66
67
package main
import (
"C"
"fmt"
"os"
"github.com/c0mm4nd/wasman"
"github.com/c0mm4nd/wasman/config"
)
import "unsafe"
// Run me on root folder
// go run ./examples/log
func main() {
linker1 := wasman.NewLinker(config.LinkerConfig{})
// cannot call host func in the host func
err := linker1.DefineAdvancedFunc("env", "log_message", func(ins *wasman.Instance) interface{} {
return func(ptr uint32, l uint32) {
// need ptr & l
messageByLen := ins.Memory.Value[int(ptr):int(ptr+l)]
fmt.Println(string(messageByLen))
// this method just need one ptr
messageByCharVec := C.GoString((*C.char)(unsafe.Pointer(&ins.Memory.Value[ptr])))
fmt.Println(messageByCharVec)
}
})
if err != nil {
panic(err)
}
wasm, err := os.Open("examples/log.wasm")
if err != nil {
panic(err)
}
module, err := wasman.NewModule(config.ModuleConfig{}, wasm)
if err != nil {
panic(err)
}
ins, err := linker1.Instantiate(module)
if err != nil {
panic(err)
}
name := "wasman engine"
ret, _, err := ins.CallExportedFunc("allocate", uint64(len(name)+1))
if err != nil {
panic(err)
}
ptr := ret[0]
copy(ins.Memory.Value[ptr:], name)
for range make([]byte, 100) {
_, _, err = ins.CallExportedFunc("greet", ptr)
if err != nil {
panic(err)
}
}
fmt.Println("mem size", len(ins.Memory.Value))
}