wasmer_engine_universal/
builder.rs

1use crate::UniversalEngine;
2use wasmer_compiler::{CompilerConfig, Features, Target};
3
4/// The Universal builder
5pub struct Universal {
6    #[allow(dead_code)]
7    compiler_config: Option<Box<dyn CompilerConfig>>,
8    target: Option<Target>,
9    features: Option<Features>,
10}
11
12impl Universal {
13    /// Create a new Universal
14    pub fn new<T>(compiler_config: T) -> Self
15    where
16        T: Into<Box<dyn CompilerConfig>>,
17    {
18        Self {
19            compiler_config: Some(compiler_config.into()),
20            target: None,
21            features: None,
22        }
23    }
24
25    /// Create a new headless Universal
26    pub fn headless() -> Self {
27        Self {
28            compiler_config: None,
29            target: None,
30            features: None,
31        }
32    }
33
34    /// Set the target
35    pub fn target(mut self, target: Target) -> Self {
36        self.target = Some(target);
37        self
38    }
39
40    /// Set the features
41    pub fn features(mut self, features: Features) -> Self {
42        self.features = Some(features);
43        self
44    }
45
46    /// Build the `UniversalEngine` for this configuration
47    #[cfg(feature = "compiler")]
48    pub fn engine(self) -> UniversalEngine {
49        let target = self.target.unwrap_or_default();
50        if let Some(compiler_config) = self.compiler_config {
51            let features = self
52                .features
53                .unwrap_or_else(|| compiler_config.default_features_for_target(&target));
54            let compiler = compiler_config.compiler();
55            UniversalEngine::new(compiler, target, features)
56        } else {
57            UniversalEngine::headless()
58        }
59    }
60
61    /// Build the `UniversalEngine` for this configuration
62    #[cfg(not(feature = "compiler"))]
63    pub fn engine(self) -> UniversalEngine {
64        UniversalEngine::headless()
65    }
66}