wasmer_compiler/engine/
builder.rs1use super::Engine;
2#[cfg(feature = "compiler")]
3use crate::CompilerConfig;
4use wasmer_types::{target::Target, Features, HashAlgorithm};
5
6pub struct EngineBuilder {
8 #[cfg(feature = "compiler")]
10 compiler_config: Option<Box<dyn CompilerConfig>>,
11 target: Option<Target>,
13 features: Option<Features>,
15 hash_algorithm: Option<HashAlgorithm>,
17}
18
19impl EngineBuilder {
20 #[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 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 pub fn set_target(mut self, target: Option<Target>) -> Self {
47 self.target = target;
48 self
49 }
50
51 pub fn set_features(mut self, features: Option<Features>) -> Self {
53 self.features = features;
54 self
55 }
56
57 pub fn set_hash_algorithm(mut self, hash_algorithm: Option<HashAlgorithm>) -> Self {
59 self.hash_algorithm = hash_algorithm;
60 self
61 }
62
63 #[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 #[cfg(not(feature = "compiler"))]
83 pub fn engine(self) -> Engine {
84 Engine::headless()
85 }
86
87 pub fn features(&self) -> Option<&Features> {
89 self.features.as_ref()
90 }
91
92 pub fn target(&self) -> Option<&Target> {
94 self.target.as_ref()
95 }
96}