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 ole32.lib bcrypt.lib` 61 * 62 * ## Building from Source 63 * 64 * The C API is located in the 65 * [`crates/c-api`](https://github.com/bytecodealliance/wasmtime/tree/main/crates/c-api) 66 * directory of the [Wasmtime 67 * repository](https://github.com/bytecodealliance/wasmtime). To build from 68 * source you'll need a Rust compiler and a checkout of the `wasmtime` project. 69 * Afterwards you can execute: 70 * 71 * ``` 72 * $ cargo build --release -p wasmtime-c-api 73 * ``` 74 * 75 * This will place the final artifacts in `target/release`, with names depending 76 * on what platform you're compiling for. 77 * 78 * ## Other resources 79 * 80 * Some other handy resources you might find useful when exploring the C API 81 * documentation are: 82 * 83 * * [Rust `wasmtime` crate 84 * documentation](https://bytecodealliance.github.io/wasmtime/api/wasmtime/) - 85 * although this documentation is for Rust and not C, you'll find that many 86 * functions mirror one another and there may be extra documentation in Rust 87 * you find helpful. If you find yourself having to frequently do this, 88 * though, please feel free to [file an 89 * issue](https://github.com/bytecodealliance/wasmtime/issues/new). 90 * 91 * * [C embedding 92 * examples](https://bytecodealliance.github.io/wasmtime/examples-c-embed.html) 93 * are available online and are tested from the Wasmtime repository itself. 94 * 95 * * [Contribution documentation for 96 * Wasmtime](https://bytecodealliance.github.io/wasmtime/contributing.html) in 97 * case you're interested in helping out! 98 */ 99 100 /** 101 * \file wasmtime.h 102 * 103 * \brief Wasmtime's C API 104 * 105 * This file is the central inclusion point for Wasmtime's C API. There are a 106 * number of sub-header files but this file includes them all. The C API is 107 * based on \ref wasm.h but there are many Wasmtime-specific APIs which are 108 * tailored to Wasmtime's implementation. 109 * 110 * The #wasm_config_t and #wasm_engine_t types are used from \ref wasm.h. 111 * Additionally all type-level information (like #wasm_functype_t) is also 112 * used from \ref wasm.h. Otherwise, though, all wasm objects (like 113 * #wasmtime_store_t or #wasmtime_func_t) are used from this header file. 114 * 115 * ### Thread Safety 116 * 117 * The multithreading story of the C API very closely follows the 118 * multithreading story of the Rust API for Wasmtime. All objects are safe to 119 * send to other threads so long as user-specific data is also safe to send to 120 * other threads. Functions are safe to call from any thread but some functions 121 * cannot be called concurrently. For example, functions which correspond to 122 * `&T` in Rust can be called concurrently with any other methods that take 123 * `&T`. Functions that take `&mut T` in Rust, however, cannot be called 124 * concurrently with any other function (but can still be invoked on any 125 * thread). 126 * 127 * This generally equates to mutation of internal state. Functions which don't 128 * mutate anything, such as learning type information through 129 * #wasmtime_func_type, can be called concurrently. Functions which do require 130 * mutation, for example #wasmtime_func_call, cannot be called concurrently. 131 * This is conveyed in the C API with either `const wasmtime_context_t*` 132 * (concurrency is ok as it's read-only) or `wasmtime_context_t*` (concurrency 133 * is not ok, mutation may happen). 134 * 135 * When in doubt assume that functions cannot be called concurrently with 136 * aliasing objects. 137 * 138 * ### Aliasing 139 * 140 * The C API for Wasmtime is intended to be a relatively thin layer over the 141 * Rust API for Wasmtime. Rust has much more strict rules about aliasing than C 142 * does, and the Rust API for Wasmtime is designed around these rules to be 143 * used safely. These same rules must be upheld when using the C API of 144 * Wasmtime. 145 * 146 * The main consequence of this is that the #wasmtime_context_t pointer into 147 * the #wasmtime_store_t must be carefully used. Since the context is an 148 * internal pointer into the store it must be used carefully to ensure you're 149 * not doing something that Rust would otherwise forbid at compile time. A 150 * #wasmtime_context_t can only be used when you would otherwise have been 151 * provided access to it. For example in a host function created with 152 * #wasmtime_func_new you can use #wasmtime_context_t in the host function 153 * callback. This is because an argument, a #wasmtime_caller_t, provides access 154 * to #wasmtime_context_t. On the other hand a destructor passed to 155 * #wasmtime_externref_new, however, cannot use a #wasmtime_context_t because 156 * it was not provided access to one. Doing so may lead to memory unsafety. 157 * 158 * ### Stores 159 * 160 * A foundational construct in this API is the #wasmtime_store_t. A store is a 161 * collection of host-provided objects and instantiated wasm modules. Stores are 162 * often treated as a "single unit" and items within a store are all allowed to 163 * reference one another. References across stores cannot currently be created. 164 * For example you cannot pass a function from one store into another store. 165 * 166 * A store is not intended to be a global long-lived object. Stores provide no 167 * means of internal garbage collections of wasm objects (such as instances), 168 * meaning that no memory from a store will be deallocated until you call 169 * #wasmtime_store_delete. If you're working with a web server, for example, 170 * then it's recommended to think of a store as a "one per request" sort of 171 * construct. Globally you'd have one #wasm_engine_t and a cache of 172 * #wasmtime_module_t instances compiled into that engine. Each request would 173 * create a new #wasmtime_store_t and then instantiate a #wasmtime_module_t 174 * into the store. This process of creating a store and instantiating a module 175 * is expected to be quite fast. When the request is finished you'd delete the 176 * #wasmtime_store_t keeping memory usage reasonable for the lifetime of the 177 * server. 178 */ 179 180 #ifndef WASMTIME_API_H 181 #define WASMTIME_API_H 182 183 #include <wasi.h> 184 #include <wasmtime/config.h> 185 #include <wasmtime/engine.h> 186 #include <wasmtime/error.h> 187 #include <wasmtime/extern.h> 188 #include <wasmtime/func.h> 189 #include <wasmtime/global.h> 190 #include <wasmtime/instance.h> 191 #include <wasmtime/linker.h> 192 #include <wasmtime/memory.h> 193 #include <wasmtime/module.h> 194 #include <wasmtime/store.h> 195 #include <wasmtime/table.h> 196 #include <wasmtime/trap.h> 197 #include <wasmtime/val.h> 198 199 #ifdef __cplusplus 200 extern "C" { 201 #endif 202 203 /** 204 * \brief Converts from the text format of WebAssembly to to the binary format. 205 * 206 * \param wat this it the input pointer with the WebAssembly Text Format inside of 207 * it. This will be parsed and converted to the binary format. 208 * \param wat_len this it the length of `wat`, in bytes. 209 * \param ret if the conversion is successful, this byte vector is filled in with 210 * the WebAssembly binary format. 211 * 212 * \return a non-null error if parsing fails, or returns `NULL`. If parsing 213 * fails then `ret` isn't touched. 214 * 215 * This function does not take ownership of `wat`, and the caller is expected to 216 * deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t. 217 */ 218 WASM_API_EXTERN wasmtime_error_t* wasmtime_wat2wasm( 219 const char *wat, 220 size_t wat_len, 221 wasm_byte_vec_t *ret 222 ); 223 224 #ifdef __cplusplus 225 } // extern "C" 226 #endif 227 228 #endif // WASMTIME_API_H 229