-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathexample_early_exit_test.go
112 lines (94 loc) · 2.59 KB
/
example_early_exit_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
104
105
106
107
108
109
110
111
112
// A Wasm module can sometimes not work, it will produce errors in some
// cases.
//
// In this example we'll see how to handle such errors in the most
// basic way. To do that we'll use a Wasm module that we know will
// produce an error.
//
// You can run the example directly by executing in Wasmer root:
//
// ```shell
// go test examples/example_early_exit_test.go
// ```
//
// Ready?
package wasmer
import (
"fmt"
"runtime"
"github.com/wasmerio/wasmer-go/wasmer"
)
type exitCode struct {
code int32
}
func (self *exitCode) Error() string {
return fmt.Sprintf("exit code: %d", self.code)
}
func earlyExit(args []wasmer.Value) ([]wasmer.Value, error) {
return nil, &exitCode{1}
}
func ExampleFunction_Call() {
// Let's declare the Wasm module.
//
// We are using the text representation of the module here.
wasmBytes := []byte(`
(module
(type $run_t (func (param i32 i32) (result i32)))
(type $early_exit_t (func (param) (result)))
(import "env" "early_exit" (func $early_exit (type $early_exit_t)))
(func $run (type $run_t) (param $x i32) (param $y i32) (result i32)
(call $early_exit)
(i32.add
local.get $x
local.get $y))
(export "run" (func $run)))
`)
// 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 import object with the expected function.
importObject := wasmer.NewImportObject()
importObject.Register(
"env",
map[string]wasmer.IntoExtern{
"early_exit": wasmer.NewFunction(
store,
wasmer.NewFunctionType(wasmer.NewValueTypes(), wasmer.NewValueTypes()),
earlyExit,
),
},
)
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.
//
// Get the `run` function which we'll use as our entrypoint.
fmt.Println("Calling `run` function...")
run, err := instance.Exports.GetFunction("run")
if err != nil {
panic(fmt.Sprintln("Failed to retrieve the `run` function:", err))
}
_, err = run(1, 7)
if err == nil {
panic(fmt.Sprintln("`run` did not error"))
}
fmt.Println("Exited early with:", err)
// These lines are here to ensure that SetFinalizer works correctly
runtime.GC()
runtime.GC()
// Output:
// Compiling module...
// Instantiating module...
// Calling `run` function...
// Exited early with: exit code: 1
}