lunatic_process/runtimes/
mod.rs1use std::sync::Arc;
10
11use anyhow::Result;
12use dashmap::DashMap;
13use tokio::task::JoinHandle;
14
15use crate::state::ProcessState;
16
17use self::wasmtime::{WasmtimeCompiledModule, WasmtimeRuntime};
18
19pub mod wasmtime;
20
21pub struct RawWasm {
22 pub id: Option<u64>,
24 pub bytes: Vec<u8>,
25}
26
27impl RawWasm {
28 pub fn new(id: Option<u64>, bytes: Vec<u8>) -> Self {
29 Self { id, bytes }
30 }
31
32 pub fn as_slice(&self) -> &[u8] {
33 self.bytes.as_slice()
34 }
35}
36
37impl From<Vec<u8>> for RawWasm {
38 fn from(bytes: Vec<u8>) -> Self {
39 Self::new(None, bytes)
40 }
41}
42
43pub trait WasmRuntime<T>: Clone
49where
50 T: crate::state::ProcessState + Default + Send,
51{
52 type WasmInstance: WasmInstance;
53
54 fn compile_module(&mut self, data: RawWasm) -> anyhow::Result<usize>;
56
57 fn wasm_module(&self, index: usize) -> Option<&RawWasm>;
59
60 }
68
69pub trait WasmInstance {
70 type Param;
71
72 }
75
76pub struct Modules<T> {
77 modules: Arc<DashMap<u64, Arc<WasmtimeCompiledModule<T>>>>,
78}
79
80impl<T> Clone for Modules<T> {
81 fn clone(&self) -> Self {
82 Self {
83 modules: self.modules.clone(),
84 }
85 }
86}
87
88impl<T> Default for Modules<T> {
89 fn default() -> Self {
90 Self {
91 modules: Arc::new(DashMap::new()),
92 }
93 }
94}
95
96impl<T: ProcessState + 'static> Modules<T> {
97 pub fn get(&self, module_id: u64) -> Option<Arc<WasmtimeCompiledModule<T>>> {
98 self.modules.get(&module_id).map(|m| m.clone())
99 }
100
101 pub fn compile(
102 &self,
103 runtime: WasmtimeRuntime,
104 wasm: RawWasm,
105 ) -> JoinHandle<Result<Arc<WasmtimeCompiledModule<T>>>> {
106 let modules = self.modules.clone();
107 tokio::task::spawn_blocking(move || {
108 let id = wasm.id;
109 match runtime.compile_module(wasm) {
110 Ok(m) => {
111 let module = Arc::new(m);
112 if let Some(id) = id {
113 modules.insert(id, Arc::clone(&module));
114 }
115 Ok(module)
116 }
117 Err(e) => Err(e),
118 }
119 })
120 }
121}