marine_wasmtime_backend/
lib.rs1mod caller;
18mod store;
19mod utils;
20mod module;
21mod instance;
22mod wasi;
23mod function;
24mod imports;
25mod memory;
26
27use store::*;
28use caller::*;
29use module::*;
30use instance::*;
31use wasi::*;
32use function::*;
33use memory::*;
34use imports::*;
35use utils::*;
36
37use marine_wasm_backend_traits::prelude::*;
38
39use wasmtime_wasi::WasiCtx;
40
41const MB: usize = 1024 * 1024;
42
43pub const DEFAULT_WASM_STACK_SIZE: usize = 2 * MB;
45
46#[derive(Clone)]
47pub struct WasmtimeWasmBackend {
48 engine: wasmtime::Engine,
49}
50
51impl WasmBackend for WasmtimeWasmBackend {
52 type Store = WasmtimeStore;
53 type Module = WasmtimeModule;
54 type Imports = WasmtimeImports;
55 type Instance = WasmtimeInstance;
56 type Context<'c> = WasmtimeContext<'c>;
57 type ContextMut<'c> = WasmtimeContextMut<'c>;
58 type ImportCallContext<'c> = WasmtimeImportCallContext<'c>;
59 type HostFunction = WasmtimeFunction;
60 type ExportFunction = WasmtimeFunction;
61 type Memory = WasmtimeMemory;
62 type MemoryView = WasmtimeMemory;
63 type Wasi = WasmtimeWasi;
64
65 fn new_async() -> WasmBackendResult<Self> {
66 Self::new(WasmtimeConfig::default())
67 }
68}
69
70impl WasmtimeWasmBackend {
71 pub fn increment_epoch(&self) {
72 self.engine.increment_epoch()
73 }
74
75 pub fn new(config: WasmtimeConfig) -> WasmBackendResult<Self> {
76 let engine =
77 wasmtime::Engine::new(&config.config).map_err(WasmBackendError::InitializationError)?;
78
79 Ok(Self { engine })
80 }
81}
82
83#[derive(Default)]
84pub struct StoreState {
85 wasi: Vec<WasiCtx>, limits: MemoryLimiter,
87}
88
89#[derive(Clone)]
90pub struct WasmtimeConfig {
91 config: wasmtime::Config,
92}
93
94impl Default for WasmtimeConfig {
95 fn default() -> Self {
96 let mut config = wasmtime::Config::default();
97 config
98 .async_support(true)
99 .debug_info(true)
100 .max_wasm_stack(DEFAULT_WASM_STACK_SIZE)
101 .epoch_interruption(true)
102 .wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
103
104 Self { config }
105 }
106}
107
108impl WasmtimeConfig {
109 pub fn from_raw(mut config: wasmtime::Config) -> Self {
112 config.async_support(true);
113 Self { config }
114 }
115
116 pub fn debug_info(&mut self, enable: bool) -> &mut Self {
121 self.config.debug_info(enable);
122 self
123 }
124
125 pub fn epoch_interruption(&mut self, enable: bool) -> &mut Self {
129 self.config.epoch_interruption(enable);
130 self
131 }
132
133 pub fn max_wasm_stack(&mut self, size: usize) -> &mut Self {
138 self.config.max_wasm_stack(size);
139 self
140 }
141
142 pub fn async_wasm_stack(&mut self, size: usize) -> &mut Self {
149 self.config.async_stack_size(size);
150 self
151 }
152
153 pub fn wasm_backtrace(&mut self, enable: bool) -> &mut Self {
157 self.config
158 .wasm_backtrace(enable)
159 .wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
160 self
161 }
162}