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