Coverage Report

Created: 2023-04-25 07:07

/rust/registry/src/index.crates.io-6f17d22bba15001f/wasmi-0.20.0/src/func/caller.rs
Line
Count
Source (jump to first uncovered line)
1
use super::super::{AsContext, AsContextMut, StoreContext, StoreContextMut};
2
use crate::{Engine, Extern, Instance};
3
4
/// Represents the caller’s context when creating a host function via [`Func::wrap`].
5
///
6
/// [`Func::wrap`]: struct.Func.html#method.wrap
7
pub struct Caller<'a, T> {
8
    pub(crate) store: StoreContextMut<'a, T>,
9
    /// The module instance associated to the call.
10
    /// This is `Some` if the host function was called from a Wasm function
11
    /// since all Wasm function are associated to a module instance.
12
    /// This usually is `None` if the host function was called from the host side.
13
    instance: Option<Instance>,
14
}
15
16
impl<'a, T> Caller<'a, T> {
17
    /// Creates a new [`Caller`] from the given store context and [`Instance`] handle.
18
0
    pub(crate) fn new<C>(ctx: &'a mut C, instance: Option<Instance>) -> Self
19
0
    where
20
0
        C: AsContextMut<UserState = T>,
21
0
    {
22
0
        Self {
23
0
            store: ctx.as_context_mut(),
24
0
            instance,
25
0
        }
26
0
    }
Unexecuted instantiation: <wasmi::func::caller::Caller<()>>::new::<&mut wasmi::store::StoreContextMut<()>>
Unexecuted instantiation: <wasmi::func::caller::Caller<_>>::new::<_>
27
28
    /// Queries the caller for an exported definition identifier by `name`.
29
    ///
30
    /// Returns `None` if there is no associated [`Instance`] of the caller
31
    /// or if the caller does not provide an export under the name `name`.
32
0
    pub fn get_export(&self, name: &str) -> Option<Extern> {
33
0
        self.instance
34
0
            .and_then(|instance| instance.get_export(self, name))
35
0
    }
36
37
    /// Returns a shared reference to the host provided data.
38
0
    pub fn host_data(&self) -> &T {
39
0
        self.store.store.state()
40
0
    }
41
42
    /// Returns an exclusive reference to the host provided data.
43
0
    pub fn host_data_mut(&mut self) -> &mut T {
44
0
        self.store.store.state_mut()
45
0
    }
46
47
    /// Returns a shared reference to the used [`Engine`].
48
0
    pub fn engine(&self) -> &Engine {
49
0
        self.store.store.engine()
50
0
    }
51
}
52
53
impl<T> AsContext for Caller<'_, T> {
54
    type UserState = T;
55
56
    #[inline]
57
0
    fn as_context(&self) -> StoreContext<'_, Self::UserState> {
58
0
        self.store.as_context()
59
0
    }
60
}
61
62
impl<T> AsContextMut for Caller<'_, T> {
63
    #[inline]
64
0
    fn as_context_mut(&mut self) -> StoreContextMut<'_, Self::UserState> {
65
0
        self.store.as_context_mut()
66
0
    }
67
}
68
69
impl<'a, T: AsContextMut> From<&'a mut T> for Caller<'a, T::UserState> {
70
    #[inline]
71
0
    fn from(ctx: &'a mut T) -> Self {
72
0
        Self {
73
0
            store: ctx.as_context_mut(),
74
0
            instance: None,
75
0
        }
76
0
    }
77
}