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

Function ExampleLinker

example_test.go:240–309  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

238}
239
240func ExampleLinker() {
241 store := wasmtime.NewStore(wasmtime.NewEngine())
242
243 // Compile two wasm modules where the first references the second
244 wasm1, err := wasmtime.Wat2Wasm(`
245 (module
246 (import "wasm2" "double" (func $double (param i32) (result i32)))
247 (func (export "double_and_add") (param i32 i32) (result i32)
248 local.get 0
249 call $double
250 local.get 1
251 i32.add
252 )
253 )
254 `)
255 if err != nil {
256 log.Fatal(err)
257 }
258
259 wasm2, err := wasmtime.Wat2Wasm(`
260 (module
261 (func (export "double") (param i32) (result i32)
262 local.get 0
263 i32.const 2
264 i32.mul
265 )
266 )
267 `)
268 if err != nil {
269 log.Fatal(err)
270 }
271
272 // Next compile both modules
273 module1, err := wasmtime.NewModule(store.Engine, wasm1)
274 if err != nil {
275 log.Fatal(err)
276 }
277 module2, err := wasmtime.NewModule(store.Engine, wasm2)
278 if err != nil {
279 log.Fatal(err)
280 }
281
282 linker := wasmtime.NewLinker(store.Engine)
283
284 // The second module is instantiated first since it has no imports, and
285 // then we insert the instance back into the linker under the name
286 // the first module expects.
287 instance2, err := linker.Instantiate(store, module2)
288 if err != nil {
289 log.Fatal(err)
290 }
291 err = linker.DefineInstance(store, "wasm2", instance2)
292 if err != nil {
293 log.Fatal(err)
294 }
295
296 // And now we can instantiate our first module, executing the result
297 // afterwards

Callers

nothing calls this directly

Calls 4

InstantiateMethod · 0.95
DefineInstanceMethod · 0.95
GetFuncMethod · 0.80
CallMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…