| 116 | } |
| 117 | |
| 118 | int main() { |
| 119 | // Initialize. |
| 120 | printf("Initializing...\n"); |
| 121 | |
| 122 | wasm_config_t *config = wasm_config_new(); |
| 123 | assert(config != NULL); |
| 124 | wasmtime_config_wasm_multi_memory_set(config, true); |
| 125 | |
| 126 | wasm_engine_t *engine = wasm_engine_new_with_config(config); |
| 127 | assert(engine != NULL); |
| 128 | |
| 129 | wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); |
| 130 | wasmtime_context_t *context = wasmtime_store_context(store); |
| 131 | |
| 132 | // Load our input file to parse it next |
| 133 | FILE *file = fopen("examples/multimemory.wat", "r"); |
| 134 | if (!file) { |
| 135 | printf("> Error loading file!\n"); |
| 136 | return 1; |
| 137 | } |
| 138 | fseek(file, 0L, SEEK_END); |
| 139 | size_t file_size = ftell(file); |
| 140 | fseek(file, 0L, SEEK_SET); |
| 141 | wasm_byte_vec_t wat; |
| 142 | wasm_byte_vec_new_uninitialized(&wat, file_size); |
| 143 | if (fread(wat.data, file_size, 1, file) != 1) { |
| 144 | printf("> Error loading module!\n"); |
| 145 | return 1; |
| 146 | } |
| 147 | fclose(file); |
| 148 | |
| 149 | // Parse the wat into the binary wasm format |
| 150 | wasm_byte_vec_t binary; |
| 151 | wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &binary); |
| 152 | if (error != NULL) |
| 153 | exit_with_error("failed to parse wat", error, NULL); |
| 154 | wasm_byte_vec_delete(&wat); |
| 155 | |
| 156 | // Compile. |
| 157 | printf("Compiling module...\n"); |
| 158 | wasmtime_module_t *module = NULL; |
| 159 | error = |
| 160 | wasmtime_module_new(engine, (uint8_t *)binary.data, binary.size, &module); |
| 161 | if (error) |
| 162 | exit_with_error("failed to compile module", error, NULL); |
| 163 | wasm_byte_vec_delete(&binary); |
| 164 | |
| 165 | // Instantiate. |
| 166 | printf("Instantiating module...\n"); |
| 167 | wasmtime_instance_t instance; |
| 168 | wasm_trap_t *trap = NULL; |
| 169 | error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap); |
| 170 | if (error != NULL || trap != NULL) |
| 171 | exit_with_error("failed to instantiate", error, trap); |
| 172 | wasmtime_module_delete(module); |
| 173 | |
| 174 | // Extract export. |
| 175 | printf("Extracting exports...\n"); |
nothing calls this directly
no test coverage detected