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

Function run

example/hostref.cc:115–228  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

113}
114
115void run() {
116 // Initialize.
117 std::cout << "Initializing..." << std::endl;
118 auto engine = wasm::Engine::make();
119 auto store_ = wasm::Store::make(engine.get());
120 auto store = store_.get();
121
122 // Load binary.
123 std::cout << "Loading binary..." << std::endl;
124 std::ifstream file("hostref.wasm");
125 file.seekg(0, std::ios_base::end);
126 auto file_size = file.tellg();
127 file.seekg(0);
128 auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
129 file.read(binary.get(), file_size);
130 file.close();
131 if (file.fail()) {
132 std::cout << "> Error loading module!" << std::endl;
133 return;
134 }
135
136 // Compile.
137 std::cout << "Compiling module..." << std::endl;
138 auto module = wasm::Module::make(store, binary);
139 if (!module) {
140 std::cout << "> Error compiling module!" << std::endl;
141 return;
142 }
143
144 // Create external callback function.
145 std::cout << "Creating callback..." << std::endl;
146 auto callback_type = wasm::FuncType::make(
147 wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::EXTERNREF)),
148 wasm::ownvec<wasm::ValType>::make(wasm::ValType::make(wasm::ValKind::EXTERNREF))
149 );
150 auto callback_func = wasm::Func::make(store, callback_type.get(), callback);
151
152 // Instantiate.
153 std::cout << "Instantiating module..." << std::endl;
154 auto imports = wasm::vec<wasm::Extern*>::make(callback_func.get());
155 auto instance = wasm::Instance::make(store, module.get(), imports);
156 if (!instance) {
157 std::cout << "> Error instantiating module!" << std::endl;
158 return;
159 }
160
161 // Extract export.
162 std::cout << "Extracting exports..." << std::endl;
163 auto exports = instance->exports();
164 size_t i = 0;
165 auto global = get_export_global(exports, i++);
166 auto table = get_export_table(exports, i++);
167 auto global_set = get_export_func(exports, i++);
168 auto global_get = get_export_func(exports, i++);
169 auto table_set = get_export_func(exports, i++);
170 auto table_get = get_export_func(exports, i++);
171 auto func_call = get_export_func(exports, i++);
172

Callers 1

mainFunction · 0.70

Calls 15

ValClass · 0.85
exportsMethod · 0.80
refMethod · 0.80
release_refMethod · 0.80
setMethod · 0.80
get_export_globalFunction · 0.70
get_export_tableFunction · 0.70
get_export_funcFunction · 0.70
checkFunction · 0.70
call_v_rFunction · 0.70
call_r_vFunction · 0.70
call_i_rFunction · 0.70

Tested by

no test coverage detected