| 113 | } |
| 114 | |
| 115 | void 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 |
no test coverage detected