MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/multi.cc:27–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25}
26
27int main() {
28 std::cout << "Initializing...\n";
29 Engine engine;
30 Store store(engine);
31
32 std::cout << "Compiling module...\n";
33 auto wat = readFile("examples/multi.wat");
34 Module module = Module::compile(engine, wat).unwrap();
35
36 std::cout << "Creating callback...\n";
37 Func callback_func = Func::wrap(
38 store, [](int32_t a, int64_t b) -> std::tuple<int64_t, int32_t> {
39 // Rust example adds 1 to each argument but flips order.
40 return std::make_tuple(b + 1, a + 1);
41 });
42
43 std::cout << "Instantiating module...\n";
44 Instance instance = Instance::create(store, module, {callback_func}).unwrap();
45
46 std::cout << "Extracting export...\n";
47 Func g = std::get<Func>(*instance.get(store, "g"));
48
49 std::cout << "Calling export \"g\"...\n";
50 // Provide (i32=1, i64=3) like the Rust example
51 auto results = g.call(store, {Val(int32_t(1)), Val(int64_t(3))}).unwrap();
52
53 std::cout << "Printing result...\n";
54 std::cout << "> " << results[0].i64() << " " << results[1].i32() << "\n";
55
56 std::cout << "Calling export \"round_trip_many\"...\n";
57 Func round_trip_many =
58 std::get<Func>(*instance.get(store, "round_trip_many"));
59 auto many_results =
60 round_trip_many
61 .call(store, {Val(int64_t(0)), Val(int64_t(1)), Val(int64_t(2)),
62 Val(int64_t(3)), Val(int64_t(4)), Val(int64_t(5)),
63 Val(int64_t(6)), Val(int64_t(7)), Val(int64_t(8)),
64 Val(int64_t(9))})
65 .unwrap();
66 std::cout << "Printing result...\n";
67 std::cout << "> (";
68 for (size_t i = 0; i < many_results.size(); i++) {
69 if (i)
70 std::cout << ", ";
71 std::cout << many_results[i].i64();
72 }
73 std::cout << ")\n";
74 return 0;
75}

Callers

nothing calls this directly

Calls 10

readFileFunction · 0.70
compileFunction · 0.50
createFunction · 0.50
ValEnum · 0.50
unwrapMethod · 0.45
getMethod · 0.45
callMethod · 0.45
i64Method · 0.45
i32Method · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected