1*eaa01d7bSTill Schneidereit /*
2*eaa01d7bSTill Schneidereit Example of instantiating of the WebAssembly module and invoking its exported
3*eaa01d7bSTill Schneidereit function.
4*eaa01d7bSTill Schneidereit
5*eaa01d7bSTill Schneidereit You can build the example using CMake:
6*eaa01d7bSTill Schneidereit
7*eaa01d7bSTill Schneidereit mkdir build && (cd build && cmake .. && \
8*eaa01d7bSTill Schneidereit cmake --build . --target wasmtime-multi-cpp)
9*eaa01d7bSTill Schneidereit
10*eaa01d7bSTill Schneidereit And then run it:
11*eaa01d7bSTill Schneidereit
12*eaa01d7bSTill Schneidereit build/wasmtime-multi-cpp
13*eaa01d7bSTill Schneidereit */
14*eaa01d7bSTill Schneidereit
15*eaa01d7bSTill Schneidereit #include <fstream>
16*eaa01d7bSTill Schneidereit #include <iostream>
17*eaa01d7bSTill Schneidereit #include <sstream>
18*eaa01d7bSTill Schneidereit #include <wasmtime.hh>
19*eaa01d7bSTill Schneidereit
20*eaa01d7bSTill Schneidereit using namespace wasmtime;
21*eaa01d7bSTill Schneidereit
readFile(const char * name)22*eaa01d7bSTill Schneidereit std::string readFile(const char *name) {
23*eaa01d7bSTill Schneidereit std::ifstream watFile;
24*eaa01d7bSTill Schneidereit watFile.open(name);
25*eaa01d7bSTill Schneidereit std::stringstream strStream;
26*eaa01d7bSTill Schneidereit strStream << watFile.rdbuf();
27*eaa01d7bSTill Schneidereit return strStream.str();
28*eaa01d7bSTill Schneidereit }
29*eaa01d7bSTill Schneidereit
main()30*eaa01d7bSTill Schneidereit int main() {
31*eaa01d7bSTill Schneidereit std::cout << "Initializing...\n";
32*eaa01d7bSTill Schneidereit Engine engine;
33*eaa01d7bSTill Schneidereit Store store(engine);
34*eaa01d7bSTill Schneidereit
35*eaa01d7bSTill Schneidereit std::cout << "Compiling module...\n";
36*eaa01d7bSTill Schneidereit auto wat = readFile("examples/multi.wat");
37*eaa01d7bSTill Schneidereit Module module = Module::compile(engine, wat).unwrap();
38*eaa01d7bSTill Schneidereit
39*eaa01d7bSTill Schneidereit std::cout << "Creating callback...\n";
40*eaa01d7bSTill Schneidereit Func callback_func = Func::wrap(
41*eaa01d7bSTill Schneidereit store, [](int32_t a, int64_t b) -> std::tuple<int64_t, int32_t> {
42*eaa01d7bSTill Schneidereit // Rust example adds 1 to each argument but flips order.
43*eaa01d7bSTill Schneidereit return std::make_tuple(b + 1, a + 1);
44*eaa01d7bSTill Schneidereit });
45*eaa01d7bSTill Schneidereit
46*eaa01d7bSTill Schneidereit std::cout << "Instantiating module...\n";
47*eaa01d7bSTill Schneidereit Instance instance = Instance::create(store, module, {callback_func}).unwrap();
48*eaa01d7bSTill Schneidereit
49*eaa01d7bSTill Schneidereit std::cout << "Extracting export...\n";
50*eaa01d7bSTill Schneidereit Func g = std::get<Func>(*instance.get(store, "g"));
51*eaa01d7bSTill Schneidereit
52*eaa01d7bSTill Schneidereit std::cout << "Calling export \"g\"...\n";
53*eaa01d7bSTill Schneidereit // Provide (i32=1, i64=3) like the Rust example
54*eaa01d7bSTill Schneidereit auto results = g.call(store, {Val(int32_t(1)), Val(int64_t(3))}).unwrap();
55*eaa01d7bSTill Schneidereit
56*eaa01d7bSTill Schneidereit std::cout << "Printing result...\n";
57*eaa01d7bSTill Schneidereit std::cout << "> " << results[0].i64() << " " << results[1].i32() << "\n";
58*eaa01d7bSTill Schneidereit
59*eaa01d7bSTill Schneidereit std::cout << "Calling export \"round_trip_many\"...\n";
60*eaa01d7bSTill Schneidereit Func round_trip_many =
61*eaa01d7bSTill Schneidereit std::get<Func>(*instance.get(store, "round_trip_many"));
62*eaa01d7bSTill Schneidereit auto many_results =
63*eaa01d7bSTill Schneidereit round_trip_many
64*eaa01d7bSTill Schneidereit .call(store, {Val(int64_t(0)), Val(int64_t(1)), Val(int64_t(2)),
65*eaa01d7bSTill Schneidereit Val(int64_t(3)), Val(int64_t(4)), Val(int64_t(5)),
66*eaa01d7bSTill Schneidereit Val(int64_t(6)), Val(int64_t(7)), Val(int64_t(8)),
67*eaa01d7bSTill Schneidereit Val(int64_t(9))})
68*eaa01d7bSTill Schneidereit .unwrap();
69*eaa01d7bSTill Schneidereit std::cout << "Printing result...\n";
70*eaa01d7bSTill Schneidereit std::cout << "> (";
71*eaa01d7bSTill Schneidereit for (size_t i = 0; i < many_results.size(); i++) {
72*eaa01d7bSTill Schneidereit if (i)
73*eaa01d7bSTill Schneidereit std::cout << ", ";
74*eaa01d7bSTill Schneidereit std::cout << many_results[i].i64();
75*eaa01d7bSTill Schneidereit }
76*eaa01d7bSTill Schneidereit std::cout << ")\n";
77*eaa01d7bSTill Schneidereit return 0;
78*eaa01d7bSTill Schneidereit }
79