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