Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-http/src/p3/mod.rs
1692 views
1
//! Experimental, unstable and incomplete implementation of wasip3 version of `wasi:http`.
2
//!
3
//! This module is under heavy development.
4
//! It is not compliant with semver and is not ready
5
//! for production use.
6
//!
7
//! Bug and security fixes limited to wasip3 will not be given patch releases.
8
//!
9
//! Documentation of this module may be incorrect or out-of-sync with the implementation.
10
11
pub mod bindings;
12
mod conv;
13
#[expect(unused, reason = "work in progress")] // TODO: implement
14
mod host;
15
16
use bindings::http::{handler, types};
17
use wasmtime::component::{HasData, Linker, ResourceTable};
18
19
pub(crate) struct WasiHttp;
20
21
impl HasData for WasiHttp {
22
type Data<'a> = WasiHttpCtxView<'a>;
23
}
24
25
#[derive(Clone, Default)]
26
pub struct WasiHttpCtx {}
27
28
pub struct WasiHttpCtxView<'a> {
29
pub ctx: &'a mut WasiHttpCtx,
30
pub table: &'a mut ResourceTable,
31
}
32
33
pub trait WasiHttpView: Send {
34
fn http(&mut self) -> WasiHttpCtxView<'_>;
35
}
36
37
/// Add all interfaces from this module into the `linker` provided.
38
///
39
/// This function will add all interfaces implemented by this module to the
40
/// [`Linker`], which corresponds to the `wasi:http/imports` world supported by
41
/// this module.
42
///
43
/// # Example
44
///
45
/// ```
46
/// use wasmtime::{Engine, Result, Store, Config};
47
/// use wasmtime::component::{Linker, ResourceTable};
48
/// use wasmtime_wasi_http::p3::{WasiHttpCtx, WasiHttpCtxView, WasiHttpView};
49
///
50
/// fn main() -> Result<()> {
51
/// let mut config = Config::new();
52
/// config.async_support(true);
53
/// config.wasm_component_model_async(true);
54
/// let engine = Engine::new(&config)?;
55
///
56
/// let mut linker = Linker::<MyState>::new(&engine);
57
/// wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;
58
/// // ... add any further functionality to `linker` if desired ...
59
///
60
/// let mut store = Store::new(
61
/// &engine,
62
/// MyState::default(),
63
/// );
64
///
65
/// // ... use `linker` to instantiate within `store` ...
66
///
67
/// Ok(())
68
/// }
69
///
70
/// #[derive(Default)]
71
/// struct MyState {
72
/// http: WasiHttpCtx,
73
/// table: ResourceTable,
74
/// }
75
///
76
/// impl WasiHttpView for MyState {
77
/// fn http(&mut self) -> WasiHttpCtxView<'_> {
78
/// WasiHttpCtxView {
79
/// ctx: &mut self.http,
80
/// table: &mut self.table,
81
/// }
82
/// }
83
/// }
84
/// ```
85
pub fn add_to_linker<T>(linker: &mut Linker<T>) -> wasmtime::Result<()>
86
where
87
T: WasiHttpView + 'static,
88
{
89
handler::add_to_linker::<_, WasiHttp>(linker, T::http)?;
90
types::add_to_linker::<_, WasiHttp>(linker, T::http)?;
91
Ok(())
92
}
93
94