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
use super::super::{AsContext, AsContextMut, StoreContext, StoreContextMut};
use crate::{Engine, Extern, Instance};
pub struct Caller<'a, T> {
pub(crate) store: StoreContextMut<'a, T>,
instance: Option<Instance>,
}
impl<'a, T> Caller<'a, T> {
pub(crate) fn new<C>(ctx: &'a mut C, instance: Option<&Instance>) -> Self
where
C: AsContextMut<UserState = T>,
{
Self {
store: ctx.as_context_mut(),
instance: instance.copied(),
}
}
pub fn get_export(&self, name: &str) -> Option<Extern> {
self.instance
.and_then(|instance| instance.get_export(self, name))
}
pub fn data(&self) -> &T {
self.store.store.data()
}
pub fn data_mut(&mut self) -> &mut T {
self.store.store.data_mut()
}
pub fn engine(&self) -> &Engine {
self.store.store.engine()
}
}
impl<T> AsContext for Caller<'_, T> {
type UserState = T;
#[inline]
fn as_context(&self) -> StoreContext<'_, Self::UserState> {
self.store.as_context()
}
}
impl<T> AsContextMut for Caller<'_, T> {
#[inline]
fn as_context_mut(&mut self) -> StoreContextMut<'_, Self::UserState> {
self.store.as_context_mut()
}
}
impl<'a, T: AsContextMut> From<&'a mut T> for Caller<'a, T::UserState> {
#[inline]
fn from(ctx: &'a mut T) -> Self {
Self {
store: ctx.as_context_mut(),
instance: None,
}
}
}