wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Does wasmer-go support importing memory into the wasm module #346

Closed radkomih closed 2 years ago

radkomih commented 2 years ago

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)
radkomih commented 2 years ago

Found what I was looking for.

james-lawrence commented 2 years ago

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

radkomih commented 2 years ago

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