Skip to main content

wasmer_compiler/engine/
builder.rs

1use super::Engine;
2#[cfg(feature = "compiler")]
3use crate::CompilerConfig;
4use wasmer_types::{Features, target::Target};
5
6/// The Builder contents of `Engine`
7pub struct EngineBuilder {
8    /// The compiler
9    #[cfg(feature = "compiler")]
10    compiler_config: Option<Box<dyn CompilerConfig>>,
11    /// The machine target
12    target: Option<Target>,
13    /// The features to compile the Wasm module with
14    features: Option<Features>,
15}
16
17impl EngineBuilder {
18    /// Create a new builder with pre-made components
19    #[cfg(feature = "compiler")]
20    pub fn new<T>(compiler_config: T) -> Self
21    where
22        T: Into<Box<dyn CompilerConfig>>,
23    {
24        Self {
25            compiler_config: Some(compiler_config.into()),
26            target: None,
27            features: None,
28        }
29    }
30
31    /// Create a new headless Backend
32    pub fn headless() -> Self {
33        Self {
34            #[cfg(feature = "compiler")]
35            compiler_config: None,
36            target: None,
37            features: None,
38        }
39    }
40
41    /// Set the target
42    pub fn set_target(mut self, target: Option<Target>) -> Self {
43        self.target = target;
44        self
45    }
46
47    /// Set the features
48    pub fn set_features(mut self, features: Option<Features>) -> Self {
49        self.features = features;
50        self
51    }
52
53    /// Build the `Engine` for this configuration
54    #[cfg(feature = "compiler")]
55    pub fn engine(self) -> Engine {
56        let target = self.target.unwrap_or_default();
57        if let Some(compiler_config) = self.compiler_config {
58            let features = self
59                .features
60                .unwrap_or_else(|| compiler_config.default_features_for_target(&target));
61            Engine::new(compiler_config, target, features)
62        } else {
63            Engine::headless()
64        }
65    }
66
67    /// Build the `Engine` for this configuration
68    #[cfg(not(feature = "compiler"))]
69    pub fn engine(self) -> Engine {
70        Engine::headless()
71    }
72
73    /// The Wasm features
74    pub fn features(&self) -> Option<&Features> {
75        self.features.as_ref()
76    }
77
78    /// The target
79    pub fn target(&self) -> Option<&Target> {
80        self.target.as_ref()
81    }
82}