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::WASMI => cfg!(feature = "wasmi"),
73        wasmer_backend_t::WAMR => cfg!(feature = "wamr"),
74        wasmer_backend_t::JSC => cfg!(feature = "jsc"),
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    #[cfg(not(target_os = "windows"))]
81    use inline_c::assert_c;
82    use std::env::{remove_var, set_var};
83    #[cfg(target_os = "windows")]
84    use wasmer_inline_c::assert_c;
85
86    #[test]
87    fn test_wasmer_is_headless() {
88        unsafe {
89            set_var(
90                "COMPILER",
91                if cfg!(feature = "compiler") { "0" } else { "1" },
92            );
93        }
94
95        (assert_c! {
96            #include "tests/wasmer.h"
97            #include <stdlib.h>
98
99            int main() {
100                assert(wasmer_is_headless() == (getenv("COMPILER")[0] == '1'));
101
102                return 0;
103            }
104        })
105        .success();
106
107        unsafe {
108            remove_var("COMPILER");
109        }
110    }
111
112    #[test]
113    fn test_wasmer_is_backend_available() {
114        unsafe {
115            set_var(
116                "CRANELIFT",
117                if cfg!(feature = "cranelift") {
118                    "1"
119                } else {
120                    "0"
121                },
122            );
123            set_var("LLVM", if cfg!(feature = "llvm") { "1" } else { "0" });
124            set_var(
125                "SINGLEPASS",
126                if cfg!(feature = "singlepass") {
127                    "1"
128                } else {
129                    "0"
130                },
131            );
132        }
133
134        (assert_c! {
135            #include "tests/wasmer.h"
136            #include <stdlib.h>
137
138            int main() {
139                assert(wasmer_is_backend_available(CRANELIFT) == (getenv("CRANELIFT")[0] == '1'));
140                assert(wasmer_is_backend_available(LLVM) == (getenv("LLVM")[0] == '1'));
141                assert(wasmer_is_backend_available(SINGLEPASS) == (getenv("SINGLEPASS")[0] == '1'));
142
143                return 0;
144            }
145        })
146        .success();
147
148        unsafe {
149            remove_var("CRANELIFT");
150            remove_var("LLVM");
151            remove_var("SINGLEPASS");
152        }
153    }
154}