wasmer/wasm_c_api/mod.rs
1//! Implementation of the [official WebAssembly C
2//! API](https://github.com/WebAssembly/wasm-c-api) for Wasmer.
3//!
4//! We would like to remind the reader that this official standard can
5//! be characterized as a _living standard_. As such, the API is not
6//! yet stable, even though it shows maturity over time. The API is
7//! described by the `wasm.h` C header, which is included by
8//! `wasmer.h` C header file (which contains extension of the
9//! standard API, for example to provide WASI or vendor-specific
10//! features).
11//!
12//! # Quick Guide
13//!
14//! Usually, the user first needs to create an [`engine`] and a
15//! [`store`]. Once it's done, the user needs to create a [`module`]
16//! and then [instantiate][instance] it. When instantiating the
17//! module, the user is able to pass a set of
18//! [imports][externals]. With an instance, the user is able to call
19//! the [exports][instance::wasm_instance_exports].
20//!
21//! Every module comes with examples and entry points to guide the
22//! discovery of this API.
23
24/// `Context`.
25mod function_env;
26
27/// Private Rust macros.
28#[macro_use]
29mod macros;
30
31/// An engine drives the compilation and the runtime.
32///
33/// Entry points: A default engine is created with
34/// [`wasm_engine_new`][engine::wasm_engine_new] and freed with
35/// [`wasm_engine_delete`][engine::wasm_engine_delete].
36///
37/// # Example
38///
39/// The simplest way to get a default engine is the following:
40///
41/// ```rust
42/// # use wasmer_inline_c::assert_c;
43/// # fn main() {
44/// # (assert_c! {
45/// # #include "tests/wasmer.h"
46/// #
47/// int main() {
48/// // Create the engine.
49/// wasm_engine_t* engine = wasm_engine_new();
50///
51/// // Check we have a valid engine!
52/// assert(engine);
53///
54/// // Free everything.
55/// wasm_engine_delete(engine);
56///
57/// return 0;
58/// }
59/// # })
60/// # .success();
61/// # }
62/// ```
63///
64/// To configure the engine, see the [`wasm_config_new`][engine::wasm_config_new].
65pub mod engine;
66
67/// cbindgen:ignore
68pub mod externals;
69
70/// A WebAssembly instance is a stateful, executable instance of a
71/// WebAssembly module.
72///
73/// Instance objects contain all the exported WebAssembly functions,
74/// memories, tables and globals that allow interacting with
75/// WebAssembly.
76///
77/// Entry points: A WebAssembly instance is created with
78/// [`wasm_instance_new`][instance::wasm_instance_new] and freed with
79/// [`wasm_instance_delete`][instance::wasm_instance_delete].
80///
81/// # Example
82///
83/// The simplest way to instantiate a Wasm module is the following:
84///
85/// ```rust
86/// # use wasmer_inline_c::assert_c;
87/// # fn main() {
88/// # (assert_c! {
89/// # #include "tests/wasmer.h"
90/// #
91/// int main() {
92/// // Create the engine and the store.
93/// wasm_engine_t* engine = wasm_engine_new();
94/// wasm_store_t* store = wasm_store_new(engine);
95///
96/// // Create a WebAssembly module from a WAT definition.
97/// wasm_byte_vec_t wat;
98/// wasmer_byte_vec_new_from_string(&wat, "(module)");
99/// wasm_byte_vec_t wasm;
100/// wat2wasm(&wat, &wasm);
101///
102/// // Create the module.
103/// wasm_module_t* module = wasm_module_new(store, &wasm);
104/// assert(module);
105///
106/// // Instantiate the module.
107/// wasm_extern_vec_t imports = WASM_EMPTY_VEC;
108/// wasm_trap_t* trap = NULL;
109///
110/// wasm_instance_t* instance = wasm_instance_new(store, module, &imports, &trap);
111/// assert(instance);
112///
113/// // Now do something with the instance, like calling the
114/// // exports with `wasm_instance_exports`.
115///
116/// // Free everything.
117/// wasm_instance_delete(instance);
118/// wasm_module_delete(module);
119/// wasm_byte_vec_delete(&wasm);
120/// wasm_byte_vec_delete(&wat);
121/// wasm_store_delete(store);
122/// wasm_engine_delete(engine);
123///
124/// return 0;
125/// }
126/// # })
127/// # .success();
128/// # }
129/// ```
130///
131/// cbindgen:ignore
132pub mod instance;
133
134/// A WebAssembly module contains stateless WebAssembly code that has
135/// already been compiled and can be instantiated multiple times.
136///
137/// Entry points: A WebAssembly module is created with
138/// [`wasm_module_new`][module::wasm_module_new] and freed with
139/// [`wasm_module_delete`][module::wasm_module_delete].
140///
141/// # Example
142///
143/// ```rust
144/// # use wasmer_inline_c::assert_c;
145/// # fn main() {
146/// # (assert_c! {
147/// # #include "tests/wasmer.h"
148/// #
149/// int main() {
150/// // Create the engine and the store.
151/// wasm_engine_t* engine = wasm_engine_new();
152/// wasm_store_t* store = wasm_store_new(engine);
153///
154/// // Create a WebAssembly module from a WAT definition.
155/// wasm_byte_vec_t wat;
156/// wasmer_byte_vec_new_from_string(&wat, "(module)");
157/// wasm_byte_vec_t wasm;
158/// wat2wasm(&wat, &wasm);
159///
160/// // Create the module.
161/// wasm_module_t* module = wasm_module_new(store, &wasm);
162///
163/// // It works!
164/// assert(module);
165///
166/// // Free everything.
167/// wasm_byte_vec_delete(&wasm);
168/// wasm_byte_vec_delete(&wat);
169/// wasm_module_delete(module);
170/// wasm_store_delete(store);
171/// wasm_engine_delete(engine);
172///
173/// return 0;
174/// }
175/// # })
176/// # .success();
177/// # }
178/// ```
179///
180/// cbindgen:ignore
181pub mod module;
182
183/// A store represents all global state that can be manipulated by
184/// WebAssembly programs. It consists of the runtime representation of
185/// all instances of functions, tables, memories, and globals that
186/// have been allocated during the lifetime of the abstract machine.
187///
188/// The store holds the [engine] (that is —amonst many things— used to
189/// compile the Wasm bytes into a valid [module] artifact), in addition
190/// to extra private types.
191///
192/// Entry points: A store is created with
193/// [`wasm_store_new`][store::wasm_store_new] and freed with
194/// [`wasm_store_delete`][store::wasm_store_delete]. To customize the
195/// engine the store holds, see
196/// [`wasm_config_new`][engine::wasm_config_new].
197///
198/// # Example
199///
200/// ```rust
201/// # use wasmer_inline_c::assert_c;
202/// # fn main() {
203/// # (assert_c! {
204/// # #include "tests/wasmer.h"
205/// #
206/// int main() {
207/// // Create the engine.
208/// wasm_engine_t* engine = wasm_engine_new();
209///
210/// // Create the store.
211/// wasm_store_t* store = wasm_store_new(engine);
212///
213/// // It works!
214/// assert(store);
215///
216/// // Free everything.
217/// wasm_store_delete(store);
218/// wasm_engine_delete(engine);
219///
220/// return 0;
221/// }
222/// # })
223/// # .success();
224/// # }
225/// ```
226///
227/// cbindgen:ignore
228pub mod store;
229
230/// A trap represents an error which stores trace message with
231/// backtrace.
232///
233/// # Example
234///
235/// ```rust
236/// # use wasmer_inline_c::assert_c;
237/// # fn main() {
238/// # (assert_c! {
239/// # #include "tests/wasmer.h"
240/// #
241/// int main() {
242/// // Create an engine and a store.
243/// wasm_engine_t* engine = wasm_engine_new();
244/// wasm_store_t* store = wasm_store_new(engine);
245///
246/// // Create the trap message.
247/// wasm_message_t message;
248/// wasm_name_new_from_string_nt(&message, "foobar");
249///
250/// // Create the trap with its message.
251/// // The backtrace will be generated automatically.
252/// wasm_trap_t* trap = wasm_trap_new(store, &message);
253/// assert(trap);
254///
255/// wasm_name_delete(&message);
256///
257/// // Do something with the trap.
258///
259/// // Free everything.
260/// wasm_trap_delete(trap);
261/// wasm_store_delete(store);
262/// wasm_engine_delete(engine);
263///
264/// return 0;
265/// }
266/// # })
267/// # .success();
268/// # }
269/// ```
270///
271/// Usually, a trap is returned from a host function (an imported
272/// function).
273///
274/// cbindgen:ignore
275pub mod trap;
276
277/// cbindgen:ignore
278pub mod types;
279
280/// This module contains _unstable non-standard_ C API.
281///
282/// Use them at your own risks. The API is subject to change or to
283/// break without any plan to keep any compatibility :-).
284pub mod unstable;
285
286/// Possible runtime values that a WebAssembly module can either
287/// consume or produce.
288///
289/// cbindgen:ignore
290pub mod value;
291
292/// Wasmer-specific API to get or query the version of this Wasm C API.
293///
294/// The `wasmer.h` file provides the `WASMER_VERSION`,
295/// `WASMER_VERSION_MAJOR`, `WASMER_VERSION_MINOR`,
296/// `WASMER_VERSION_PATCH` and `WASMER_VERSION_PRE`
297/// constants. However, in absence of this header file, it is possible
298/// to retrieve the same information with their respective functions,
299/// namely [`wasmer_version`][version::wasmer_version],
300/// [`wasmer_version_major`][version::wasmer_version_major],
301/// [`wasmer_version_minor`][version::wasmer_version_minor],
302/// [`wasmer_version_patch`][version::wasmer_version_patch], and
303/// [`wasmer_version_pre`][version::wasmer_version_pre].
304///
305/// # Example
306///
307/// ```rust
308/// # use wasmer_inline_c::assert_c;
309/// # fn main() {
310/// # (assert_c! {
311/// # #include "tests/wasmer.h"
312/// #
313/// int main() {
314/// // Get and print the version.
315/// const char* version = wasmer_version();
316/// printf("%s", version);
317///
318/// // No need to free the string. It's statically allocated on
319/// // the Rust side.
320///
321/// return 0;
322/// }
323/// # })
324/// # .success()
325/// # .stdout(env!("CARGO_PKG_VERSION"));
326/// # }
327/// ```
328pub mod version;
329
330#[cfg(feature = "wasi")]
331pub mod wasi;
332
333/// Wasmer-specific API to transform the WAT format into Wasm bytes.
334///
335/// It is used mostly for testing or for small program purposes.
336///
337/// # Example
338///
339/// ```rust
340/// # use wasmer_inline_c::assert_c;
341/// # fn main() {
342/// # (assert_c! {
343/// # #include "tests/wasmer.h"
344/// #
345/// int main() {
346/// // Our WAT module.
347/// wasm_byte_vec_t wat;
348/// wasm_byte_vec_new(&wat, 8, "(module)");
349///
350/// // Our Wasm bytes.
351/// wasm_byte_vec_t wasm;
352/// wat2wasm(&wat, &wasm);
353///
354/// // It works!
355/// assert(wasm.size > 0);
356///
357/// // Free everything.
358/// wasm_byte_vec_delete(&wasm);
359/// wasm_byte_vec_delete(&wat);
360///
361/// return 0;
362/// }
363/// # })
364/// # .success();
365/// # }
366/// ```
367#[cfg(feature = "wat")]
368pub mod wat;