4
4
// This example illustrates how to use exported globals. They come
5
5
// in 2 flavors:
6
6
//
7
- // 1. Immutable globals (const),
8
- // 2. Mutable globals.
7
+ // 1. Immutable globals (const),
8
+ // 2. Mutable globals.
9
9
//
10
10
// You can run the example directly by executing in Wasmer root:
11
11
//
@@ -18,6 +18,7 @@ package wasmer
18
18
19
19
import (
20
20
"fmt"
21
+
21
22
"github.com/wasmerio/wasmer-go/wasmer"
22
23
)
23
24
@@ -44,7 +45,6 @@ func ExampleGlobal_Set() {
44
45
45
46
fmt .Println ("Compiling module..." )
46
47
module , err := wasmer .NewModule (store , wasmBytes )
47
-
48
48
if err != nil {
49
49
fmt .Println ("Failed to compile module:" , err )
50
50
}
@@ -55,7 +55,6 @@ func ExampleGlobal_Set() {
55
55
fmt .Println ("Instantiating module..." )
56
56
// Let's instantiate the Wasm module.
57
57
instance , err := wasmer .NewInstance (module , importObject )
58
-
59
58
if err != nil {
60
59
panic (fmt .Sprintln ("Failed to instantiate the module:" , err ))
61
60
}
@@ -64,13 +63,11 @@ func ExampleGlobal_Set() {
64
63
//
65
64
// The Wasm module exports some globals. Let's get them.
66
65
one , err := instance .Exports .GetGlobal ("one" )
67
-
68
66
if err != nil {
69
67
panic (fmt .Sprintln ("Failed to retrieve the `one` global:" , err ))
70
68
}
71
69
72
70
some , err := instance .Exports .GetGlobal ("some" )
73
-
74
71
if err != nil {
75
72
panic (fmt .Sprintln ("Failed to retrieve the `some` global:" , err ))
76
73
}
@@ -91,19 +88,16 @@ func ExampleGlobal_Set() {
91
88
// We will use an exported function for the `one` global
92
89
// and the Global API for `some`.
93
90
getOne , err := instance .Exports .GetFunction ("get_one" )
94
-
95
91
if err != nil {
96
92
panic (fmt .Sprintln ("Failed to retrieve the `get_one` function:" , err ))
97
93
}
98
94
99
95
oneValue , err := getOne ()
100
-
101
96
if err != nil {
102
97
panic (fmt .Sprintln ("Failed to call the `get_one` function:" , err ))
103
98
}
104
99
105
100
someValue , err := some .Get ()
106
-
107
101
if err != nil {
108
102
panic (fmt .Sprintln ("Failed to get the `some` global value:" , err ))
109
103
}
@@ -121,7 +115,6 @@ func ExampleGlobal_Set() {
121
115
}
122
116
123
117
oneValue , err = getOne ()
124
-
125
118
if err != nil {
126
119
panic (fmt .Sprintln ("Failed to call the `get_one` function:" , err ))
127
120
}
@@ -134,33 +127,28 @@ func ExampleGlobal_Set() {
134
127
//
135
128
// We will use both for the `some` global.
136
129
setSome , err := instance .Exports .GetFunction ("set_some" )
137
-
138
130
if err != nil {
139
131
panic (fmt .Sprintln ("Failed to retrieve the `set_some` function:" , err ))
140
132
}
141
133
142
134
_ , err = setSome (float32 (21.0 ))
143
-
144
135
if err != nil {
145
136
panic (fmt .Sprintln ("Failed to call the `set_some` function:" , err ))
146
137
}
147
138
148
139
someValue , err = some .Get ()
149
-
150
140
if err != nil {
151
141
panic (fmt .Sprintln ("Failed to get the `some` global value:" , err ))
152
142
}
153
143
154
144
fmt .Printf ("`some` value after `set_some`: %.1f\n " , someValue )
155
145
156
146
err = some .Set (float32 (42.0 ), wasmer .F32 )
157
-
158
147
if err != nil {
159
148
panic (fmt .Sprintln ("Failed to set the `some` global value:" , err ))
160
149
}
161
150
162
151
someValue , err = some .Get ()
163
-
164
152
if err != nil {
165
153
panic (fmt .Sprintln ("Failed to get the `some` global value:" , err ))
166
154
}
0 commit comments