-
Notifications
You must be signed in to change notification settings - Fork 8
/
bindgen_funcs.go
94 lines (78 loc) · 2.51 KB
/
bindgen_funcs.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"os"
"github.com/second-state/WasmEdge-go/wasmedge"
bindgen "github.com/second-state/wasmedge-bindgen/host/go"
)
func main() {
/// Expected Args[0]: program name (./bindgen_funcs)
/// Expected Args[1]: wasm file (rust_bindgen_funcs_lib.wasm))
/// Set not to print debug info
wasmedge.SetLogErrorLevel()
/// Create configure
var conf = wasmedge.NewConfigure(wasmedge.WASI)
/// Create VM with configure
var vm = wasmedge.NewVMWithConfig(conf)
/// Init WASI
var wasi = vm.GetImportModule(wasmedge.WASI)
wasi.InitWasi(
os.Args[1:], /// The args
os.Environ(), /// The envs
[]string{".:."}, /// The mapping preopens
)
/// Load and validate the wasm
vm.LoadWasmFile(os.Args[1])
vm.Validate()
// Instantiate the bindgen and vm
bg := bindgen.New(vm)
bg.Instantiate()
/// create_line: string, string, string -> string (inputs are JSON stringified)
res, _, err := bg.Execute("create_line", "{\"x\":2.5,\"y\":7.8}", "{\"x\":2.5,\"y\":5.8}", "A thin red line")
if err == nil {
fmt.Println("Run bindgen -- create_line:", res[0].(string))
} else {
fmt.Println("Run bindgen -- create_line FAILED", err)
}
/// say: string -> string
res, e, err := bg.Execute("say", "bindgen funcs test")
if err == nil {
if e != nil {
fmt.Println("Error -- say:", e)
} else {
fmt.Println("Run bindgen -- say:", res[1].(string), res[0].(uint16))
}
} else {
fmt.Println("Run bindgen -- say FAILED")
}
/// obfusticate: string -> string
res, _, err = bg.Execute("obfusticate", "A quick brown fox jumps over the lazy dog")
if err == nil {
fmt.Println("Run bindgen -- obfusticate:", res[0].(string))
} else {
fmt.Println("Run bindgen -- obfusticate FAILED")
}
/// lowest_common_multiple: i32, i32 -> i32
res, _, err = bg.Execute("lowest_common_multiple", int32(123), int32(2))
if err == nil {
fmt.Println("Run bindgen -- lowest_common_multiple:", res[0].(int32))
} else {
fmt.Println("Run bindgen -- lowest_common_multiple FAILED")
}
/// sha3_digest: array -> array
res, _, err = bg.Execute("sha3_digest", []byte("This is an important message"))
if err == nil {
fmt.Println("Run bindgen -- sha3_digest:", res[0].([]byte))
} else {
fmt.Println("Run bindgen -- sha3_digest FAILED")
}
/// keccak_digest: array -> array
res, _, err = bg.Execute("keccak_digest", []byte("This is an important message"))
if err == nil {
fmt.Println("Run bindgen -- keccak_digest:", res[0].([]byte))
} else {
fmt.Println("Run bindgen -- keccak_digest FAILED")
}
bg.Release()
conf.Release()
}