1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::errors::{Error, Result};
use crate::{WasmtimeEngineProvider, WasmtimeEngineProviderPre};
#[allow(missing_debug_implementations)]
#[derive(Default)]
pub struct WasmtimeEngineProviderBuilder<'a> {
engine: Option<wasmtime::Engine>,
module: Option<wasmtime::Module>,
module_bytes: Option<&'a [u8]>,
#[cfg(feature = "cache")]
cache_enabled: bool,
#[cfg(feature = "cache")]
cache_path: Option<std::path::PathBuf>,
wasi_params: Option<wapc::WasiParams>,
epoch_deadlines: Option<crate::EpochDeadlines>,
}
#[allow(deprecated)]
impl<'a> WasmtimeEngineProviderBuilder<'a> {
#[must_use]
pub fn new() -> Self {
Default::default()
}
#[must_use]
pub fn module_bytes(mut self, module_bytes: &'a [u8]) -> Self {
self.module_bytes = Some(module_bytes);
self
}
#[must_use]
pub fn module(mut self, module: wasmtime::Module) -> Self {
self.module = Some(module);
self
}
#[must_use]
pub fn engine(mut self, engine: wasmtime::Engine) -> Self {
self.engine = Some(engine);
self
}
#[must_use]
pub fn wasi_params(mut self, wasi: wapc::WasiParams) -> Self {
self.wasi_params = Some(wasi);
self
}
#[cfg(feature = "cache")]
#[must_use]
pub fn enable_cache(mut self, path: Option<&std::path::Path>) -> Self {
self.cache_enabled = true;
self.cache_path = path.map(|p| p.to_path_buf());
self
}
#[must_use]
pub fn enable_epoch_interruptions(mut self, wapc_init_deadline: u64, wapc_func_deadline: u64) -> Self {
self.epoch_deadlines = Some(crate::EpochDeadlines {
wapc_init: wapc_init_deadline,
wapc_func: wapc_func_deadline,
});
self
}
pub(crate) fn build_pre(&self) -> Result<WasmtimeEngineProviderPre> {
if self.module_bytes.is_some() && self.module.is_some() {
return Err(Error::BuilderInvalidConfig(
"`module_bytes` and `module` cannot be provided at the same time".to_owned(),
));
}
if self.module_bytes.is_none() && self.module.is_none() {
return Err(Error::BuilderInvalidConfig(
"Neither `module_bytes` nor `module` have been provided".to_owned(),
));
}
let mut pre = match &self.engine {
Some(e) => {
let module = self.module_bytes.as_ref().map_or_else(
|| Ok(self.module.as_ref().unwrap().clone()),
|module_bytes| wasmtime::Module::new(e, module_bytes),
)?;
WasmtimeEngineProviderPre::new(e.clone(), module, self.wasi_params.clone())
}
None => {
let mut config = wasmtime::Config::default();
if self.epoch_deadlines.is_some() {
config.epoch_interruption(true);
}
cfg_if::cfg_if! {
if #[cfg(feature = "cache")] {
if self.cache_enabled {
config.strategy(wasmtime::Strategy::Cranelift);
if let Some(cache) = &self.cache_path {
config.cache_config_load(cache)?;
} else if let Err(e) = config.cache_config_load_default() {
warn!("Wasmtime cache configuration not found ({}). Repeated loads will speed up significantly with a cache configuration. See https://docs.wasmtime.dev/cli-cache.html for more information.",e);
}
}
}
}
let engine = wasmtime::Engine::new(&config)?;
let module = self.module_bytes.as_ref().map_or_else(
|| Ok(self.module.as_ref().unwrap().clone()),
|module_bytes| wasmtime::Module::new(&engine, module_bytes),
)?;
WasmtimeEngineProviderPre::new(engine, module, self.wasi_params.clone())
}
}?;
pre.epoch_deadlines = self.epoch_deadlines;
Ok(pre)
}
pub fn build(&self) -> Result<WasmtimeEngineProvider> {
let pre = self.build_pre()?;
pre.rehydrate()
}
}