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