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