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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use std::{
    any::{type_name, Any, TypeId},
    collections::HashMap,
    marker::PhantomData,
    sync::Arc,
};

use anyhow::{bail, Result};

use super::{Data, Linker};

/// A trait for Spin "host components".
///
/// A Spin host component is an interface provided to Spin components that is
/// implemented by the host. This trait is designed to be compatible with
/// [`wasmtime::component::bindgen`]'s generated bindings.
///
/// # Example
///
/// ```ignore
/// use spin_core::my_interface;
///
/// #[derive(Default)]
/// struct MyHostComponent {
///     // ...
/// }
///
/// #[async_trait]
/// impl my_interface::Host for MyHostComponent {
///     // ...
/// }
///
/// impl HostComponent for MyHostComponent {
///     type Data = Self;
///
///     fn add_to_linker<T: Send>(
///         linker: &mut Linker<T>,
///         get: impl Fn(&mut spin_core::Data<T>) -> &mut Self::Data + Send + Sync + Copy + 'static,
///     ) -> anyhow::Result<()> {
///         my_interface::add_to_linker(linker, get)
///     }
///
///     fn build_data(&self) -> Self::Data {
///         Default::default()
///     }
/// }
/// ```
pub trait HostComponent: Send + Sync + 'static {
    /// Host component runtime data.
    type Data: Send + Sized + 'static;

    /// Add this component to the given Linker, using the given runtime state-getting handle.
    ///
    /// This function signature mirrors those generated by [`wasmtime::component::bindgen`].
    fn add_to_linker<T: Send>(
        linker: &mut Linker<T>,
        get: impl Fn(&mut Data<T>) -> &mut Self::Data + Send + Sync + Copy + 'static,
    ) -> Result<()>;

    /// Builds new host component runtime data for [`HostComponentsData`].
    fn build_data(&self) -> Self::Data;
}

impl<HC: HostComponent> HostComponent for Arc<HC> {
    type Data = HC::Data;

    fn add_to_linker<T: Send>(
        linker: &mut Linker<T>,
        get: impl Fn(&mut Data<T>) -> &mut Self::Data + Send + Sync + Copy + 'static,
    ) -> Result<()> {
        HC::add_to_linker(linker, get)
    }

    fn build_data(&self) -> Self::Data {
        (**self).build_data()
    }
}

/// An opaque handle which can be passed to [`HostComponentsData`] to access
/// host component data.
#[derive(Clone, Copy)]
pub struct AnyHostComponentDataHandle(usize);

impl<T: HostComponent> From<HostComponentDataHandle<T>> for AnyHostComponentDataHandle {
    fn from(value: HostComponentDataHandle<T>) -> Self {
        value.inner
    }
}

/// An opaque handle returned by [`crate::EngineBuilder::add_host_component`]
/// which can be passed to [`HostComponentsData`] to access or set associated
/// [`HostComponent::Data`].
pub struct HostComponentDataHandle<HC: HostComponent> {
    inner: AnyHostComponentDataHandle,
    _phantom: PhantomData<fn() -> HC::Data>,
}

impl<HC: HostComponent> HostComponentDataHandle<HC> {
    fn from_any(handle: AnyHostComponentDataHandle) -> Self {
        Self {
            inner: handle,
            _phantom: PhantomData,
        }
    }
}

impl<HC: HostComponent> Clone for HostComponentDataHandle<HC> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<HC: HostComponent> Copy for HostComponentDataHandle<HC> {}

impl<HC: HostComponent> From<HostComponentDataHandle<Arc<HC>>> for HostComponentDataHandle<HC> {
    fn from(value: HostComponentDataHandle<Arc<HC>>) -> Self {
        Self::from_any(value.inner)
    }
}

#[doc(hidden)]
pub trait DynSafeHostComponent {
    fn build_data_box(&self) -> AnyData;
}

impl<T: HostComponent> DynSafeHostComponent for T
where
    T::Data: Any + Send,
{
    fn build_data_box(&self) -> AnyData {
        Box::new(self.build_data())
    }
}

type BoxHostComponent = Box<dyn DynSafeHostComponent + Send + Sync>;

#[derive(Default)]
pub struct HostComponentsBuilder {
    handles: HashMap<TypeId, AnyHostComponentDataHandle>,
    host_components: Vec<BoxHostComponent>,
}

impl HostComponentsBuilder {
    pub fn add_host_component<T: Send, HC: HostComponent>(
        &mut self,
        linker: &mut Linker<T>,
        host_component: HC,
    ) -> Result<HostComponentDataHandle<HC>> {
        let type_id = TypeId::of::<HC>();
        if self.handles.contains_key(&type_id) {
            bail!(
                "already have a host component of type {}",
                type_name::<HC>()
            )
        }

        let handle = AnyHostComponentDataHandle(self.host_components.len());
        self.handles.insert(type_id, handle);

        self.host_components.push(Box::new(host_component));
        HC::add_to_linker(linker, move |data| {
            data.host_components_data
                .get_or_insert_any(handle)
                .downcast_mut()
                .unwrap()
        })?;
        Ok(HostComponentDataHandle::<HC> {
            inner: handle,
            _phantom: PhantomData,
        })
    }

    pub fn build(self) -> HostComponents {
        HostComponents {
            handles: self.handles,
            host_components: Arc::new(self.host_components),
        }
    }
}

pub struct HostComponents {
    handles: HashMap<TypeId, AnyHostComponentDataHandle>,
    host_components: Arc<Vec<BoxHostComponent>>,
}

impl HostComponents {
    pub fn builder() -> HostComponentsBuilder {
        Default::default()
    }

    pub fn new_data(&self) -> HostComponentsData {
        // Fill with `None`
        let data = std::iter::repeat_with(Default::default)
            .take(self.host_components.len())
            .collect();
        HostComponentsData {
            data,
            host_components: self.host_components.clone(),
        }
    }

    pub fn find_handle<HC: HostComponent>(&self) -> Option<HostComponentDataHandle<HC>> {
        self.handles
            .get(&TypeId::of::<HC>())
            .map(|handle| HostComponentDataHandle::from_any(*handle))
    }
}

type AnyData = Box<dyn Any + Send>;

/// Holds a heterogenous set of [`HostComponent::Data`]s.
pub struct HostComponentsData {
    data: Vec<Option<AnyData>>,
    host_components: Arc<Vec<BoxHostComponent>>,
}

impl HostComponentsData {
    /// Sets the [`HostComponent::Data`] for the given `handle`.
    pub fn set<HC: HostComponent>(&mut self, handle: HostComponentDataHandle<HC>, data: HC::Data) {
        self.data[handle.inner.0] = Some(Box::new(data));
    }

    /// Retrieves a mutable reference to [`HostComponent::Data`] for the given `handle`.
    ///
    /// If unset, the data will be initialized with [`HostComponent::build_data`].
    ///
    /// # Panics
    ///
    /// If the given handle was not obtained from the same [`HostComponentsBuilder`] that
    /// was the source of this [`HostComponentsData`], this function may panic.
    pub fn get_or_insert<HC: HostComponent>(
        &mut self,
        handle: HostComponentDataHandle<HC>,
    ) -> &mut HC::Data {
        let data = self.get_or_insert_any(handle.inner);
        data.downcast_mut().unwrap()
    }

    /// Retrieves a mutable reference to [`HostComponent::Data`] for the given `handle`.
    ///
    /// If unset, the data will be initialized with [`HostComponent::build_data`].
    ///
    /// # Panics
    ///
    /// If the given handle was not obtained from the same [`HostComponentsBuilder`] that
    /// was the source of this [`HostComponentsData`], this function may panic.
    pub fn get_or_insert_any(&mut self, handle: AnyHostComponentDataHandle) -> &mut AnyData {
        let idx = handle.0;
        self.data[idx].get_or_insert_with(|| self.host_components[idx].build_data_box())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct TestHC;

    impl HostComponent for TestHC {
        type Data = u8;

        fn add_to_linker<T: Send>(
            _linker: &mut Linker<T>,
            _get: impl Fn(&mut Data<T>) -> &mut Self::Data + Send + Sync + Copy + 'static,
        ) -> Result<()> {
            Ok(())
        }

        fn build_data(&self) -> Self::Data {
            0
        }
    }

    #[test]
    fn host_components_data() {
        let engine = wasmtime::Engine::default();
        let mut linker: crate::Linker<()> = crate::Linker::new(&engine);

        let mut builder = HostComponents::builder();
        let handle1 = builder
            .add_host_component(&mut linker, Arc::new(TestHC))
            .unwrap();
        let handle2 = builder.add_host_component(&mut linker, TestHC).unwrap();
        let host_components = builder.build();
        let mut hc_data = host_components.new_data();

        assert_eq!(hc_data.get_or_insert(handle1), &0);

        hc_data.set(handle2, 1);
        assert_eq!(hc_data.get_or_insert(handle2), &1);
    }

    #[test]
    fn find_handle() {
        let engine = wasmtime::Engine::default();
        let mut linker: crate::Linker<()> = crate::Linker::new(&engine);

        let mut builder = HostComponents::builder();
        builder.add_host_component(&mut linker, TestHC).unwrap();
        let host_components = builder.build();
        let handle = host_components.find_handle::<TestHC>().unwrap();
        let mut hc_data = host_components.new_data();
        assert_eq!(hc_data.get_or_insert(handle), &0);
    }
}