MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime-go / ExampleConfig_multi

Function ExampleConfig_multi

example_test.go:155–238  ·  view source on GitHub ↗

An example of enabling the multi-value feature of WebAssembly and interacting with multi-value functions.

()

Source from the content-addressed store, hash-verified

153// An example of enabling the multi-value feature of WebAssembly and
154// interacting with multi-value functions.
155func ExampleConfig_multi() {
156 // Configure our `Store`, but be sure to use a `Config` that enables the
157 // wasm multi-value feature since it's not stable yet.
158 config := wasmtime.NewConfig()
159 config.SetWasmMultiValue(true)
160 store := wasmtime.NewStore(wasmtime.NewEngineWithConfig(config))
161
162 wasm, err := wasmtime.Wat2Wasm(`
163 (module
164 (func $f (import "" "f") (param i32 i64) (result i64 i32))
165
166 (func $g (export "g") (param i32 i64) (result i64 i32)
167 (call $f (local.get 0) (local.get 1))
168 )
169
170 (func $round_trip_many
171 (export "round_trip_many")
172 (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
173 (result i64 i64 i64 i64 i64 i64 i64 i64 i64 i64)
174
175 local.get 0
176 local.get 1
177 local.get 2
178 local.get 3
179 local.get 4
180 local.get 5
181 local.get 6
182 local.get 7
183 local.get 8
184 local.get 9
185 )
186 )
187 `)
188 if err != nil {
189 log.Fatal(err)
190 }
191 module, err := wasmtime.NewModule(store.Engine, wasm)
192 if err != nil {
193 log.Fatal(err)
194 }
195
196 callback := wasmtime.WrapFunc(store, func(a int32, b int64) (int64, int32) {
197 return b + 1, a + 1
198 })
199
200 instance, err := wasmtime.NewInstance(store, module, []wasmtime.AsExtern{callback})
201 if err != nil {
202 log.Fatal(err)
203 }
204
205 g := instance.GetFunc(store, "g")
206
207 results, err := g.Call(store, 1, 3)
208 if err != nil {
209 log.Fatal(err)
210 }
211 arr := results.([]wasmtime.Val)
212 a := arr[0].I64()

Callers

nothing calls this directly

Calls 6

SetWasmMultiValueMethod · 0.95
GetFuncMethod · 0.95
CallMethod · 0.80
I64Method · 0.80
I32Method · 0.80
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…