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

Function run

example/reflect.cc:76–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

74
75
76void run() {
77 // Initialize.
78 std::cout << "Initializing..." << std::endl;
79 auto engine = wasm::Engine::make();
80 auto store_ = wasm::Store::make(engine.get());
81 auto store = store_.get();
82
83 // Load binary.
84 std::cout << "Loading binary..." << std::endl;
85 std::ifstream file("reflect.wasm");
86 file.seekg(0, std::ios_base::end);
87 auto file_size = file.tellg();
88 file.seekg(0);
89 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
90 file.read(binary.get(), file_size);
91 file.close();
92 if (file.fail()) {
93 std::cout << "> Error loading module!" << std::endl;
94 exit(1);
95 }
96
97 // Compile.
98 std::cout << "Compiling module..." << std::endl;
99 auto module = wasm::Module::make(store, binary);
100 if (!module) {
101 std::cout << "> Error compiling module!" << std::endl;
102 exit(1);
103 }
104
105 // Instantiate.
106 std::cout << "Instantiating module..." << std::endl;
107 auto imports = wasm::vec<wasm::Extern*>::make();
108 auto instance = wasm::Instance::make(store, module.get(), imports);
109 if (!instance) {
110 std::cout << "> Error instantiating module!" << std::endl;
111 exit(1);
112 }
113
114 // Extract exports.
115 std::cout << "Extracting export..." << std::endl;
116 auto export_types = module->exports();
117 auto exports = instance->exports();
118 assert(exports.size() == export_types.size());
119
120 for (size_t i = 0; i < exports.size(); ++i) {
121 assert(exports[i]->kind() == export_types[i]->type()->kind());
122 std::cout << "> export " << i << " " << export_types[i]->name() << std::endl;
123 std::cout << ">> initial: " << *export_types[i]->type() << std::endl;
124 std::cout << ">> current: " << *exports[i]->type() << std::endl;
125 if (exports[i]->kind() == wasm::ExternKind::FUNC) {
126 auto func = exports[i]->func();
127 std::cout << ">> in-arity: " << func->param_arity();
128 std::cout << ", out-arity: " << func->result_arity() << std::endl;
129 }
130 }
131
132 // Shut down.
133 std::cout << "Shutting down..." << std::endl;

Callers 1

mainFunction · 0.70

Calls 9

exportsMethod · 0.80
typeMethod · 0.80
nameMethod · 0.80
funcMethod · 0.80
param_arityMethod · 0.80
result_arityMethod · 0.80
getMethod · 0.45
sizeMethod · 0.45
kindMethod · 0.45

Tested by

no test coverage detected