Get or compile a WASM module from raw bytes. Modules are cached by SHA-256 hash of the binary. First compilation is slow (Cranelift JIT); subsequent calls return the cached module.
(&self, wasm_bytes: &[u8])
| 48 | /// Modules are cached by SHA-256 hash of the binary. First compilation |
| 49 | /// is slow (Cranelift JIT); subsequent calls return the cached module. |
| 50 | pub fn get_or_compile(&self, wasm_bytes: &[u8]) -> crate::Result<Arc<Module>> { |
| 51 | let hash = sha256(wasm_bytes); |
| 52 | |
| 53 | // Fast path: check cache. |
| 54 | { |
| 55 | let cache = self.module_cache.lock().unwrap_or_else(|p| p.into_inner()); |
| 56 | if let Some(module) = cache.get(&hash) { |
| 57 | return Ok(Arc::clone(module)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Slow path: compile and cache. |
| 62 | let module = |
| 63 | Module::new(&self.engine, wasm_bytes).map_err(|e| crate::Error::BadRequest { |
| 64 | detail: format!("WASM module compilation failed: {e}"), |
| 65 | })?; |
| 66 | let arc = Arc::new(module); |
| 67 | |
| 68 | let mut cache = self.module_cache.lock().unwrap_or_else(|p| p.into_inner()); |
| 69 | cache.entry(hash).or_insert_with(|| Arc::clone(&arc)); |
| 70 | |
| 71 | Ok(arc) |
| 72 | } |
| 73 | |
| 74 | /// Get a reference to the underlying wasmtime Engine. |
| 75 | pub fn engine(&self) -> &Engine { |
no test coverage detected