1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#[cfg(feature = "sys")]
pub(crate) mod sys;
#[cfg(feature = "sys")]
pub use sys::*;

use crate::wasm_c_api::unstable::target_lexicon::wasmer_target_t;

use super::{wasm_config_t, wasmer_backend_t};

#[cfg(feature = "jsc")]
pub(crate) mod jsc;

#[cfg(feature = "v8")]
pub(crate) mod v8;

#[cfg(feature = "wasmi")]
pub(crate) mod wasmi;

#[cfg(feature = "wamr")]
pub(crate) mod wamr;

#[repr(C)]
#[derive(Debug)]
pub(crate) enum wasmer_backend_config_kind_t {
    #[cfg(feature = "sys")]
    Sys(sys::wasmer_sys_engine_config_t),

    #[cfg(feature = "jsc")]
    Jsc(jsc::wasmer_jsc_engine_config_t),

    #[cfg(feature = "v8")]
    V8(v8::wasmer_v8_engine_config_t),

    #[cfg(feature = "wasmi")]
    Wasmi(wasmi::wasmer_wasmi_engine_config_t),

    #[cfg(feature = "wamr")]
    Wamr(wamr::wasmer_wamr_engine_config_t),
}

impl Default for wasmer_backend_config_kind_t {
    fn default() -> Self {
        match wasmer_backend_t::default() {
            #[cfg(feature = "llvm")]
            super::wasmer_backend_t::LLVM => Self::Sys(sys::wasmer_sys_engine_config_t::default()),
            #[cfg(feature = "cranelift")]
            super::wasmer_backend_t::CRANELIFT => {
                Self::Sys(sys::wasmer_sys_engine_config_t::default())
            }
            #[cfg(feature = "singlepass")]
            super::wasmer_backend_t::SINGLEPASS => {
                Self::Sys(sys::wasmer_sys_engine_config_t::default())
            }
            #[cfg(feature = "sys")]
            super::wasmer_backend_t::HEADLESS => {
                Self::Sys(sys::wasmer_sys_engine_config_t::default())
            }
            #[cfg(feature = "v8")]
            super::wasmer_backend_t::V8 => Self::V8(v8::wasmer_v8_engine_config_t::default()),
            #[cfg(feature = "wasmi")]
            super::wasmer_backend_t::WASMI => {
                Self::Wasmi(wasmi::wasmer_wasmi_engine_config_t::default())
            }
            #[cfg(feature = "wamr")]
            super::wasmer_backend_t::WAMR => {
                Self::Wamr(wamr::wasmer_wamr_engine_config_t::default())
            }
            #[cfg(feature = "jsc")]
            super::wasmer_backend_t::JSC => Self::Jsc(jsc::wasmer_jsc_engine_config_t::default()),

            _ => unreachable!(),
        }
    }
}

#[repr(C)]
#[derive(Debug, Default)]
pub(crate) struct wasmer_backend_config_t {
    pub inner: wasmer_backend_config_kind_t,
    pub target: Option<Box<wasmer_target_t>>,
    #[cfg(feature = "middlewares")]
    pub middlewares: Vec<wasmer_middleware_t>,
}

/// Unstable non-standard Wasmer-specific API to update the
/// configuration to specify a particular target for the engine.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// #    (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
///     // Create the configuration.
///     wasm_config_t* config = wasm_config_new();
///
///     // Set the target.
///     {
///         wasmer_triple_t* triple = wasmer_triple_new_from_host();
///         wasmer_cpu_features_t* cpu_features = wasmer_cpu_features_new();
///         wasmer_target_t* target = wasmer_target_new(triple, cpu_features);
///
///         wasm_config_sys_set_target(config, target);
///     }
///
///     // Create the engine.
///     wasm_engine_t* engine = wasm_engine_new_with_config(config);
///
///     // Check we have an engine!
///     assert(engine);
///
///     // Free everything.
///     wasm_engine_delete(engine);
///
///     return 0;
/// }
/// #    })
/// #    .success();
/// # }
/// ```
#[no_mangle]
pub extern "C" fn wasm_config_set_target(config: &mut wasm_config_t, target: Box<wasmer_target_t>) {
    config.backend_config.target = Some(target);
}

/// Updates the configuration to add a module middleware.
///
/// This function takes ownership of `middleware`.
///
/// This is a Wasmer-specific function.
///
/// # Example
///
/// See the documentation of the [`metering`] module.
#[no_mangle]
#[cfg(feature = "middlewares")]
pub extern "C" fn wasm_config_push_middleware(
    config: &mut wasm_config_t,
    middleware: Box<wasmer_middleware_t>,
) {
    config.backend_config.middlewares.push(*middleware)
}