unc_vm_engine/universal/
builder.rs

1use crate::universal::UniversalEngine;
2use unc_vm_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    pool: Option<super::LimitedMemoryPool>,
11}
12
13impl Universal {
14    /// Create a new Universal
15    pub fn new<T>(compiler_config: T) -> Self
16    where
17        T: Into<Box<dyn CompilerConfig>>,
18    {
19        Self {
20            compiler_config: Some(compiler_config.into()),
21            target: None,
22            features: None,
23            pool: None,
24        }
25    }
26
27    /// Create a new headless Universal
28    pub fn headless() -> Self {
29        Self { compiler_config: None, target: None, features: None, pool: None }
30    }
31
32    /// Set the target
33    pub fn target(mut self, target: Target) -> Self {
34        self.target = Some(target);
35        self
36    }
37
38    /// Set the features
39    pub fn features(mut self, features: Features) -> Self {
40        self.features = Some(features);
41        self
42    }
43
44    /// Set the pool of reusable code memory
45    pub fn code_memory_pool(mut self, pool: super::LimitedMemoryPool) -> Self {
46        self.pool = Some(pool);
47        self
48    }
49
50    /// Build the `UniversalEngine` for this configuration
51    pub fn engine(self) -> UniversalEngine {
52        let target = self.target.unwrap_or_default();
53        let pool =
54            self.pool.unwrap_or_else(|| panic!("Universal::code_memory_pool was not set up!"));
55        if let Some(compiler_config) = self.compiler_config {
56            let features = self
57                .features
58                .unwrap_or_else(|| compiler_config.default_features_for_target(&target));
59            let compiler = compiler_config.compiler();
60            UniversalEngine::new(compiler, target, features, pool)
61        } else {
62            UniversalEngine::headless(pool)
63        }
64    }
65}