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