-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathexample_exports_function_test.go
103 lines (87 loc) · 2.67 KB
/
example_exports_function_test.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
95
96
97
98
99
100
101
102
103
// A Wasm module can export entities, like functions, memories,
// globals and tables.
//
// This example illustrates how to use exported functions. They come
// in 2 flavors:
//
// 1. Dynamic functions,
// 2. Native function.
//
// You can run the example directly by executing in Wasmer root:
//
// ```shell
// go test examples/example_exports_function_test.go
// ```
//
// Ready?
package wasmer
import (
"fmt"
"github.com/wasmerio/wasmer-go/wasmer"
)
func ExampleFunction() {
// Let's declare the Wasm module.
//
// We are using the text representation of the module here.
wasmBytes := []byte(`
(module
(type $sum_t (func (param i32 i32) (result i32)))
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add)
(export "sum" (func $sum_f)))
`)
// Create an Engine
engine := wasmer.NewEngine()
// Create a Store
store := wasmer.NewStore(engine)
fmt.Println("Compiling module...")
module, err := wasmer.NewModule(store, wasmBytes)
if err != nil {
fmt.Println("Failed to compile module:", err)
}
// Create an empty import object.
importObject := wasmer.NewImportObject()
fmt.Println("Instantiating module...")
// Let's instantiate the Wasm module.
instance, err := wasmer.NewInstance(module, importObject)
if err != nil {
panic(fmt.Sprintln("Failed to instantiate the module:", err))
}
// Here we go.
//
// The Wasm module exports a function called `sum`. Let's get
// it.
sum, err := instance.Exports.GetRawFunction("sum")
if err != nil {
panic(fmt.Sprintln("Failed to retrieve the `sum` function:", err))
}
fmt.Println("Calling `sum` function...")
// Let's call the `sum` exported function.
result, err := sum.Call(1, 2)
if err != nil {
panic(fmt.Sprintln("Failed to call the `sum` function:", err))
}
fmt.Println("Result of the `sum` function:", result)
// That was fun. But what if we can get rid of the `Call` call? Well,
// that's possible with the native flavor. The function will seem like
// it's a standard Go function.
sumNative := sum.Native()
fmt.Println("Calling `sum` function (natively)...")
// Let's call the `sum` exported function. The parameters are
// statically typed Rust values of type `i32` and `i32`. The
// result, in this case particular case, in a unit of type `i32`.
result, err = sumNative(3, 4)
if err != nil {
panic(fmt.Sprintln("Failed to call the `sum` function natively:", err))
}
fmt.Println("Result of the `sum` function:", result)
// Output:
// Compiling module...
// Instantiating module...
// Calling `sum` function...
// Result of the `sum` function: 3
// Calling `sum` function (natively)...
// Result of the `sum` function: 7
}