Path: blob/main/crates/wizer/src/component.rs
2459 views
use crate::Wizer;1use anyhow::bail;23mod info;4mod instrument;5mod parse;6mod rewrite;7mod snapshot;8#[cfg(feature = "wasmtime")]9mod wasmtime;10#[cfg(feature = "wasmtime")]11pub use wasmtime::*;1213const WIZER_INSTANCE: &str = "wasmtime:wizer/access";1415pub use self::info::ComponentContext;1617impl Wizer {18/// Same as [`Wizer::instrument`], except for components.19pub fn instrument_component<'a>(20&self,21wasm: &'a [u8],22) -> anyhow::Result<(ComponentContext<'a>, Vec<u8>)> {23// Make sure we're given valid Wasm from the get go.24self.wasm_validate(&wasm)?;2526let mut cx = parse::parse(wasm)?;27let instrumented_wasm = instrument::instrument(&mut cx)?;28self.debug_assert_valid_wasm(&instrumented_wasm);2930Ok((cx, instrumented_wasm))31}3233/// Same as [`Wizer::snapshot`], except for components.34pub async fn snapshot_component(35&self,36mut cx: ComponentContext<'_>,37instance: &mut impl ComponentInstanceState,38) -> anyhow::Result<Vec<u8>> {39if !self.func_renames.is_empty() {40bail!("components do not support renaming functions");41}4243let snapshot = snapshot::snapshot(&cx, instance).await;44let rewritten_wasm = self.rewrite_component(&mut cx, &snapshot);45self.debug_assert_valid_wasm(&rewritten_wasm);4647Ok(rewritten_wasm)48}49}5051/// Trait representing the ability to invoke functions on a component to learn52/// about its internal state.53pub trait ComponentInstanceState: Send {54/// Looks up the exported `instance` which has `func` as an export, calls55/// it, and returns the `list<u8>` return type.56fn call_func_ret_list_u8(57&mut self,58instance: &str,59func: &str,60contents: impl FnOnce(&[u8]) + Send,61) -> impl Future<Output = ()> + Send;6263/// Same as [`Self::call_func_ret_list_u8`], but for the `s32` WIT type.64fn call_func_ret_s32(&mut self, instance: &str, func: &str)65-> impl Future<Output = i32> + Send;6667/// Same as [`Self::call_func_ret_list_u8`], but for the `s64` WIT type.68fn call_func_ret_s64(&mut self, instance: &str, func: &str)69-> impl Future<Output = i64> + Send;7071/// Same as [`Self::call_func_ret_list_u8`], but for the `f32` WIT type.72fn call_func_ret_f32(&mut self, instance: &str, func: &str)73-> impl Future<Output = u32> + Send;7475/// Same as [`Self::call_func_ret_list_u8`], but for the `f64` WIT type.76fn call_func_ret_f64(&mut self, instance: &str, func: &str)77-> impl Future<Output = u64> + Send;78}798081