1 /**
2  * \mainpage Wasmtime C API
3  *
4  * This documentation is an overview and API reference for the C API of
5  * Wasmtime. The C API is spread between three different header files:
6  *
7  * * \ref wasmtime.h
8  * * \ref wasi.h
9  * * \ref wasm.h
10  *
11  * The \ref wasmtime.h header file includes all the other header files and is
12  * the main header file you'll likely be using. The \ref wasm.h header file
13  * comes directly from the
14  * [WebAssembly/wasm-c-api](https://github.com/WebAssembly/wasm-c-api)
15  * repository, and at this time the upstream header file does not have
16  * documentation so Wasmtime provides documentation here. It should be noted
17  * some semantics may be Wasmtime-specific and may not be portable to other
18  * engines.
19  *
20  * ## Installing the C API
21  *
22  * To install the C API from precompiled binaries you can download the
23  * appropriate binary from the [releases page of
24  * Wasmtime](https://github.com/bytecodealliance/wasmtime/releases). Artifacts
25  * for the C API all end in "-c-api" for the filename.
26  *
27  * Each archive contains an `include` directory with necessary headers, as well
28  * as a `lib` directory with both a static archive and a dynamic library of
29  * Wasmtime. You can link to either of them as you see fit.
30  *
31  * ## Installing the C API through CMake
32  *
33  * CMake can be used to make the process of linking and compiling easier. An
34  * example of this if you have wasmtime as a git submodule at
35  * `third_party/wasmtime`:
36  * ```
37  * add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/wasmtime/crates/c-api
38  * ${CMAKE_CURRENT_BINARY_DIR}/wasmtime)
39  * ...
40  * target_include_directories(YourProject PUBLIC wasmtime)
41  * target_link_libraries(YourProject PUBLIC wasmtime)
42  * ```
43  * `BUILD_SHARED_LIBS` is provided as a define if you would like to build a
44  * shared library instead. You must distribute the appropriate shared library
45  * for your platform if you do this.
46  *
47  * ## Linking against the C API
48  *
49  * You'll want to arrange the `include` directory of the C API to be in your
50  * compiler's header path (e.g. the `-I` flag). If you're compiling for Windows
51  * and you're using the static library then you'll also need to pass
52  * `-DWASM_API_EXTERN=` and `-DWASI_API_EXTERN=` to disable dllimport.
53  *
54  * Your final artifact can then be linked with `-lwasmtime`. If you're linking
55  * against the static library you may need to pass other system libraries
56  * depending on your platform:
57  *
58  * * Linux - `-lpthread -ldl -lm`
59  * * macOS - no extra flags needed
60  * * Windows - `ws2_32.lib advapi32.lib userenv.lib ntdll.lib shell32.lib
61  * ole32.lib bcrypt.lib`
62  *
63  * ## Building from Source
64  *
65  * The C API is located in the
66  * [`crates/c-api`](https://github.com/bytecodealliance/wasmtime/tree/main/crates/c-api)
67  * directory of the [Wasmtime
68  * repository](https://github.com/bytecodealliance/wasmtime). To build from
69  * source you'll need a Rust compiler and a checkout of the `wasmtime` project.
70  * Afterwards you can execute:
71  *
72  * ```
73  * $ cargo build --release -p wasmtime-c-api
74  * ```
75  *
76  * This will place the final artifacts in `target/release`, with names depending
77  * on what platform you're compiling for.
78  *
79  * ## Other resources
80  *
81  * Some other handy resources you might find useful when exploring the C API
82  * documentation are:
83  *
84  * * [Rust `wasmtime` crate
85  *   documentation](https://bytecodealliance.github.io/wasmtime/api/wasmtime/) -
86  *   although this documentation is for Rust and not C, you'll find that many
87  *   functions mirror one another and there may be extra documentation in Rust
88  *   you find helpful. If you find yourself having to frequently do this,
89  *   though, please feel free to [file an
90  *   issue](https://github.com/bytecodealliance/wasmtime/issues/new).
91  *
92  * * [C embedding
93  *   examples](https://bytecodealliance.github.io/wasmtime/lang-c.html)
94  *   are available online and are tested from the Wasmtime repository itself.
95  *
96  * * [Contribution documentation for
97  *   Wasmtime](https://bytecodealliance.github.io/wasmtime/contributing.html) in
98  *   case you're interested in helping out!
99  */
100 
101 /**
102  * \file wasmtime.h
103  *
104  * \brief Wasmtime's C API
105  *
106  * This file is the central inclusion point for Wasmtime's C API. There are a
107  * number of sub-header files but this file includes them all. The C API is
108  * based on \ref wasm.h but there are many Wasmtime-specific APIs which are
109  * tailored to Wasmtime's implementation.
110  *
111  * The #wasm_config_t and #wasm_engine_t types are used from \ref wasm.h.
112  * Additionally all type-level information (like #wasm_functype_t) is also
113  * used from \ref wasm.h. Otherwise, though, all wasm objects (like
114  * #wasmtime_store_t or #wasmtime_func_t) are used from this header file.
115  *
116  * ### Thread Safety
117  *
118  * The multithreading story of the C API very closely follows the
119  * multithreading story of the Rust API for Wasmtime. All objects are safe to
120  * send to other threads so long as user-specific data is also safe to send to
121  * other threads. Functions are safe to call from any thread but some functions
122  * cannot be called concurrently. For example, functions which correspond to
123  * `&T` in Rust can be called concurrently with any other methods that take
124  * `&T`. Functions that take `&mut T` in Rust, however, cannot be called
125  * concurrently with any other function (but can still be invoked on any
126  * thread).
127  *
128  * This generally equates to mutation of internal state. Functions which don't
129  * mutate anything, such as learning type information through
130  * #wasmtime_func_type, can be called concurrently. Functions which do require
131  * mutation, for example #wasmtime_func_call, cannot be called concurrently.
132  * This is conveyed in the C API with either `const wasmtime_context_t*`
133  * (concurrency is ok as it's read-only) or `wasmtime_context_t*` (concurrency
134  * is not ok, mutation may happen).
135  *
136  * When in doubt assume that functions cannot be called concurrently with
137  * aliasing objects.
138  *
139  * ### Aliasing
140  *
141  * The C API for Wasmtime is intended to be a relatively thin layer over the
142  * Rust API for Wasmtime. Rust has much more strict rules about aliasing than C
143  * does, and the Rust API for Wasmtime is designed around these rules to be
144  * used safely. These same rules must be upheld when using the C API of
145  * Wasmtime.
146  *
147  * The main consequence of this is that the #wasmtime_context_t pointer into
148  * the #wasmtime_store_t must be carefully used. Since the context is an
149  * internal pointer into the store it must be used carefully to ensure you're
150  * not doing something that Rust would otherwise forbid at compile time. A
151  * #wasmtime_context_t can only be used when you would otherwise have been
152  * provided access to it. For example in a host function created with
153  * #wasmtime_func_new you can use #wasmtime_context_t in the host function
154  * callback. This is because an argument, a #wasmtime_caller_t, provides access
155  * to #wasmtime_context_t.
156  *
157  * ### Stores
158  *
159  * A foundational construct in this API is the #wasmtime_store_t. A store is a
160  * collection of host-provided objects and instantiated wasm modules. Stores are
161  * often treated as a "single unit" and items within a store are all allowed to
162  * reference one another. References across stores cannot currently be created.
163  * For example you cannot pass a function from one store into another store.
164  *
165  * A store is not intended to be a global long-lived object. Stores provide no
166  * means of internal garbage collections of wasm objects (such as instances),
167  * meaning that no memory from a store will be deallocated until you call
168  * #wasmtime_store_delete. If you're working with a web server, for example,
169  * then it's recommended to think of a store as a "one per request" sort of
170  * construct. Globally you'd have one #wasm_engine_t and a cache of
171  * #wasmtime_module_t instances compiled into that engine. Each request would
172  * create a new #wasmtime_store_t and then instantiate a #wasmtime_module_t
173  * into the store. This process of creating a store and instantiating a module
174  * is expected to be quite fast. When the request is finished you'd delete the
175  * #wasmtime_store_t keeping memory usage reasonable for the lifetime of the
176  * server.
177  */
178 
179 #ifndef WASMTIME_API_H
180 #define WASMTIME_API_H
181 
182 #include <wasi.h>
183 #include <wasmtime/conf.h>
184 // clang-format off
185 // IWYU pragma: begin_exports
186 #include <wasmtime/config.h>
187 #include <wasmtime/engine.h>
188 #include <wasmtime/error.h>
189 #include <wasmtime/extern.h>
190 #include <wasmtime/func.h>
191 #include <wasmtime/global.h>
192 #include <wasmtime/instance.h>
193 #include <wasmtime/linker.h>
194 #include <wasmtime/memory.h>
195 #include <wasmtime/module.h>
196 #include <wasmtime/profiling.h>
197 #include <wasmtime/sharedmemory.h>
198 #include <wasmtime/store.h>
199 #include <wasmtime/table.h>
200 #include <wasmtime/trap.h>
201 #include <wasmtime/val.h>
202 #include <wasmtime/async.h>
203 // IWYU pragma: end_exports
204 // clang-format on
205 
206 /**
207  * \brief Wasmtime version string.
208  */
209 #define WASMTIME_VERSION "26.0.0"
210 /**
211  * \brief Wasmtime major version number.
212  */
213 #define WASMTIME_VERSION_MAJOR 26
214 /**
215  * \brief Wasmtime minor version number.
216  */
217 #define WASMTIME_VERSION_MINOR 0
218 /**
219  * \brief Wasmtime patch version number.
220  */
221 #define WASMTIME_VERSION_PATCH 0
222 
223 #ifdef __cplusplus
224 extern "C" {
225 #endif
226 
227 #ifdef WASMTIME_FEATURE_WAT
228 
229 /**
230  * \brief Converts from the text format of WebAssembly to the binary format.
231  *
232  * \param wat this it the input pointer with the WebAssembly Text Format inside
233  *        of it. This will be parsed and converted to the binary format.
234  * \param wat_len this it the length of `wat`, in bytes.
235  * \param ret if the conversion is successful, this byte vector is filled in
236  *        with the WebAssembly binary format.
237  *
238  * \return a non-null error if parsing fails, or returns `NULL`. If parsing
239  * fails then `ret` isn't touched.
240  *
241  * This function does not take ownership of `wat`, and the caller is expected to
242  * deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t.
243  */
244 WASM_API_EXTERN wasmtime_error_t *
245 wasmtime_wat2wasm(const char *wat, size_t wat_len, wasm_byte_vec_t *ret);
246 
247 #endif
248 
249 #ifdef __cplusplus
250 } // extern "C"
251 #endif
252 
253 #endif // WASMTIME_API_H
254