1 /* 2 Example of instantiating of the WebAssembly module and invoking its exported 3 function. 4 5 You can build using cmake: 6 7 mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-multi 8 9 Also note that this example was taken from 10 https://github.com/WebAssembly/wasm-c-api/blob/master/example/multi.c 11 originally 12 */ 13 14 #include <inttypes.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <wasm.h> 19 #include <wasmtime.h> 20 21 static void exit_with_error(const char *message, wasmtime_error_t *error, 22 wasm_trap_t *trap); 23 24 // A function to be called from Wasm code. 25 wasm_trap_t *callback(void *env, wasmtime_caller_t *caller, 26 const wasmtime_val_t *args, size_t nargs, 27 wasmtime_val_t *results, size_t nresults) { 28 (void)env; 29 (void)caller; 30 (void)nargs; 31 (void)nresults; 32 33 printf("Calling back...\n"); 34 printf("> %" PRIu32 " %" PRIu64 "\n", args[0].of.i32, args[1].of.i64); 35 printf("\n"); 36 37 results[0] = args[1]; 38 results[1] = args[0]; 39 return NULL; 40 } 41 42 // A function closure. 43 wasm_trap_t *closure_callback(void *env, wasmtime_caller_t *caller, 44 const wasmtime_val_t *args, size_t nargs, 45 wasmtime_val_t *results, size_t nresults) { 46 (void)caller; 47 (void)args; 48 (void)nargs; 49 (void)nresults; 50 51 int i = *(int *)env; 52 printf("Calling back closure...\n"); 53 printf("> %d\n", i); 54 55 results[0].kind = WASMTIME_I32; 56 results[0].of.i32 = (int32_t)i; 57 return NULL; 58 } 59 60 int main() { 61 // Initialize. 62 printf("Initializing...\n"); 63 wasm_engine_t *engine = wasm_engine_new(); 64 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL); 65 wasmtime_context_t *context = wasmtime_store_context(store); 66 67 // Load our input file to parse it next 68 FILE *file = fopen("examples/multi.wat", "r"); 69 if (!file) { 70 printf("> Error loading file!\n"); 71 return 1; 72 } 73 fseek(file, 0L, SEEK_END); 74 size_t file_size = ftell(file); 75 fseek(file, 0L, SEEK_SET); 76 wasm_byte_vec_t wat; 77 wasm_byte_vec_new_uninitialized(&wat, file_size); 78 if (fread(wat.data, file_size, 1, file) != 1) { 79 printf("> Error loading module!\n"); 80 return 1; 81 } 82 fclose(file); 83 84 // Parse the wat into the binary wasm format 85 wasm_byte_vec_t binary; 86 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &binary); 87 if (error != NULL) 88 exit_with_error("failed to parse wat", error, NULL); 89 wasm_byte_vec_delete(&wat); 90 91 // Compile. 92 printf("Compiling module...\n"); 93 wasmtime_module_t *module = NULL; 94 error = 95 wasmtime_module_new(engine, (uint8_t *)binary.data, binary.size, &module); 96 if (error) 97 exit_with_error("failed to compile module", error, NULL); 98 wasm_byte_vec_delete(&binary); 99 100 // Create external print functions. 101 printf("Creating callback...\n"); 102 wasm_functype_t *callback_type = 103 wasm_functype_new_2_2(wasm_valtype_new_i32(), wasm_valtype_new_i64(), 104 wasm_valtype_new_i64(), wasm_valtype_new_i32()); 105 wasmtime_func_t callback_func; 106 wasmtime_func_new(context, callback_type, callback, NULL, NULL, 107 &callback_func); 108 wasm_functype_delete(callback_type); 109 110 // Instantiate. 111 printf("Instantiating module...\n"); 112 wasmtime_extern_t imports[1]; 113 imports[0].kind = WASMTIME_EXTERN_FUNC; 114 imports[0].of.func = callback_func; 115 wasmtime_instance_t instance; 116 wasm_trap_t *trap = NULL; 117 error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap); 118 if (error != NULL || trap != NULL) 119 exit_with_error("failed to instantiate", error, trap); 120 wasmtime_module_delete(module); 121 122 // Extract export. 123 printf("Extracting export...\n"); 124 wasmtime_extern_t run; 125 bool ok = wasmtime_instance_export_get(context, &instance, "g", 1, &run); 126 assert(ok); 127 assert(run.kind == WASMTIME_EXTERN_FUNC); 128 129 // Call. 130 printf("Calling export...\n"); 131 wasmtime_val_t args[2]; 132 args[0].kind = WASMTIME_I32; 133 args[0].of.i32 = 1; 134 args[1].kind = WASMTIME_I64; 135 args[1].of.i64 = 2; 136 wasmtime_val_t results[2]; 137 error = wasmtime_func_call(context, &run.of.func, args, 2, results, 2, &trap); 138 if (error != NULL || trap != NULL) 139 exit_with_error("failed to call run", error, trap); 140 141 // Print result. 142 printf("Printing result...\n"); 143 printf("> %" PRIu64 " %" PRIu32 "\n", results[0].of.i64, results[1].of.i32); 144 145 assert(results[0].kind == WASMTIME_I64); 146 assert(results[0].of.i64 == 2); 147 assert(results[1].kind == WASMTIME_I32); 148 assert(results[1].of.i32 == 1); 149 150 // Shut down. 151 printf("Shutting down...\n"); 152 wasmtime_store_delete(store); 153 wasm_engine_delete(engine); 154 155 // All done. 156 printf("Done.\n"); 157 return 0; 158 } 159 160 static void exit_with_error(const char *message, wasmtime_error_t *error, 161 wasm_trap_t *trap) { 162 fprintf(stderr, "error: %s\n", message); 163 wasm_byte_vec_t error_message; 164 if (error != NULL) { 165 wasmtime_error_message(error, &error_message); 166 wasmtime_error_delete(error); 167 } else { 168 wasm_trap_message(trap, &error_message); 169 wasm_trap_delete(trap); 170 } 171 fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data); 172 wasm_byte_vec_delete(&error_message); 173 exit(1); 174 } 175