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