1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Implementation of the `wasi:http/proxy` world.
//!
//! The implementation at the top of the module for use in async contexts,
//! while the `sync` module provides implementation for use in sync contexts.

use crate::WasiHttpView;

mod bindings {
    #![allow(missing_docs)]
    wasmtime::component::bindgen!({
        world: "wasi:http/proxy",
        tracing: true,
        async: true,
        with: {
            "wasi:http": crate::bindings::http,
            "wasi": wasmtime_wasi::bindings,
        },
    });
}

/// Raw bindings to the `wasi:http/proxy` exports.
pub use bindings::exports;

/// Bindings to the `wasi:http/proxy` world.
pub use bindings::Proxy;

/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
///
/// This function will add the `async` variant of all interfaces into the
/// `Linker` provided. By `async` this means that this function is only
/// compatible with [`Config::async_support(true)`][async]. For embeddings with
/// async support disabled see [`sync::add_to_linker`] instead.
///
/// [async]: wasmtime::Config::async_support
///
/// # Example
///
/// ```
/// use wasmtime::{Engine, Result, Store, Config};
/// use wasmtime::component::{ResourceTable, Linker};
/// use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
///
/// fn main() -> Result<()> {
///     let mut config = Config::new();
///     config.async_support(true);
///     let engine = Engine::new(&config)?;
///
///     let mut linker = Linker::<MyState>::new(&engine);
///     wasmtime_wasi_http::proxy::add_to_linker(&mut linker)?;
///     // ... add any further functionality to `linker` if desired ...
///
///     let mut store = Store::new(
///         &engine,
///         MyState {
///             ctx: WasiCtxBuilder::new().build(),
///             http_ctx: WasiHttpCtx::new(),
///             table: ResourceTable::new(),
///         },
///     );
///
///     // use `linker.instantiate_async` to instantiate within `store`
///
///     Ok(())
/// }
///
/// struct MyState {
///     ctx: WasiCtx,
///     http_ctx: WasiHttpCtx,
///     table: ResourceTable,
/// }
///
/// impl WasiHttpView for MyState {
///     fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
///     fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// impl WasiView for MyState {
///     fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
///     fn table(&mut self) -> &mut ResourceTable { &mut self.table }
/// }
/// ```
pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
    T: WasiHttpView + wasmtime_wasi::WasiView,
{
    let closure = type_annotate::<T, _>(|t| t);
    wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::io::poll::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::io::streams::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
    wasmtime_wasi::bindings::random::random::add_to_linker_get_host(l, closure)?;

    add_only_http_to_linker(l)
}

// NB: workaround some rustc inference - a future refactoring may make this
// obsolete.
fn type_annotate<T, F>(val: F) -> F
where
    F: Fn(&mut T) -> &mut T,
{
    val
}

#[doc(hidden)]
pub fn add_only_http_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
    T: WasiHttpView + wasmtime_wasi::WasiView + crate::bindings::http::types::Host,
{
    let closure = type_annotate::<T, _>(|t| t);
    crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
    crate::bindings::http::types::add_to_linker_get_host(l, closure)?;

    Ok(())
}

/// Sync implementation of the `wasi:http/proxy` world.
pub mod sync {
    use crate::WasiHttpView;

    mod bindings {
        #![allow(missing_docs)]
        wasmtime::component::bindgen!({
            world: "wasi:http/proxy",
            tracing: true,
            async: false,
            with: {
                "wasi:http": crate::bindings::http, // http is in this crate
                "wasi:io": wasmtime_wasi::bindings::sync, // io is sync
                "wasi": wasmtime_wasi::bindings, // everything else
            },
        });
    }

    /// Raw bindings to the `wasi:http/proxy` exports.
    pub use bindings::exports;

    /// Bindings to the `wasi:http/proxy` world.
    pub use bindings::Proxy;

    /// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
    ///
    /// This function will add the `sync` variant of all interfaces into the
    /// `Linker` provided. For embeddings with async support see [`super::add_to_linker`] instead.
    ///
    /// # Example
    ///
    /// ```
    /// use wasmtime::{Engine, Result, Store, Config};
    /// use wasmtime::component::{ResourceTable, Linker};
    /// use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};
    /// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
    ///
    /// fn main() -> Result<()> {
    ///     let config = Config::default();
    ///     let engine = Engine::new(&config)?;
    ///
    ///     let mut linker = Linker::<MyState>::new(&engine);
    ///     wasmtime_wasi_http::proxy::sync::add_to_linker(&mut linker)?;
    ///     // ... add any further functionality to `linker` if desired ...
    ///
    ///     let mut store = Store::new(
    ///         &engine,
    ///         MyState {
    ///             ctx: WasiCtxBuilder::new().build(),
    ///             http_ctx: WasiHttpCtx::new(),
    ///             table: ResourceTable::new(),
    ///         },
    ///     );
    ///
    ///     // use `linker.instantiate` to instantiate within `store`
    ///
    ///     Ok(())
    /// }
    ///
    /// struct MyState {
    ///     ctx: WasiCtx,
    ///     http_ctx: WasiHttpCtx,
    ///     table: ResourceTable,
    /// }
    ///
    /// impl WasiHttpView for MyState {
    ///     fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
    ///     fn table(&mut self) -> &mut ResourceTable { &mut self.table }
    /// }
    /// impl WasiView for MyState {
    ///     fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
    ///     fn table(&mut self) -> &mut ResourceTable { &mut self.table }
    /// }
    /// ```
    pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
    where
        T: WasiHttpView + wasmtime_wasi::WasiView,
    {
        let closure = super::type_annotate::<T, _>(|t| t);

        wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::sync::io::poll::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::sync::io::streams::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::io::error::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
        wasmtime_wasi::bindings::random::random::add_to_linker_get_host(l, closure)?;

        add_only_http_to_linker(l)?;

        Ok(())
    }

    #[doc(hidden)]
    // TODO: This is temporary solution until the wasmtime_wasi command functions can be removed
    pub fn add_only_http_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
    where
        T: WasiHttpView + wasmtime_wasi::WasiView + crate::bindings::http::types::Host,
    {
        let closure = super::type_annotate::<T, _>(|t| t);

        crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
        crate::bindings::http::types::add_to_linker_get_host(l, closure)?;

        Ok(())
    }
}