| 52 | |
| 53 | |
| 54 | void run() { |
| 55 | // Initialize. |
| 56 | std::cout << "Initializing..." << std::endl; |
| 57 | auto engine = wasm::Engine::make(); |
| 58 | auto store_ = wasm::Store::make(engine.get()); |
| 59 | auto store = store_.get(); |
| 60 | |
| 61 | // Load binary. |
| 62 | std::cout << "Loading binary..." << std::endl; |
| 63 | std::ifstream file("global.wasm"); |
| 64 | file.seekg(0, std::ios_base::end); |
| 65 | auto file_size = file.tellg(); |
| 66 | file.seekg(0); |
| 67 | auto binary = wasm::vec<byte_t>::make_uninitialized(file_size); |
| 68 | file.read(binary.get(), file_size); |
| 69 | file.close(); |
| 70 | if (file.fail()) { |
| 71 | std::cout << "> Error loading module!" << std::endl; |
| 72 | exit(1); |
| 73 | } |
| 74 | |
| 75 | // Compile. |
| 76 | std::cout << "Compiling module..." << std::endl; |
| 77 | auto module = wasm::Module::make(store, binary); |
| 78 | if (!module) { |
| 79 | std::cout << "> Error compiling module!" << std::endl; |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | // Create external globals. |
| 84 | std::cout << "Creating globals..." << std::endl; |
| 85 | auto const_f32_type = wasm::GlobalType::make( |
| 86 | wasm::ValType::make(wasm::ValKind::F32), wasm::Mutability::CONST); |
| 87 | auto const_i64_type = wasm::GlobalType::make( |
| 88 | wasm::ValType::make(wasm::ValKind::I64), wasm::Mutability::CONST); |
| 89 | auto var_f32_type = wasm::GlobalType::make( |
| 90 | wasm::ValType::make(wasm::ValKind::F32), wasm::Mutability::VAR); |
| 91 | auto var_i64_type = wasm::GlobalType::make( |
| 92 | wasm::ValType::make(wasm::ValKind::I64), wasm::Mutability::VAR); |
| 93 | auto const_f32_import = wasm::Global::make(store, const_f32_type.get(), wasm::Val::f32(1)); |
| 94 | auto const_i64_import = wasm::Global::make(store, const_i64_type.get(), wasm::Val::i64(2)); |
| 95 | auto var_f32_import = wasm::Global::make(store, var_f32_type.get(), wasm::Val::f32(3)); |
| 96 | auto var_i64_import = wasm::Global::make(store, var_i64_type.get(), wasm::Val::i64(4)); |
| 97 | |
| 98 | // Instantiate. |
| 99 | std::cout << "Instantiating module..." << std::endl; |
| 100 | auto imports = wasm::vec<wasm::Extern*>::make( |
| 101 | const_f32_import.get(), const_i64_import.get(), |
| 102 | var_f32_import.get(), var_i64_import.get() |
| 103 | ); |
| 104 | auto instance = wasm::Instance::make(store, module.get(), imports); |
| 105 | if (!instance) { |
| 106 | std::cout << "> Error instantiating module!" << std::endl; |
| 107 | exit(1); |
| 108 | } |
| 109 | |
| 110 | // Extract export. |
| 111 | std::cout << "Extracting exports..." << std::endl; |