Working with Multiple Memories

You can also browse this source code online and clone the wasmtime repository to run the example locally.

This example demonstrates using a module with multiple linear memories.

multimemory.wat

(module
  (memory (export "memory0") 2 3)
  (memory (export "memory1") 2 4)

  (func (export "size0") (result i32) (memory.size 0))
  (func (export "load0") (param i32) (result i32)
    local.get 0
    i32.load8_s 0
  )
  (func (export "store0") (param i32 i32)
    local.get 0
    local.get 1
    i32.store8 0
  )
  (func (export "size1") (result i32) (memory.size 1))
  (func (export "load1") (param i32) (result i32)
    local.get 0
    i32.load8_s 1
  )
  (func (export "store1") (param i32 i32)
    local.get 0
    local.get 1
    i32.store8 1
  )

  (data (memory 0) (i32.const 0x1000) "\01\02\03\04")
  (data (memory 1) (i32.const 0x1000) "\04\03\02\01")
)

multimemory.cc

/*
An example of how to interact with multiple memories.

You can build the example using CMake:

mkdir build && (cd build && cmake .. && \
  cmake --build . --target wasmtime-multimemory-cpp)

And then run it:

build/wasmtime-multimemory-cpp
*/

#include <fstream>
#include <iostream>
#include <sstream>
#include <wasmtime.hh>

using namespace wasmtime;

std::string readFile(const char *name) {
  std::ifstream watFile;
  watFile.open(name);
  std::stringstream strStream;
  strStream << watFile.rdbuf();
  return strStream.str();
}

int main() {
  std::cout << "Initializing...\n";
  Config config;
  config.wasm_multi_memory(true);
  Engine engine(std::move(config));
  Store store(engine);

  std::cout << "Compiling module...\n";
  auto wat = readFile("examples/multimemory.wat");
  Module module = Module::compile(engine, wat).unwrap();

  std::cout << "Instantiating module...\n";
  Instance instance = Instance::create(store, module, {}).unwrap();
  Memory memory0 = std::get<Memory>(*instance.get(store, "memory0"));
  Memory memory1 = std::get<Memory>(*instance.get(store, "memory1"));

  std::cout << "Checking memory...\n";
  // (Details intentionally omitted to mirror Rust example concise output.)

  std::cout << "Mutating memory...\n";
  auto d0 = memory0.data(store);
  if (d0.size() >= 0x1004)
    d0[0x1003] = 5;
  auto d1 = memory1.data(store);
  if (d1.size() >= 0x1004)
    d1[0x1003] = 7;

  std::cout << "Growing memory...\n";
  memory0.grow(store, 1).unwrap();
  memory1.grow(store, 2).unwrap();

  return 0;
}