wasmtime_wizer/
component.rs1use crate::Wizer;
2use anyhow::bail;
3
4mod info;
5mod instrument;
6mod parse;
7mod rewrite;
8mod snapshot;
9#[cfg(feature = "wasmtime")]
10mod wasmtime;
11#[cfg(feature = "wasmtime")]
12pub use wasmtime::*;
13
14const WIZER_INSTANCE: &str = "wasmtime:wizer/access";
15
16pub use self::info::ComponentContext;
17
18impl Wizer {
19 pub fn instrument_component<'a>(
21 &self,
22 wasm: &'a [u8],
23 ) -> anyhow::Result<(ComponentContext<'a>, Vec<u8>)> {
24 self.wasm_validate(&wasm)?;
26
27 let mut cx = parse::parse(wasm)?;
28 let instrumented_wasm = instrument::instrument(&mut cx)?;
29 self.debug_assert_valid_wasm(&instrumented_wasm);
30
31 Ok((cx, instrumented_wasm))
32 }
33
34 pub async fn snapshot_component(
36 &self,
37 mut cx: ComponentContext<'_>,
38 instance: &mut impl ComponentInstanceState,
39 ) -> anyhow::Result<Vec<u8>> {
40 if !self.func_renames.is_empty() {
41 bail!("components do not support renaming functions");
42 }
43
44 let snapshot = snapshot::snapshot(&cx, instance).await;
45 let rewritten_wasm = self.rewrite_component(&mut cx, &snapshot);
46 self.debug_assert_valid_wasm(&rewritten_wasm);
47
48 Ok(rewritten_wasm)
49 }
50}
51
52pub trait ComponentInstanceState: Send {
55 fn call_func_ret_list_u8(
58 &mut self,
59 instance: &str,
60 func: &str,
61 contents: impl FnOnce(&[u8]) + Send,
62 ) -> impl Future<Output = ()> + Send;
63
64 fn call_func_ret_s32(&mut self, instance: &str, func: &str)
66 -> impl Future<Output = i32> + Send;
67
68 fn call_func_ret_s64(&mut self, instance: &str, func: &str)
70 -> impl Future<Output = i64> + Send;
71
72 fn call_func_ret_f32(&mut self, instance: &str, func: &str)
74 -> impl Future<Output = u32> + Send;
75
76 fn call_func_ret_f64(&mut self, instance: &str, func: &str)
78 -> impl Future<Output = u64> + Send;
79}