1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <inttypes.h> 5 #include <wasm.h> 6 #include <wasmtime.h> 7 8 #define own 9 10 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap); 11 12 int main(int argc, const char* argv[]) { 13 // Configuring engine to support generating of DWARF info. 14 // lldb can be used to attach to the program and observe 15 // original fib-wasm.c source code and variables. 16 wasm_config_t* config = wasm_config_new(); 17 wasmtime_config_debug_info_set(config, true); 18 19 // Initialize. 20 printf("Initializing...\n"); 21 wasm_engine_t* engine = wasm_engine_new_with_config(config); 22 wasmtime_store_t* store = wasmtime_store_new(engine, NULL, NULL); 23 wasmtime_context_t* context = wasmtime_store_context(store); 24 25 // Load binary. 26 printf("Loading binary...\n"); 27 FILE* file = fopen("target/wasm32-unknown-unknown/debug/fib.wasm", "rb"); 28 if (!file) { 29 printf("> Error opening module!\n"); 30 return 1; 31 } 32 fseek(file, 0L, SEEK_END); 33 size_t file_size = ftell(file); 34 fseek(file, 0L, SEEK_SET); 35 wasm_byte_vec_t binary; 36 wasm_byte_vec_new_uninitialized(&binary, file_size); 37 if (fread(binary.data, file_size, 1, file) != 1) { 38 printf("> Error reading module!\n"); 39 return 1; 40 } 41 fclose(file); 42 43 // Compile. 44 printf("Compiling module...\n"); 45 wasmtime_module_t *module = NULL; 46 wasmtime_error_t* error = wasmtime_module_new(engine, (uint8_t*) binary.data, binary.size, &module); 47 if (!module) 48 exit_with_error("failed to compile module", error, NULL); 49 wasm_byte_vec_delete(&binary); 50 51 // Instantiate. 52 printf("Instantiating module...\n"); 53 wasmtime_instance_t instance; 54 wasm_trap_t *trap = NULL; 55 error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap); 56 if (error != NULL || trap != NULL) 57 exit_with_error("failed to instantiate", error, trap); 58 wasmtime_module_delete(module); 59 60 // Extract export. 61 wasmtime_extern_t fib; 62 bool ok = wasmtime_instance_export_get(context, &instance, "fib", 3, &fib); 63 assert(ok); 64 65 // Call. 66 printf("Calling fib...\n"); 67 wasmtime_val_t params[1]; 68 params[0].kind = WASMTIME_I32; 69 params[0].of.i32 = 6; 70 wasmtime_val_t results[1]; 71 error = wasmtime_func_call(context, &fib.of.func, params, 1, results, 1, &trap); 72 if (error != NULL || trap != NULL) 73 exit_with_error("failed to call function", error, trap); 74 75 assert(results[0].kind == WASMTIME_I32); 76 printf("> fib(6) = %d\n", results[0].of.i32); 77 78 // Shut down. 79 printf("Shutting down...\n"); 80 wasmtime_store_delete(store); 81 wasm_engine_delete(engine); 82 83 // All done. 84 printf("Done.\n"); 85 return 0; 86 } 87 88 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) { 89 fprintf(stderr, "error: %s\n", message); 90 wasm_byte_vec_t error_message; 91 if (error != NULL) { 92 wasmtime_error_message(error, &error_message); 93 } else { 94 wasm_trap_message(trap, &error_message); 95 } 96 fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data); 97 wasm_byte_vec_delete(&error_message); 98 exit(1); 99 } 100