Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/multiversion.rs
1692 views
1
/// Auto-generated bindings for a pre-instantiated version of a
2
/// component which implements the world `foo`.
3
///
4
/// This structure is created through [`FooPre::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 [`Foo`] as well.
9
pub struct FooPre<T: 'static> {
10
instance_pre: wasmtime::component::InstancePre<T>,
11
indices: FooIndices,
12
}
13
impl<T: 'static> Clone for FooPre<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> FooPre<_T> {
22
/// Creates a new copy of `FooPre` 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 = FooIndices::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 [`Foo`] 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<Foo> {
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> FooPre<_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<Foo> {
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
/// `foo`.
68
///
69
/// This is an implementation detail of [`FooPre`] and can
70
/// be constructed if needed as well.
71
///
72
/// For more information see [`Foo`] as well.
73
#[derive(Clone)]
74
pub struct FooIndices {
75
interface0: exports::my::dep0_1_0::a::GuestIndices,
76
interface1: exports::my::dep0_2_0::a::GuestIndices,
77
}
78
/// Auto-generated bindings for an instance a component which
79
/// implements the world `foo`.
80
///
81
/// This structure can be created through a number of means
82
/// depending on your requirements and what you have on hand:
83
///
84
/// * The most convenient way is to use
85
/// [`Foo::instantiate`] which only needs a
86
/// [`Store`], [`Component`], and [`Linker`].
87
///
88
/// * Alternatively you can create a [`FooPre`] ahead of
89
/// time with a [`Component`] to front-load string lookups
90
/// of exports once instead of per-instantiation. This
91
/// method then uses [`FooPre::instantiate`] to
92
/// create a [`Foo`].
93
///
94
/// * If you've instantiated the instance yourself already
95
/// then you can use [`Foo::new`].
96
///
97
/// These methods are all equivalent to one another and move
98
/// around the tradeoff of what work is performed when.
99
///
100
/// [`Store`]: wasmtime::Store
101
/// [`Component`]: wasmtime::component::Component
102
/// [`Linker`]: wasmtime::component::Linker
103
pub struct Foo {
104
interface0: exports::my::dep0_1_0::a::Guest,
105
interface1: exports::my::dep0_2_0::a::Guest,
106
}
107
const _: () = {
108
#[allow(unused_imports)]
109
use wasmtime::component::__internal::anyhow;
110
impl FooIndices {
111
/// Creates a new copy of `FooIndices` bindings which can then
112
/// be used to instantiate into a particular store.
113
///
114
/// This method may fail if the component does not have the
115
/// required exports.
116
pub fn new<_T>(
117
_instance_pre: &wasmtime::component::InstancePre<_T>,
118
) -> wasmtime::Result<Self> {
119
let _component = _instance_pre.component();
120
let _instance_type = _instance_pre.instance_type();
121
let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?;
122
let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?;
123
Ok(FooIndices {
124
interface0,
125
interface1,
126
})
127
}
128
/// Uses the indices stored in `self` to load an instance
129
/// of [`Foo`] from the instance provided.
130
///
131
/// Note that at this time this method will additionally
132
/// perform type-checks of all exports.
133
pub fn load(
134
&self,
135
mut store: impl wasmtime::AsContextMut,
136
instance: &wasmtime::component::Instance,
137
) -> wasmtime::Result<Foo> {
138
let _ = &mut store;
139
let _instance = instance;
140
let interface0 = self.interface0.load(&mut store, &_instance)?;
141
let interface1 = self.interface1.load(&mut store, &_instance)?;
142
Ok(Foo { interface0, interface1 })
143
}
144
}
145
impl Foo {
146
/// Convenience wrapper around [`FooPre::new`] and
147
/// [`FooPre::instantiate`].
148
pub fn instantiate<_T>(
149
store: impl wasmtime::AsContextMut<Data = _T>,
150
component: &wasmtime::component::Component,
151
linker: &wasmtime::component::Linker<_T>,
152
) -> wasmtime::Result<Foo> {
153
let pre = linker.instantiate_pre(component)?;
154
FooPre::new(pre)?.instantiate(store)
155
}
156
/// Convenience wrapper around [`FooIndices::new`] and
157
/// [`FooIndices::load`].
158
pub fn new(
159
mut store: impl wasmtime::AsContextMut,
160
instance: &wasmtime::component::Instance,
161
) -> wasmtime::Result<Foo> {
162
let indices = FooIndices::new(&instance.instance_pre(&store))?;
163
indices.load(&mut store, instance)
164
}
165
/// Convenience wrapper around [`FooPre::new`] and
166
/// [`FooPre::instantiate_async`].
167
pub async fn instantiate_async<_T>(
168
store: impl wasmtime::AsContextMut<Data = _T>,
169
component: &wasmtime::component::Component,
170
linker: &wasmtime::component::Linker<_T>,
171
) -> wasmtime::Result<Foo>
172
where
173
_T: Send,
174
{
175
let pre = linker.instantiate_pre(component)?;
176
FooPre::new(pre)?.instantiate_async(store).await
177
}
178
pub fn add_to_linker<T, D>(
179
linker: &mut wasmtime::component::Linker<T>,
180
host_getter: fn(&mut T) -> D::Data<'_>,
181
) -> wasmtime::Result<()>
182
where
183
D: my::dep0_1_0::a::HostWithStore + my::dep0_2_0::a::HostWithStore,
184
for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host,
185
T: 'static,
186
{
187
my::dep0_1_0::a::add_to_linker::<T, D>(linker, host_getter)?;
188
my::dep0_2_0::a::add_to_linker::<T, D>(linker, host_getter)?;
189
Ok(())
190
}
191
pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest {
192
&self.interface0
193
}
194
pub fn my_dep0_2_0_a(&self) -> &exports::my::dep0_2_0::a::Guest {
195
&self.interface1
196
}
197
}
198
};
199
pub mod my {
200
pub mod dep0_1_0 {
201
#[allow(clippy::all)]
202
pub mod a {
203
#[allow(unused_imports)]
204
use wasmtime::component::__internal::{anyhow, Box};
205
pub trait HostWithStore: wasmtime::component::HasData {}
206
impl<_T: ?Sized> HostWithStore for _T
207
where
208
_T: wasmtime::component::HasData,
209
{}
210
pub trait Host {
211
fn x(&mut self) -> ();
212
}
213
impl<_T: Host + ?Sized> Host for &mut _T {
214
fn x(&mut self) -> () {
215
Host::x(*self)
216
}
217
}
218
pub fn add_to_linker<T, D>(
219
linker: &mut wasmtime::component::Linker<T>,
220
host_getter: fn(&mut T) -> D::Data<'_>,
221
) -> wasmtime::Result<()>
222
where
223
D: HostWithStore,
224
for<'a> D::Data<'a>: Host,
225
T: 'static,
226
{
227
let mut inst = linker.instance("my:dep/[email protected]")?;
228
inst.func_wrap(
229
"x",
230
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
231
let host = &mut host_getter(caller.data_mut());
232
let r = Host::x(host);
233
Ok(r)
234
},
235
)?;
236
Ok(())
237
}
238
}
239
}
240
pub mod dep0_2_0 {
241
#[allow(clippy::all)]
242
pub mod a {
243
#[allow(unused_imports)]
244
use wasmtime::component::__internal::{anyhow, Box};
245
pub trait HostWithStore: wasmtime::component::HasData {}
246
impl<_T: ?Sized> HostWithStore for _T
247
where
248
_T: wasmtime::component::HasData,
249
{}
250
pub trait Host {
251
fn x(&mut self) -> ();
252
}
253
impl<_T: Host + ?Sized> Host for &mut _T {
254
fn x(&mut self) -> () {
255
Host::x(*self)
256
}
257
}
258
pub fn add_to_linker<T, D>(
259
linker: &mut wasmtime::component::Linker<T>,
260
host_getter: fn(&mut T) -> D::Data<'_>,
261
) -> wasmtime::Result<()>
262
where
263
D: HostWithStore,
264
for<'a> D::Data<'a>: Host,
265
T: 'static,
266
{
267
let mut inst = linker.instance("my:dep/[email protected]")?;
268
inst.func_wrap(
269
"x",
270
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
271
let host = &mut host_getter(caller.data_mut());
272
let r = Host::x(host);
273
Ok(r)
274
},
275
)?;
276
Ok(())
277
}
278
}
279
}
280
}
281
pub mod exports {
282
pub mod my {
283
pub mod dep0_1_0 {
284
#[allow(clippy::all)]
285
pub mod a {
286
#[allow(unused_imports)]
287
use wasmtime::component::__internal::{anyhow, Box};
288
pub struct Guest {
289
x: wasmtime::component::Func,
290
}
291
#[derive(Clone)]
292
pub struct GuestIndices {
293
x: wasmtime::component::ComponentExportIndex,
294
}
295
impl GuestIndices {
296
/// Constructor for [`GuestIndices`] which takes a
297
/// [`Component`](wasmtime::component::Component) as input and can be executed
298
/// before instantiation.
299
///
300
/// This constructor can be used to front-load string lookups to find exports
301
/// within a component.
302
pub fn new<_T>(
303
_instance_pre: &wasmtime::component::InstancePre<_T>,
304
) -> wasmtime::Result<GuestIndices> {
305
let instance = _instance_pre
306
.component()
307
.get_export_index(None, "my:dep/[email protected]")
308
.ok_or_else(|| {
309
anyhow::anyhow!(
310
"no exported instance named `my:dep/[email protected]`"
311
)
312
})?;
313
let mut lookup = move |name| {
314
_instance_pre
315
.component()
316
.get_export_index(Some(&instance), name)
317
.ok_or_else(|| {
318
anyhow::anyhow!(
319
"instance export `my:dep/[email protected]` does \
320
not have export `{name}`"
321
)
322
})
323
};
324
let _ = &mut lookup;
325
let x = lookup("x")?;
326
Ok(GuestIndices { x })
327
}
328
pub fn load(
329
&self,
330
mut store: impl wasmtime::AsContextMut,
331
instance: &wasmtime::component::Instance,
332
) -> wasmtime::Result<Guest> {
333
let _instance = instance;
334
let _instance_pre = _instance.instance_pre(&store);
335
let _instance_type = _instance_pre.instance_type();
336
let mut store = store.as_context_mut();
337
let _ = &mut store;
338
let x = *_instance
339
.get_typed_func::<(), ()>(&mut store, &self.x)?
340
.func();
341
Ok(Guest { x })
342
}
343
}
344
impl Guest {
345
pub fn call_x<S: wasmtime::AsContextMut>(
346
&self,
347
mut store: S,
348
) -> wasmtime::Result<()> {
349
let callee = unsafe {
350
wasmtime::component::TypedFunc::<
351
(),
352
(),
353
>::new_unchecked(self.x)
354
};
355
let () = callee.call(store.as_context_mut(), ())?;
356
callee.post_return(store.as_context_mut())?;
357
Ok(())
358
}
359
}
360
}
361
}
362
pub mod dep0_2_0 {
363
#[allow(clippy::all)]
364
pub mod a {
365
#[allow(unused_imports)]
366
use wasmtime::component::__internal::{anyhow, Box};
367
pub struct Guest {
368
x: wasmtime::component::Func,
369
}
370
#[derive(Clone)]
371
pub struct GuestIndices {
372
x: wasmtime::component::ComponentExportIndex,
373
}
374
impl GuestIndices {
375
/// Constructor for [`GuestIndices`] which takes a
376
/// [`Component`](wasmtime::component::Component) as input and can be executed
377
/// before instantiation.
378
///
379
/// This constructor can be used to front-load string lookups to find exports
380
/// within a component.
381
pub fn new<_T>(
382
_instance_pre: &wasmtime::component::InstancePre<_T>,
383
) -> wasmtime::Result<GuestIndices> {
384
let instance = _instance_pre
385
.component()
386
.get_export_index(None, "my:dep/[email protected]")
387
.ok_or_else(|| {
388
anyhow::anyhow!(
389
"no exported instance named `my:dep/[email protected]`"
390
)
391
})?;
392
let mut lookup = move |name| {
393
_instance_pre
394
.component()
395
.get_export_index(Some(&instance), name)
396
.ok_or_else(|| {
397
anyhow::anyhow!(
398
"instance export `my:dep/[email protected]` does \
399
not have export `{name}`"
400
)
401
})
402
};
403
let _ = &mut lookup;
404
let x = lookup("x")?;
405
Ok(GuestIndices { x })
406
}
407
pub fn load(
408
&self,
409
mut store: impl wasmtime::AsContextMut,
410
instance: &wasmtime::component::Instance,
411
) -> wasmtime::Result<Guest> {
412
let _instance = instance;
413
let _instance_pre = _instance.instance_pre(&store);
414
let _instance_type = _instance_pre.instance_type();
415
let mut store = store.as_context_mut();
416
let _ = &mut store;
417
let x = *_instance
418
.get_typed_func::<(), ()>(&mut store, &self.x)?
419
.func();
420
Ok(Guest { x })
421
}
422
}
423
impl Guest {
424
pub fn call_x<S: wasmtime::AsContextMut>(
425
&self,
426
mut store: S,
427
) -> wasmtime::Result<()> {
428
let callee = unsafe {
429
wasmtime::component::TypedFunc::<
430
(),
431
(),
432
>::new_unchecked(self.x)
433
};
434
let () = callee.call(store.as_context_mut(), ())?;
435
callee.post_return(store.as_context_mut())?;
436
Ok(())
437
}
438
}
439
}
440
}
441
}
442
}
443
444