wasmer_compiler/engine/
builder.rs

1use super::Engine;
2#[cfg(feature = "compiler")]
3use crate::CompilerConfig;
4use wasmer_types::{target::Target, Features, HashAlgorithm};
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    /// The hashing algorithm
16    hash_algorithm: Option<HashAlgorithm>,
17}
18
19impl EngineBuilder {
20    /// Create a new builder with pre-made components
21    #[cfg(feature = "compiler")]
22    pub fn new<T>(compiler_config: T) -> Self
23    where
24        T: Into<Box<dyn CompilerConfig>>,
25    {
26        Self {
27            compiler_config: Some(compiler_config.into()),
28            target: None,
29            features: None,
30            hash_algorithm: None,
31        }
32    }
33
34    /// Create a new headless Backend
35    pub fn headless() -> Self {
36        Self {
37            #[cfg(feature = "compiler")]
38            compiler_config: None,
39            target: None,
40            features: None,
41            hash_algorithm: None,
42        }
43    }
44
45    /// Set the target
46    pub fn set_target(mut self, target: Option<Target>) -> Self {
47        self.target = target;
48        self
49    }
50
51    /// Set the features
52    pub fn set_features(mut self, features: Option<Features>) -> Self {
53        self.features = features;
54        self
55    }
56
57    /// Set the hashing algorithm
58    pub fn set_hash_algorithm(mut self, hash_algorithm: Option<HashAlgorithm>) -> Self {
59        self.hash_algorithm = hash_algorithm;
60        self
61    }
62
63    /// Build the `Engine` for this configuration
64    #[cfg(feature = "compiler")]
65    pub fn engine(self) -> Engine {
66        let target = self.target.unwrap_or_default();
67        if let Some(compiler_config) = self.compiler_config {
68            let features = self
69                .features
70                .unwrap_or_else(|| compiler_config.default_features_for_target(&target));
71            let mut engine = Engine::new(compiler_config, target, features);
72
73            engine.set_hash_algorithm(self.hash_algorithm);
74
75            engine
76        } else {
77            Engine::headless()
78        }
79    }
80
81    /// Build the `Engine` for this configuration
82    #[cfg(not(feature = "compiler"))]
83    pub fn engine(self) -> Engine {
84        Engine::headless()
85    }
86
87    /// The Wasm features
88    pub fn features(&self) -> Option<&Features> {
89        self.features.as_ref()
90    }
91
92    /// The target
93    pub fn target(&self) -> Option<&Target> {
94        self.target.as_ref()
95    }
96}