wasmer_c_api/wasm_c_api/unstable/
engine.rs

1//! Unstable non-standard Wasmer-specific types for the
2//! `wasm_engine_t` and siblings.
3
4use super::{
5    super::engine::{wasm_config_t, wasmer_backend_t},
6    features::wasmer_features_t,
7};
8
9/// Unstable non-standard Wasmer-specific API to update the
10/// configuration to specify particular features for the engine.
11///
12/// # Example
13///
14/// ```rust
15/// # use wasmer_inline_c::assert_c;
16/// # fn main() {
17/// #    (assert_c! {
18/// # #include "tests/wasmer.h"
19/// #
20/// int main() {
21///     // Create the configuration.
22///     wasm_config_t* config = wasm_config_new();
23///
24///     // Set the target.
25///     {
26///         wasmer_features_t* features = wasmer_features_new();
27///         wasmer_features_simd(features, true);
28///
29///         wasm_config_set_features(config, features);
30///     }
31///
32///     // Create the engine.
33///     wasm_engine_t* engine = wasm_engine_new_with_config(config);
34///
35///     // Check we have an engine!
36///     assert(engine);
37///
38///     // Free everything.
39///     wasm_engine_delete(engine);
40///
41///     return 0;
42/// }
43/// #    })
44/// #    .success();
45/// # }
46/// ```
47#[unsafe(no_mangle)]
48pub extern "C" fn wasm_config_set_features(
49    config: &mut wasm_config_t,
50    features: Box<wasmer_features_t>,
51) {
52    config.features = Some(features);
53}
54
55/// Check whether there is no compiler available in this compiled
56/// library.
57#[unsafe(no_mangle)]
58pub extern "C" fn wasmer_is_headless() -> bool {
59    !cfg!(feature = "compiler")
60}
61
62/// Check whether the given backend is available, i.e. part of this
63/// compiled library.
64#[unsafe(no_mangle)]
65pub extern "C" fn wasmer_is_backend_available(backend: wasmer_backend_t) -> bool {
66    match backend {
67        wasmer_backend_t::LLVM => cfg!(feature = "llvm"),
68        wasmer_backend_t::CRANELIFT => cfg!(feature = "cranelift"),
69        wasmer_backend_t::SINGLEPASS => cfg!(feature = "singlepass"),
70        wasmer_backend_t::HEADLESS => cfg!(feature = "sys"),
71        wasmer_backend_t::V8 => cfg!(feature = "v8"),
72        wasmer_backend_t::JSC => cfg!(feature = "jsc"),
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    #[cfg(not(target_os = "windows"))]
79    use inline_c::assert_c;
80    use std::env::{remove_var, set_var};
81    #[cfg(target_os = "windows")]
82    use wasmer_inline_c::assert_c;
83
84    #[test]
85    fn test_wasmer_is_headless() {
86        unsafe {
87            set_var(
88                "COMPILER",
89                if cfg!(feature = "compiler") { "0" } else { "1" },
90            );
91        }
92
93        (assert_c! {
94            #include "tests/wasmer.h"
95            #include <stdlib.h>
96
97            int main() {
98                assert(wasmer_is_headless() == (getenv("COMPILER")[0] == '1'));
99
100                return 0;
101            }
102        })
103        .success();
104
105        unsafe {
106            remove_var("COMPILER");
107        }
108    }
109
110    #[test]
111    fn test_wasmer_is_backend_available() {
112        unsafe {
113            set_var(
114                "CRANELIFT",
115                if cfg!(feature = "cranelift") {
116                    "1"
117                } else {
118                    "0"
119                },
120            );
121            set_var("LLVM", if cfg!(feature = "llvm") { "1" } else { "0" });
122            set_var(
123                "SINGLEPASS",
124                if cfg!(feature = "singlepass") {
125                    "1"
126                } else {
127                    "0"
128                },
129            );
130        }
131
132        (assert_c! {
133            #include "tests/wasmer.h"
134            #include <stdlib.h>
135
136            int main() {
137                assert(wasmer_is_backend_available(CRANELIFT) == (getenv("CRANELIFT")[0] == '1'));
138                assert(wasmer_is_backend_available(LLVM) == (getenv("LLVM")[0] == '1'));
139                assert(wasmer_is_backend_available(SINGLEPASS) == (getenv("SINGLEPASS")[0] == '1'));
140
141                return 0;
142            }
143        })
144        .success();
145
146        unsafe {
147            remove_var("CRANELIFT");
148            remove_var("LLVM");
149            remove_var("SINGLEPASS");
150        }
151    }
152}