use super::Engine;
use crate::{types::target::Target, CompilerConfig};
use wasmer_types::{Features, HashAlgorithm};
pub struct EngineBuilder {
compiler_config: Option<Box<dyn CompilerConfig>>,
target: Option<Target>,
features: Option<Features>,
hash_algorithm: Option<HashAlgorithm>,
}
impl EngineBuilder {
pub fn new<T>(compiler_config: T) -> Self
where
T: Into<Box<dyn CompilerConfig>>,
{
Self {
compiler_config: Some(compiler_config.into()),
target: None,
features: None,
hash_algorithm: None,
}
}
pub fn headless() -> Self {
Self {
compiler_config: None,
target: None,
features: None,
hash_algorithm: None,
}
}
pub fn set_target(mut self, target: Option<Target>) -> Self {
self.target = target;
self
}
pub fn set_features(mut self, features: Option<Features>) -> Self {
self.features = features;
self
}
pub fn set_hash_algorithm(mut self, hash_algorithm: Option<HashAlgorithm>) -> Self {
self.hash_algorithm = hash_algorithm;
self
}
#[cfg(feature = "compiler")]
pub fn engine(self) -> Engine {
let target = self.target.unwrap_or_default();
if let Some(compiler_config) = self.compiler_config {
let features = self
.features
.unwrap_or_else(|| compiler_config.default_features_for_target(&target));
let mut engine = Engine::new(compiler_config, target, features);
engine.set_hash_algorithm(self.hash_algorithm);
engine
} else {
Engine::headless()
}
}
#[cfg(not(feature = "compiler"))]
pub fn engine(self) -> Engine {
Engine::headless()
}
pub fn features(&self) -> Option<&Features> {
self.features.as_ref()
}
pub fn target(&self) -> Option<&Target> {
self.target.as_ref()
}
}