MCPcopy Create free account
hub / github.com/WebAssembly/wasm-c-api / run

Function run

example/multi.cc:26–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24
25
26void run() {
27 // Initialize.
28 std::cout << "Initializing..." << std::endl;
29 auto engine = wasm::Engine::make();
30 auto store_ = wasm::Store::make(engine.get());
31 auto store = store_.get();
32
33 // Load binary.
34 std::cout << "Loading binary..." << std::endl;
35 std::ifstream file("multi.wasm");
36 file.seekg(0, std::ios_base::end);
37 auto file_size = file.tellg();
38 file.seekg(0);
39 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
40 file.read(binary.get(), file_size);
41 file.close();
42 if (file.fail()) {
43 std::cout << "> Error loading module!" << std::endl;
44 exit(1);
45 }
46
47 // Compile.
48 std::cout << "Compiling module..." << std::endl;
49 auto module = wasm::Module::make(store, binary);
50 if (!module) {
51 std::cout << "> Error compiling module!" << std::endl;
52 exit(1);
53 }
54
55 // Create external print functions.
56 std::cout << "Creating callback..." << std::endl;
57 auto tuple = wasm::ownvec<wasm::ValType>::make(
58 wasm::ValType::make(wasm::ValKind::I32),
59 wasm::ValType::make(wasm::ValKind::I64),
60 wasm::ValType::make(wasm::ValKind::I64),
61 wasm::ValType::make(wasm::ValKind::I32)
62 );
63 auto callback_type =
64 wasm::FuncType::make(tuple.deep_copy(), tuple.deep_copy());
65 auto callback_func = wasm::Func::make(store, callback_type.get(), callback);
66
67 // Instantiate.
68 std::cout << "Instantiating module..." << std::endl;
69 auto imports = wasm::vec<wasm::Extern*>::make(callback_func.get());
70 auto instance = wasm::Instance::make(store, module.get(), imports);
71 if (!instance) {
72 std::cout << "> Error instantiating module!" << std::endl;
73 exit(1);
74 }
75
76 // Extract export.
77 std::cout << "Extracting export..." << std::endl;
78 auto exports = instance->exports();
79 if (exports.size() == 0 || exports[0]->kind() != wasm::ExternKind::FUNC || !exports[0]->func()) {
80 std::cout << "> Error accessing export!" << std::endl;
81 exit(1);
82 }
83 auto run_func = exports[0]->func();

Callers 1

mainFunction · 0.70

Calls 10

deep_copyMethod · 0.80
exportsMethod · 0.80
funcMethod · 0.80
callMethod · 0.80
messageMethod · 0.80
i32Method · 0.80
i64Method · 0.80
getMethod · 0.45
sizeMethod · 0.45
kindMethod · 0.45

Tested by

no test coverage detected