Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/host-world_concurrent.rs
1692 views
1
/// Auto-generated bindings for a pre-instantiated version of a
2
/// component which implements the world `host`.
3
///
4
/// This structure is created through [`Host_Pre::new`] which
5
/// takes a [`InstancePre`](wasmtime::component::InstancePre) that
6
/// has been created through a [`Linker`](wasmtime::component::Linker).
7
///
8
/// For more information see [`Host_`] as well.
9
pub struct Host_Pre<T: 'static> {
10
instance_pre: wasmtime::component::InstancePre<T>,
11
indices: Host_Indices,
12
}
13
impl<T: 'static> Clone for Host_Pre<T> {
14
fn clone(&self) -> Self {
15
Self {
16
instance_pre: self.instance_pre.clone(),
17
indices: self.indices.clone(),
18
}
19
}
20
}
21
impl<_T: 'static> Host_Pre<_T> {
22
/// Creates a new copy of `Host_Pre` bindings which can then
23
/// be used to instantiate into a particular store.
24
///
25
/// This method may fail if the component behind `instance_pre`
26
/// does not have the required exports.
27
pub fn new(
28
instance_pre: wasmtime::component::InstancePre<_T>,
29
) -> wasmtime::Result<Self> {
30
let indices = Host_Indices::new(&instance_pre)?;
31
Ok(Self { instance_pre, indices })
32
}
33
pub fn engine(&self) -> &wasmtime::Engine {
34
self.instance_pre.engine()
35
}
36
pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
37
&self.instance_pre
38
}
39
/// Instantiates a new instance of [`Host_`] within the
40
/// `store` provided.
41
///
42
/// This function will use `self` as the pre-instantiated
43
/// instance to perform instantiation. Afterwards the preloaded
44
/// indices in `self` are used to lookup all exports on the
45
/// resulting instance.
46
pub fn instantiate(
47
&self,
48
mut store: impl wasmtime::AsContextMut<Data = _T>,
49
) -> wasmtime::Result<Host_> {
50
let mut store = store.as_context_mut();
51
let instance = self.instance_pre.instantiate(&mut store)?;
52
self.indices.load(&mut store, &instance)
53
}
54
}
55
impl<_T: Send + 'static> Host_Pre<_T> {
56
/// Same as [`Self::instantiate`], except with `async`.
57
pub async fn instantiate_async(
58
&self,
59
mut store: impl wasmtime::AsContextMut<Data = _T>,
60
) -> wasmtime::Result<Host_> {
61
let mut store = store.as_context_mut();
62
let instance = self.instance_pre.instantiate_async(&mut store).await?;
63
self.indices.load(&mut store, &instance)
64
}
65
}
66
/// Auto-generated bindings for index of the exports of
67
/// `host`.
68
///
69
/// This is an implementation detail of [`Host_Pre`] and can
70
/// be constructed if needed as well.
71
///
72
/// For more information see [`Host_`] as well.
73
#[derive(Clone)]
74
pub struct Host_Indices {}
75
/// Auto-generated bindings for an instance a component which
76
/// implements the world `host`.
77
///
78
/// This structure can be created through a number of means
79
/// depending on your requirements and what you have on hand:
80
///
81
/// * The most convenient way is to use
82
/// [`Host_::instantiate`] which only needs a
83
/// [`Store`], [`Component`], and [`Linker`].
84
///
85
/// * Alternatively you can create a [`Host_Pre`] ahead of
86
/// time with a [`Component`] to front-load string lookups
87
/// of exports once instead of per-instantiation. This
88
/// method then uses [`Host_Pre::instantiate`] to
89
/// create a [`Host_`].
90
///
91
/// * If you've instantiated the instance yourself already
92
/// then you can use [`Host_::new`].
93
///
94
/// These methods are all equivalent to one another and move
95
/// around the tradeoff of what work is performed when.
96
///
97
/// [`Store`]: wasmtime::Store
98
/// [`Component`]: wasmtime::component::Component
99
/// [`Linker`]: wasmtime::component::Linker
100
pub struct Host_ {}
101
pub trait Host_ImportsWithStore: wasmtime::component::HasData + Send {
102
fn foo<T>(
103
accessor: &wasmtime::component::Accessor<T, Self>,
104
) -> impl ::core::future::Future<Output = ()> + Send;
105
}
106
pub trait Host_Imports: Send {}
107
impl<_T: Host_Imports + ?Sized + Send> Host_Imports for &mut _T {}
108
const _: () = {
109
#[allow(unused_imports)]
110
use wasmtime::component::__internal::anyhow;
111
impl Host_Indices {
112
/// Creates a new copy of `Host_Indices` bindings which can then
113
/// be used to instantiate into a particular store.
114
///
115
/// This method may fail if the component does not have the
116
/// required exports.
117
pub fn new<_T>(
118
_instance_pre: &wasmtime::component::InstancePre<_T>,
119
) -> wasmtime::Result<Self> {
120
let _component = _instance_pre.component();
121
let _instance_type = _instance_pre.instance_type();
122
Ok(Host_Indices {})
123
}
124
/// Uses the indices stored in `self` to load an instance
125
/// of [`Host_`] from the instance provided.
126
///
127
/// Note that at this time this method will additionally
128
/// perform type-checks of all exports.
129
pub fn load(
130
&self,
131
mut store: impl wasmtime::AsContextMut,
132
instance: &wasmtime::component::Instance,
133
) -> wasmtime::Result<Host_> {
134
let _ = &mut store;
135
let _instance = instance;
136
Ok(Host_ {})
137
}
138
}
139
impl Host_ {
140
/// Convenience wrapper around [`Host_Pre::new`] and
141
/// [`Host_Pre::instantiate`].
142
pub fn instantiate<_T>(
143
store: impl wasmtime::AsContextMut<Data = _T>,
144
component: &wasmtime::component::Component,
145
linker: &wasmtime::component::Linker<_T>,
146
) -> wasmtime::Result<Host_> {
147
let pre = linker.instantiate_pre(component)?;
148
Host_Pre::new(pre)?.instantiate(store)
149
}
150
/// Convenience wrapper around [`Host_Indices::new`] and
151
/// [`Host_Indices::load`].
152
pub fn new(
153
mut store: impl wasmtime::AsContextMut,
154
instance: &wasmtime::component::Instance,
155
) -> wasmtime::Result<Host_> {
156
let indices = Host_Indices::new(&instance.instance_pre(&store))?;
157
indices.load(&mut store, instance)
158
}
159
/// Convenience wrapper around [`Host_Pre::new`] and
160
/// [`Host_Pre::instantiate_async`].
161
pub async fn instantiate_async<_T>(
162
store: impl wasmtime::AsContextMut<Data = _T>,
163
component: &wasmtime::component::Component,
164
linker: &wasmtime::component::Linker<_T>,
165
) -> wasmtime::Result<Host_>
166
where
167
_T: Send,
168
{
169
let pre = linker.instantiate_pre(component)?;
170
Host_Pre::new(pre)?.instantiate_async(store).await
171
}
172
pub fn add_to_linker_imports<T, D>(
173
linker: &mut wasmtime::component::Linker<T>,
174
host_getter: fn(&mut T) -> D::Data<'_>,
175
) -> wasmtime::Result<()>
176
where
177
D: Host_ImportsWithStore,
178
for<'a> D::Data<'a>: Host_Imports,
179
T: 'static + Send,
180
{
181
let mut linker = linker.root();
182
linker
183
.func_wrap_concurrent(
184
"foo",
185
move |caller: &wasmtime::component::Accessor<T>, (): ()| {
186
wasmtime::component::__internal::Box::pin(async move {
187
let host = &caller.with_getter(host_getter);
188
let r = <D as Host_ImportsWithStore>::foo(host).await;
189
Ok(r)
190
})
191
},
192
)?;
193
Ok(())
194
}
195
pub fn add_to_linker<T, D>(
196
linker: &mut wasmtime::component::Linker<T>,
197
host_getter: fn(&mut T) -> D::Data<'_>,
198
) -> wasmtime::Result<()>
199
where
200
D: Host_ImportsWithStore + Send,
201
for<'a> D::Data<'a>: Host_Imports + Send,
202
T: 'static + Send,
203
{
204
Self::add_to_linker_imports::<T, D>(linker, host_getter)?;
205
Ok(())
206
}
207
}
208
};
209
210