wasmer_c_api/wasm_c_api/engine/config/
mod.rs

1#[cfg(feature = "sys")]
2pub(crate) mod sys;
3#[cfg(feature = "sys")]
4pub use sys::*;
5
6use crate::wasm_c_api::unstable::target_lexicon::wasmer_target_t;
7
8use super::{wasm_config_t, wasmer_backend_t};
9
10#[cfg(feature = "v8")]
11pub(crate) mod v8;
12
13#[repr(C)]
14#[derive(Debug)]
15pub(crate) enum wasmer_backend_config_kind_t {
16    #[cfg(feature = "sys")]
17    Sys(sys::wasmer_sys_engine_config_t),
18
19    #[cfg(feature = "v8")]
20    V8(v8::wasmer_v8_engine_config_t),
21}
22
23impl Default for wasmer_backend_config_kind_t {
24    fn default() -> Self {
25        match wasmer_backend_t::default() {
26            #[cfg(feature = "llvm")]
27            super::wasmer_backend_t::LLVM => Self::Sys(sys::wasmer_sys_engine_config_t::default()),
28            #[cfg(feature = "cranelift")]
29            super::wasmer_backend_t::CRANELIFT => {
30                Self::Sys(sys::wasmer_sys_engine_config_t::default())
31            }
32            #[cfg(feature = "singlepass")]
33            super::wasmer_backend_t::SINGLEPASS => {
34                Self::Sys(sys::wasmer_sys_engine_config_t::default())
35            }
36            #[cfg(feature = "sys")]
37            super::wasmer_backend_t::HEADLESS => {
38                Self::Sys(sys::wasmer_sys_engine_config_t::default())
39            }
40            #[cfg(feature = "v8")]
41            super::wasmer_backend_t::V8 => Self::V8(v8::wasmer_v8_engine_config_t),
42
43            #[allow(unreachable_patterns)]
44            _ => unreachable!(),
45        }
46    }
47}
48
49#[repr(C)]
50#[derive(Debug, Default)]
51pub(crate) struct wasmer_backend_config_t {
52    pub inner: wasmer_backend_config_kind_t,
53    pub target: Option<Box<wasmer_target_t>>,
54    #[cfg(feature = "middlewares")]
55    pub middlewares: Vec<wasmer_middleware_t>,
56}
57
58/// Unstable non-standard Wasmer-specific API to update the
59/// configuration to specify a particular target for the engine.
60///
61/// # Example
62///
63/// ```rust
64/// # use wasmer_inline_c::assert_c;
65/// # fn main() {
66/// #    (assert_c! {
67/// # #include "tests/wasmer.h"
68/// #
69/// int main() {
70///     // Create the configuration.
71///     wasm_config_t* config = wasm_config_new();
72///
73///     // Set the target.
74///     {
75///         wasmer_triple_t* triple = wasmer_triple_new_from_host();
76///         wasmer_cpu_features_t* cpu_features = wasmer_cpu_features_new();
77///         wasmer_target_t* target = wasmer_target_new(triple, cpu_features);
78///
79///         wasm_config_sys_set_target(config, target);
80///     }
81///
82///     // Create the engine.
83///     wasm_engine_t* engine = wasm_engine_new_with_config(config);
84///
85///     // Check we have an engine!
86///     assert(engine);
87///
88///     // Free everything.
89///     wasm_engine_delete(engine);
90///
91///     return 0;
92/// }
93/// #    })
94/// #    .success();
95/// # }
96/// ```
97#[unsafe(no_mangle)]
98pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box<wasmer_target_t>) {
99    config.backend_config.target = Some(target);
100}
101
102/// Updates the configuration to add a module middleware.
103///
104/// This function takes ownership of `middleware`.
105///
106/// This is a Wasmer-specific function.
107///
108/// # Example
109///
110/// See the documentation of the [`wasmer_middlewares::metering`] module.
111#[unsafe(no_mangle)]
112#[cfg(feature = "middlewares")]
113pub extern "C" fn wasm_config_push_middleware(
114    config: &mut wasm_config_t,
115    middleware: Box<wasmer_middleware_t>,
116) {
117    config.backend_config.middlewares.push(*middleware)
118}