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