xref: /wasmtime-44.0.1/examples/hello.c (revision 9ce3ffe1)
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 You can also build using cmake:
21 
22 mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-hello
23 */
24 
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <wasm.h>
29 #include <wasmtime.h>
30 
31 static void exit_with_error(const char *message, wasmtime_error_t *error,
32                             wasm_trap_t *trap);
33 
34 static wasm_trap_t *hello_callback(void *env, wasmtime_caller_t *caller,
35                                    const wasmtime_val_t *args, size_t nargs,
36                                    wasmtime_val_t *results, size_t nresults) {
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   if (fread(wat.data, file_size, 1, file) != 1) {
67     printf("> Error loading module!\n");
68     return 1;
69   }
70   fclose(file);
71 
72   // Parse the wat into the binary wasm format
73   wasm_byte_vec_t wasm;
74   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
75   if (error != NULL)
76     exit_with_error("failed to parse wat", error, NULL);
77   wasm_byte_vec_delete(&wat);
78 
79   // Now that we've got our binary webassembly we can compile our module.
80   printf("Compiling module...\n");
81   wasmtime_module_t *module = NULL;
82   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
83   wasm_byte_vec_delete(&wasm);
84   if (error != NULL)
85     exit_with_error("failed to compile module", error, NULL);
86 
87   // Next up we need to create the function that the wasm module imports. Here
88   // we'll be hooking up a thunk function to the `hello_callback` native
89   // function above. Note that we can assign custom data, but we just use NULL
90   // for now).
91   printf("Creating callback...\n");
92   wasm_functype_t *hello_ty = wasm_functype_new_0_0();
93   wasmtime_func_t hello;
94   wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
95 
96   // With our callback function we can now instantiate the compiled module,
97   // giving us an instance we can then execute exports from. Note that
98   // instantiation can trap due to execution of the `start` function, so we need
99   // to handle that here too.
100   printf("Instantiating module...\n");
101   wasm_trap_t *trap = NULL;
102   wasmtime_instance_t instance;
103   wasmtime_extern_t import;
104   import.kind = WASMTIME_EXTERN_FUNC;
105   import.of.func = hello;
106   error = wasmtime_instance_new(context, module, &import, 1, &instance, &trap);
107   if (error != NULL || trap != NULL)
108     exit_with_error("failed to instantiate", error, trap);
109 
110   // Lookup our `run` export function
111   printf("Extracting export...\n");
112   wasmtime_extern_t run;
113   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
114   assert(ok);
115   assert(run.kind == WASMTIME_EXTERN_FUNC);
116 
117   // And call it!
118   printf("Calling export...\n");
119   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
120   if (error != NULL || trap != NULL)
121     exit_with_error("failed to call function", error, trap);
122 
123   // Clean up after ourselves at this point
124   printf("All finished!\n");
125   ret = 0;
126 
127   wasmtime_module_delete(module);
128   wasmtime_store_delete(store);
129   wasm_engine_delete(engine);
130   return ret;
131 }
132 
133 static void exit_with_error(const char *message, wasmtime_error_t *error,
134                             wasm_trap_t *trap) {
135   fprintf(stderr, "error: %s\n", message);
136   wasm_byte_vec_t error_message;
137   if (error != NULL) {
138     wasmtime_error_message(error, &error_message);
139     wasmtime_error_delete(error);
140   } else {
141     wasm_trap_message(trap, &error_message);
142     wasm_trap_delete(trap);
143   }
144   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
145   wasm_byte_vec_delete(&error_message);
146   exit(1);
147 }
148