1*eaa01d7bSTill Schneidereit /*
2*eaa01d7bSTill Schneidereit An example of how to interact with multiple memories.
3*eaa01d7bSTill Schneidereit 
4*eaa01d7bSTill Schneidereit You can build the example using CMake:
5*eaa01d7bSTill Schneidereit 
6*eaa01d7bSTill Schneidereit mkdir build && (cd build && cmake .. && \
7*eaa01d7bSTill Schneidereit   cmake --build . --target wasmtime-multimemory-cpp)
8*eaa01d7bSTill Schneidereit 
9*eaa01d7bSTill Schneidereit And then run it:
10*eaa01d7bSTill Schneidereit 
11*eaa01d7bSTill Schneidereit build/wasmtime-multimemory-cpp
12*eaa01d7bSTill Schneidereit */
13*eaa01d7bSTill Schneidereit 
14*eaa01d7bSTill Schneidereit #include <fstream>
15*eaa01d7bSTill Schneidereit #include <iostream>
16*eaa01d7bSTill Schneidereit #include <sstream>
17*eaa01d7bSTill Schneidereit #include <wasmtime.hh>
18*eaa01d7bSTill Schneidereit 
19*eaa01d7bSTill Schneidereit using namespace wasmtime;
20*eaa01d7bSTill Schneidereit 
readFile(const char * name)21*eaa01d7bSTill Schneidereit std::string readFile(const char *name) {
22*eaa01d7bSTill Schneidereit   std::ifstream watFile;
23*eaa01d7bSTill Schneidereit   watFile.open(name);
24*eaa01d7bSTill Schneidereit   std::stringstream strStream;
25*eaa01d7bSTill Schneidereit   strStream << watFile.rdbuf();
26*eaa01d7bSTill Schneidereit   return strStream.str();
27*eaa01d7bSTill Schneidereit }
28*eaa01d7bSTill Schneidereit 
main()29*eaa01d7bSTill Schneidereit int main() {
30*eaa01d7bSTill Schneidereit   std::cout << "Initializing...\n";
31*eaa01d7bSTill Schneidereit   Config config;
32*eaa01d7bSTill Schneidereit   config.wasm_multi_memory(true);
33*eaa01d7bSTill Schneidereit   Engine engine(std::move(config));
34*eaa01d7bSTill Schneidereit   Store store(engine);
35*eaa01d7bSTill Schneidereit 
36*eaa01d7bSTill Schneidereit   std::cout << "Compiling module...\n";
37*eaa01d7bSTill Schneidereit   auto wat = readFile("examples/multimemory.wat");
38*eaa01d7bSTill Schneidereit   Module module = Module::compile(engine, wat).unwrap();
39*eaa01d7bSTill Schneidereit 
40*eaa01d7bSTill Schneidereit   std::cout << "Instantiating module...\n";
41*eaa01d7bSTill Schneidereit   Instance instance = Instance::create(store, module, {}).unwrap();
42*eaa01d7bSTill Schneidereit   Memory memory0 = std::get<Memory>(*instance.get(store, "memory0"));
43*eaa01d7bSTill Schneidereit   Memory memory1 = std::get<Memory>(*instance.get(store, "memory1"));
44*eaa01d7bSTill Schneidereit 
45*eaa01d7bSTill Schneidereit   std::cout << "Checking memory...\n";
46*eaa01d7bSTill Schneidereit   // (Details intentionally omitted to mirror Rust example concise output.)
47*eaa01d7bSTill Schneidereit 
48*eaa01d7bSTill Schneidereit   std::cout << "Mutating memory...\n";
49*eaa01d7bSTill Schneidereit   auto d0 = memory0.data(store);
50*eaa01d7bSTill Schneidereit   if (d0.size() >= 0x1004)
51*eaa01d7bSTill Schneidereit     d0[0x1003] = 5;
52*eaa01d7bSTill Schneidereit   auto d1 = memory1.data(store);
53*eaa01d7bSTill Schneidereit   if (d1.size() >= 0x1004)
54*eaa01d7bSTill Schneidereit     d1[0x1003] = 7;
55*eaa01d7bSTill Schneidereit 
56*eaa01d7bSTill Schneidereit   std::cout << "Growing memory...\n";
57*eaa01d7bSTill Schneidereit   memory0.grow(store, 1).unwrap();
58*eaa01d7bSTill Schneidereit   memory1.grow(store, 2).unwrap();
59*eaa01d7bSTill Schneidereit 
60*eaa01d7bSTill Schneidereit   return 0;
61*eaa01d7bSTill Schneidereit }
62