wasmer_engine_staticlib/
builder.rs

1use crate::StaticlibEngine;
2use wasmer_compiler::{CompilerConfig, Features, Target};
3
4/// The Staticlib builder
5pub struct Staticlib {
6    compiler_config: Option<Box<dyn CompilerConfig>>,
7    target: Option<Target>,
8    features: Option<Features>,
9}
10
11impl Staticlib {
12    #[cfg(feature = "compiler")]
13    /// Create a new Staticlib
14    pub fn new<T>(compiler_config: T) -> Self
15    where
16        T: Into<Box<dyn CompilerConfig>>,
17    {
18        let mut compiler_config = compiler_config.into();
19        compiler_config.enable_pic();
20
21        Self {
22            compiler_config: Some(compiler_config),
23            target: None,
24            features: None,
25        }
26    }
27
28    /// Create a new headless Staticlib
29    pub fn headless() -> Self {
30        Self {
31            compiler_config: None,
32            target: None,
33            features: None,
34        }
35    }
36
37    /// Set the target
38    pub fn target(mut self, target: Target) -> Self {
39        self.target = Some(target);
40        self
41    }
42
43    /// Set the features
44    pub fn features(mut self, features: Features) -> Self {
45        self.features = Some(features);
46        self
47    }
48
49    /// Build the `StaticlibEngine` for this configuration
50    pub fn engine(self) -> StaticlibEngine {
51        if let Some(_compiler_config) = self.compiler_config {
52            #[cfg(feature = "compiler")]
53            {
54                let compiler_config = _compiler_config;
55                let target = self.target.unwrap_or_default();
56                let features = self
57                    .features
58                    .unwrap_or_else(|| compiler_config.default_features_for_target(&target));
59                let compiler = compiler_config.compiler();
60                StaticlibEngine::new(compiler, target, features)
61            }
62
63            #[cfg(not(feature = "compiler"))]
64            {
65                unreachable!("Cannot call `StaticlibEngine::new` without the `compiler` feature")
66            }
67        } else {
68            StaticlibEngine::headless()
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    #[cfg(feature = "compiler")]
77    use std::sync::Arc;
78    #[cfg(feature = "compiler")]
79    use wasmer_compiler::{Compiler, ModuleMiddleware};
80
81    #[cfg(feature = "compiler")]
82    #[derive(Default)]
83    pub struct TestCompilerConfig {
84        pub enabled_pic: bool,
85        pub middlewares: Vec<Arc<dyn ModuleMiddleware>>,
86    }
87
88    #[cfg(feature = "compiler")]
89    impl CompilerConfig for TestCompilerConfig {
90        fn enable_pic(&mut self) {
91            self.enabled_pic = true;
92        }
93
94        fn compiler(self: Box<Self>) -> Box<dyn Compiler> {
95            unimplemented!("compiler not implemented");
96        }
97
98        fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>) {
99            self.middlewares.push(middleware);
100        }
101    }
102
103    #[cfg(feature = "compiler")]
104    #[test]
105    #[should_panic(expected = "compiler not implemented")]
106    fn build_engine() {
107        let compiler_config = TestCompilerConfig::default();
108        let staticlib = Staticlib::new(compiler_config);
109        let _engine = staticlib.engine();
110    }
111
112    #[test]
113    fn build_headless_engine() {
114        let staticlib = Staticlib::headless();
115        let _engine = staticlib.engine();
116    }
117}