xref: /wasmtime-44.0.1/examples/hello.c (revision 331b0dee)
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/hello.c \
9        -I crates/c-api/include \
10        -I crates/c-api/wasm-c-api/include \
11        target/release/libwasmtime.a \
12        -lpthread -ldl -lm \
13        -o hello
14    ./hello
15 
16 Note that on Windows and macOS the command will be similar, but you'll need
17 to tweak the `-lpthread` and such annotations as well as the name of the
18 `libwasmtime.a` file on Windows.
19 */
20 
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <wasm.h>
25 #include <wasmtime.h>
26 
27 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
28 
29 static wasm_trap_t* hello_callback(
30     void *env,
31     wasmtime_caller_t *caller,
32     const wasmtime_val_t *args,
33     size_t nargs,
34     wasmtime_val_t *results,
35     size_t nresults
36 ) {
37   printf("Calling back...\n");
38   printf("> Hello World!\n");
39   return NULL;
40 }
41 
42 int main() {
43   int ret = 0;
44   // Set up our compilation context. Note that we could also work with a
45   // `wasm_config_t` here to configure what feature are enabled and various
46   // compilation settings.
47   printf("Initializing...\n");
48   wasm_engine_t *engine = wasm_engine_new();
49   assert(engine != NULL);
50 
51   // With an engine we can create a *store* which is a long-lived group of wasm
52   // modules. Note that we allocate some custom data here to live in the store,
53   // but here we skip that and specify NULL.
54   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
55   assert(store != NULL);
56   wasmtime_context_t *context = wasmtime_store_context(store);
57 
58   // Read our input file, which in this case is a wasm text file.
59   FILE* file = fopen("examples/hello.wat", "r");
60   assert(file != NULL);
61   fseek(file, 0L, SEEK_END);
62   size_t file_size = ftell(file);
63   fseek(file, 0L, SEEK_SET);
64   wasm_byte_vec_t wat;
65   wasm_byte_vec_new_uninitialized(&wat, file_size);
66   assert(fread(wat.data, file_size, 1, file) == 1);
67   fclose(file);
68 
69   // Parse the wat into the binary wasm format
70   wasm_byte_vec_t wasm;
71   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
72   if (error != NULL)
73     exit_with_error("failed to parse wat", error, NULL);
74   wasm_byte_vec_delete(&wat);
75 
76   // Now that we've got our binary webassembly we can compile our module.
77   printf("Compiling module...\n");
78   wasmtime_module_t *module = NULL;
79   error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module);
80   wasm_byte_vec_delete(&wasm);
81   if (error != NULL)
82     exit_with_error("failed to compile module", error, NULL);
83 
84   // Next up we need to create the function that the wasm module imports. Here
85   // we'll be hooking up a thunk function to the `hello_callback` native
86   // function above. Note that we can assign custom data, but we just use NULL
87   // for now).
88   printf("Creating callback...\n");
89   wasm_functype_t *hello_ty = wasm_functype_new_0_0();
90   wasmtime_func_t hello;
91   wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
92 
93   // With our callback function we can now instantiate the compiled module,
94   // giving us an instance we can then execute exports from. Note that
95   // instantiation can trap due to execution of the `start` function, so we need
96   // to handle that here too.
97   printf("Instantiating module...\n");
98   wasm_trap_t *trap = NULL;
99   wasmtime_instance_t instance;
100   wasmtime_extern_t import;
101   import.kind = WASMTIME_EXTERN_FUNC;
102   import.of.func = hello;
103   error = wasmtime_instance_new(context, module, &import, 1, &instance, &trap);
104   if (error != NULL || trap != NULL)
105     exit_with_error("failed to instantiate", error, trap);
106 
107   // Lookup our `run` export function
108   printf("Extracting export...\n");
109   wasmtime_extern_t run;
110   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
111   assert(ok);
112   assert(run.kind == WASMTIME_EXTERN_FUNC);
113 
114   // And call it!
115   printf("Calling export...\n");
116   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
117   if (error != NULL || trap != NULL)
118     exit_with_error("failed to call function", error, trap);
119 
120   // Clean up after ourselves at this point
121   printf("All finished!\n");
122   ret = 0;
123 
124   wasmtime_module_delete(module);
125   wasmtime_store_delete(store);
126   wasm_engine_delete(engine);
127   return ret;
128 }
129 
130 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
131   fprintf(stderr, "error: %s\n", message);
132   wasm_byte_vec_t error_message;
133   if (error != NULL) {
134     wasmtime_error_message(error, &error_message);
135     wasmtime_error_delete(error);
136   } else {
137     wasm_trap_message(trap, &error_message);
138     wasm_trap_delete(trap);
139   }
140   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
141   wasm_byte_vec_delete(&error_message);
142   exit(1);
143 }
144