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::default()),
60            #[cfg(feature = "wasmi")]
61            super::wasmer_backend_t::WASMI => {
62                Self::Wasmi(wasmi::wasmer_wasmi_engine_config_t::default())
63            }
64            #[cfg(feature = "wamr")]
65            super::wasmer_backend_t::WAMR => {
66                Self::Wamr(wamr::wasmer_wamr_engine_config_t::default())
67            }
68            #[cfg(feature = "jsc")]
69            super::wasmer_backend_t::JSC => Self::Jsc(jsc::wasmer_jsc_engine_config_t::default()),
70
71            _ => unreachable!(),
72        }
73    }
74}
75
76#[repr(C)]
77#[derive(Debug, Default)]
78pub(crate) struct wasmer_backend_config_t {
79    pub inner: wasmer_backend_config_kind_t,
80    pub target: Option<Box<wasmer_target_t>>,
81    #[cfg(feature = "middlewares")]
82    pub middlewares: Vec<wasmer_middleware_t>,
83}
84
85/// Unstable non-standard Wasmer-specific API to update the
86/// configuration to specify a particular target for the engine.
87///
88/// # Example
89///
90/// ```rust
91/// # use wasmer_inline_c::assert_c;
92/// # fn main() {
93/// #    (assert_c! {
94/// # #include "tests/wasmer.h"
95/// #
96/// int main() {
97///     // Create the configuration.
98///     wasm_config_t* config = wasm_config_new();
99///
100///     // Set the target.
101///     {
102///         wasmer_triple_t* triple = wasmer_triple_new_from_host();
103///         wasmer_cpu_features_t* cpu_features = wasmer_cpu_features_new();
104///         wasmer_target_t* target = wasmer_target_new(triple, cpu_features);
105///
106///         wasm_config_sys_set_target(config, target);
107///     }
108///
109///     // Create the engine.
110///     wasm_engine_t* engine = wasm_engine_new_with_config(config);
111///
112///     // Check we have an engine!
113///     assert(engine);
114///
115///     // Free everything.
116///     wasm_engine_delete(engine);
117///
118///     return 0;
119/// }
120/// #    })
121/// #    .success();
122/// # }
123/// ```
124#[unsafe(no_mangle)]
125pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box<wasmer_target_t>) {
126    config.backend_config.target = Some(target);
127}
128
129/// Updates the configuration to add a module middleware.
130///
131/// This function takes ownership of `middleware`.
132///
133/// This is a Wasmer-specific function.
134///
135/// # Example
136///
137/// See the documentation of the [`wasmer_middlewares::metering`] module.
138#[unsafe(no_mangle)]
139#[cfg(feature = "middlewares")]
140pub extern "C" fn wasm_config_push_middleware(
141    config: &mut wasm_config_t,
142    middleware: Box<wasmer_middleware_t>,
143) {
144    config.backend_config.middlewares.push(*middleware)
145}