wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go

Home Page:https://pkg.go.dev/github.com/wasmerio/wasmer-go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does wasmer-go support importing memory into the wasm module

radkomih opened this issue · comments

Summary

I don't see any examples in the docs if wasmer-go supports importing memory from the host environment into the Wasm module? Is it possible and how?

Additional details

For example: The Wasm module (the one below) should be able to use the memory provided by the host and use the host provided functions to manage it.

Type: wasm
Size: 2.5 MB
Imports:
  Functions:
    "env"."ext_allocator_free": [I32] -> []
    "env"."ext_allocator_malloc": [I32] -> [I32]
  Memories:
    "env"."memory": not shared (20 pages..)
  Tables:
  Globals:
Exports:
  Functions:
    "intr_cust_function": [I32, I32] -> [I64]
  Memories:
  Tables:
    "__indirect_function_table": FuncRef (352..352)
  Globals:
    "__data_end": I32 (constant)
    "__heap_base": I32 (constant)

Found what I was looking for.

@radkomih it'd be useful if you posted the solution =P

Sure, here is the fragment with the memory import:

...
importObject := wasmer.NewDefaultWasiImportObject()

memory, _ := wasmer.NewMemory(2, 0)

imports := wasmer.NewImports()
imports.Namespace("env").AppendMemory("memory", memory)
importObject.Extend(*imports)

instance, _ := module.InstantiateWithImportObject(importObject)
...

@james-lawrence