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