linera_wasmer_compiler/engine/
builder.rs

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