Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/many-arguments_async.rs
1692 views
1
/// Auto-generated bindings for a pre-instantiated version of a
2
/// component which implements the world `the-world`.
3
///
4
/// This structure is created through [`TheWorldPre::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 [`TheWorld`] as well.
9
pub struct TheWorldPre<T: 'static> {
10
instance_pre: wasmtime::component::InstancePre<T>,
11
indices: TheWorldIndices,
12
}
13
impl<T: 'static> Clone for TheWorldPre<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> TheWorldPre<_T> {
22
/// Creates a new copy of `TheWorldPre` 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 = TheWorldIndices::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 [`TheWorld`] 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<TheWorld> {
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> TheWorldPre<_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<TheWorld> {
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
/// `the-world`.
68
///
69
/// This is an implementation detail of [`TheWorldPre`] and can
70
/// be constructed if needed as well.
71
///
72
/// For more information see [`TheWorld`] as well.
73
#[derive(Clone)]
74
pub struct TheWorldIndices {
75
interface0: exports::foo::foo::manyarg::GuestIndices,
76
}
77
/// Auto-generated bindings for an instance a component which
78
/// implements the world `the-world`.
79
///
80
/// This structure can be created through a number of means
81
/// depending on your requirements and what you have on hand:
82
///
83
/// * The most convenient way is to use
84
/// [`TheWorld::instantiate`] which only needs a
85
/// [`Store`], [`Component`], and [`Linker`].
86
///
87
/// * Alternatively you can create a [`TheWorldPre`] ahead of
88
/// time with a [`Component`] to front-load string lookups
89
/// of exports once instead of per-instantiation. This
90
/// method then uses [`TheWorldPre::instantiate`] to
91
/// create a [`TheWorld`].
92
///
93
/// * If you've instantiated the instance yourself already
94
/// then you can use [`TheWorld::new`].
95
///
96
/// These methods are all equivalent to one another and move
97
/// around the tradeoff of what work is performed when.
98
///
99
/// [`Store`]: wasmtime::Store
100
/// [`Component`]: wasmtime::component::Component
101
/// [`Linker`]: wasmtime::component::Linker
102
pub struct TheWorld {
103
interface0: exports::foo::foo::manyarg::Guest,
104
}
105
const _: () = {
106
#[allow(unused_imports)]
107
use wasmtime::component::__internal::anyhow;
108
impl TheWorldIndices {
109
/// Creates a new copy of `TheWorldIndices` bindings which can then
110
/// be used to instantiate into a particular store.
111
///
112
/// This method may fail if the component does not have the
113
/// required exports.
114
pub fn new<_T>(
115
_instance_pre: &wasmtime::component::InstancePre<_T>,
116
) -> wasmtime::Result<Self> {
117
let _component = _instance_pre.component();
118
let _instance_type = _instance_pre.instance_type();
119
let interface0 = exports::foo::foo::manyarg::GuestIndices::new(
120
_instance_pre,
121
)?;
122
Ok(TheWorldIndices { interface0 })
123
}
124
/// Uses the indices stored in `self` to load an instance
125
/// of [`TheWorld`] 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<TheWorld> {
134
let _ = &mut store;
135
let _instance = instance;
136
let interface0 = self.interface0.load(&mut store, &_instance)?;
137
Ok(TheWorld { interface0 })
138
}
139
}
140
impl TheWorld {
141
/// Convenience wrapper around [`TheWorldPre::new`] and
142
/// [`TheWorldPre::instantiate`].
143
pub fn instantiate<_T>(
144
store: impl wasmtime::AsContextMut<Data = _T>,
145
component: &wasmtime::component::Component,
146
linker: &wasmtime::component::Linker<_T>,
147
) -> wasmtime::Result<TheWorld> {
148
let pre = linker.instantiate_pre(component)?;
149
TheWorldPre::new(pre)?.instantiate(store)
150
}
151
/// Convenience wrapper around [`TheWorldIndices::new`] and
152
/// [`TheWorldIndices::load`].
153
pub fn new(
154
mut store: impl wasmtime::AsContextMut,
155
instance: &wasmtime::component::Instance,
156
) -> wasmtime::Result<TheWorld> {
157
let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;
158
indices.load(&mut store, instance)
159
}
160
/// Convenience wrapper around [`TheWorldPre::new`] and
161
/// [`TheWorldPre::instantiate_async`].
162
pub async fn instantiate_async<_T>(
163
store: impl wasmtime::AsContextMut<Data = _T>,
164
component: &wasmtime::component::Component,
165
linker: &wasmtime::component::Linker<_T>,
166
) -> wasmtime::Result<TheWorld>
167
where
168
_T: Send,
169
{
170
let pre = linker.instantiate_pre(component)?;
171
TheWorldPre::new(pre)?.instantiate_async(store).await
172
}
173
pub fn add_to_linker<T, D>(
174
linker: &mut wasmtime::component::Linker<T>,
175
host_getter: fn(&mut T) -> D::Data<'_>,
176
) -> wasmtime::Result<()>
177
where
178
D: foo::foo::manyarg::HostWithStore + Send,
179
for<'a> D::Data<'a>: foo::foo::manyarg::Host + Send,
180
T: 'static + Send,
181
{
182
foo::foo::manyarg::add_to_linker::<T, D>(linker, host_getter)?;
183
Ok(())
184
}
185
pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest {
186
&self.interface0
187
}
188
}
189
};
190
pub mod foo {
191
pub mod foo {
192
#[allow(clippy::all)]
193
pub mod manyarg {
194
#[allow(unused_imports)]
195
use wasmtime::component::__internal::{anyhow, Box};
196
#[derive(wasmtime::component::ComponentType)]
197
#[derive(wasmtime::component::Lift)]
198
#[derive(wasmtime::component::Lower)]
199
#[component(record)]
200
#[derive(Clone)]
201
pub struct BigStruct {
202
#[component(name = "a1")]
203
pub a1: wasmtime::component::__internal::String,
204
#[component(name = "a2")]
205
pub a2: wasmtime::component::__internal::String,
206
#[component(name = "a3")]
207
pub a3: wasmtime::component::__internal::String,
208
#[component(name = "a4")]
209
pub a4: wasmtime::component::__internal::String,
210
#[component(name = "a5")]
211
pub a5: wasmtime::component::__internal::String,
212
#[component(name = "a6")]
213
pub a6: wasmtime::component::__internal::String,
214
#[component(name = "a7")]
215
pub a7: wasmtime::component::__internal::String,
216
#[component(name = "a8")]
217
pub a8: wasmtime::component::__internal::String,
218
#[component(name = "a9")]
219
pub a9: wasmtime::component::__internal::String,
220
#[component(name = "a10")]
221
pub a10: wasmtime::component::__internal::String,
222
#[component(name = "a11")]
223
pub a11: wasmtime::component::__internal::String,
224
#[component(name = "a12")]
225
pub a12: wasmtime::component::__internal::String,
226
#[component(name = "a13")]
227
pub a13: wasmtime::component::__internal::String,
228
#[component(name = "a14")]
229
pub a14: wasmtime::component::__internal::String,
230
#[component(name = "a15")]
231
pub a15: wasmtime::component::__internal::String,
232
#[component(name = "a16")]
233
pub a16: wasmtime::component::__internal::String,
234
#[component(name = "a17")]
235
pub a17: wasmtime::component::__internal::String,
236
#[component(name = "a18")]
237
pub a18: wasmtime::component::__internal::String,
238
#[component(name = "a19")]
239
pub a19: wasmtime::component::__internal::String,
240
#[component(name = "a20")]
241
pub a20: wasmtime::component::__internal::String,
242
}
243
impl core::fmt::Debug for BigStruct {
244
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
245
f.debug_struct("BigStruct")
246
.field("a1", &self.a1)
247
.field("a2", &self.a2)
248
.field("a3", &self.a3)
249
.field("a4", &self.a4)
250
.field("a5", &self.a5)
251
.field("a6", &self.a6)
252
.field("a7", &self.a7)
253
.field("a8", &self.a8)
254
.field("a9", &self.a9)
255
.field("a10", &self.a10)
256
.field("a11", &self.a11)
257
.field("a12", &self.a12)
258
.field("a13", &self.a13)
259
.field("a14", &self.a14)
260
.field("a15", &self.a15)
261
.field("a16", &self.a16)
262
.field("a17", &self.a17)
263
.field("a18", &self.a18)
264
.field("a19", &self.a19)
265
.field("a20", &self.a20)
266
.finish()
267
}
268
}
269
const _: () = {
270
assert!(
271
160 == < BigStruct as wasmtime::component::ComponentType >::SIZE32
272
);
273
assert!(
274
4 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32
275
);
276
};
277
pub trait HostWithStore: wasmtime::component::HasData + Send {}
278
impl<_T: ?Sized> HostWithStore for _T
279
where
280
_T: wasmtime::component::HasData + Send,
281
{}
282
pub trait Host: Send {
283
fn many_args(
284
&mut self,
285
a1: u64,
286
a2: u64,
287
a3: u64,
288
a4: u64,
289
a5: u64,
290
a6: u64,
291
a7: u64,
292
a8: u64,
293
a9: u64,
294
a10: u64,
295
a11: u64,
296
a12: u64,
297
a13: u64,
298
a14: u64,
299
a15: u64,
300
a16: u64,
301
) -> impl ::core::future::Future<Output = ()> + Send;
302
fn big_argument(
303
&mut self,
304
x: BigStruct,
305
) -> impl ::core::future::Future<Output = ()> + Send;
306
}
307
impl<_T: Host + ?Sized + Send> Host for &mut _T {
308
fn many_args(
309
&mut self,
310
a1: u64,
311
a2: u64,
312
a3: u64,
313
a4: u64,
314
a5: u64,
315
a6: u64,
316
a7: u64,
317
a8: u64,
318
a9: u64,
319
a10: u64,
320
a11: u64,
321
a12: u64,
322
a13: u64,
323
a14: u64,
324
a15: u64,
325
a16: u64,
326
) -> impl ::core::future::Future<Output = ()> + Send {
327
async move {
328
Host::many_args(
329
*self,
330
a1,
331
a2,
332
a3,
333
a4,
334
a5,
335
a6,
336
a7,
337
a8,
338
a9,
339
a10,
340
a11,
341
a12,
342
a13,
343
a14,
344
a15,
345
a16,
346
)
347
.await
348
}
349
}
350
fn big_argument(
351
&mut self,
352
x: BigStruct,
353
) -> impl ::core::future::Future<Output = ()> + Send {
354
async move { Host::big_argument(*self, x).await }
355
}
356
}
357
pub fn add_to_linker<T, D>(
358
linker: &mut wasmtime::component::Linker<T>,
359
host_getter: fn(&mut T) -> D::Data<'_>,
360
) -> wasmtime::Result<()>
361
where
362
D: HostWithStore,
363
for<'a> D::Data<'a>: Host,
364
T: 'static + Send,
365
{
366
let mut inst = linker.instance("foo:foo/manyarg")?;
367
inst.func_wrap_async(
368
"many-args",
369
move |
370
mut caller: wasmtime::StoreContextMut<'_, T>,
371
(
372
arg0,
373
arg1,
374
arg2,
375
arg3,
376
arg4,
377
arg5,
378
arg6,
379
arg7,
380
arg8,
381
arg9,
382
arg10,
383
arg11,
384
arg12,
385
arg13,
386
arg14,
387
arg15,
388
): (
389
u64,
390
u64,
391
u64,
392
u64,
393
u64,
394
u64,
395
u64,
396
u64,
397
u64,
398
u64,
399
u64,
400
u64,
401
u64,
402
u64,
403
u64,
404
u64,
405
)|
406
{
407
wasmtime::component::__internal::Box::new(async move {
408
let host = &mut host_getter(caller.data_mut());
409
let r = Host::many_args(
410
host,
411
arg0,
412
arg1,
413
arg2,
414
arg3,
415
arg4,
416
arg5,
417
arg6,
418
arg7,
419
arg8,
420
arg9,
421
arg10,
422
arg11,
423
arg12,
424
arg13,
425
arg14,
426
arg15,
427
)
428
.await;
429
Ok(r)
430
})
431
},
432
)?;
433
inst.func_wrap_async(
434
"big-argument",
435
move |
436
mut caller: wasmtime::StoreContextMut<'_, T>,
437
(arg0,): (BigStruct,)|
438
{
439
wasmtime::component::__internal::Box::new(async move {
440
let host = &mut host_getter(caller.data_mut());
441
let r = Host::big_argument(host, arg0).await;
442
Ok(r)
443
})
444
},
445
)?;
446
Ok(())
447
}
448
}
449
}
450
}
451
pub mod exports {
452
pub mod foo {
453
pub mod foo {
454
#[allow(clippy::all)]
455
pub mod manyarg {
456
#[allow(unused_imports)]
457
use wasmtime::component::__internal::{anyhow, Box};
458
#[derive(wasmtime::component::ComponentType)]
459
#[derive(wasmtime::component::Lift)]
460
#[derive(wasmtime::component::Lower)]
461
#[component(record)]
462
#[derive(Clone)]
463
pub struct BigStruct {
464
#[component(name = "a1")]
465
pub a1: wasmtime::component::__internal::String,
466
#[component(name = "a2")]
467
pub a2: wasmtime::component::__internal::String,
468
#[component(name = "a3")]
469
pub a3: wasmtime::component::__internal::String,
470
#[component(name = "a4")]
471
pub a4: wasmtime::component::__internal::String,
472
#[component(name = "a5")]
473
pub a5: wasmtime::component::__internal::String,
474
#[component(name = "a6")]
475
pub a6: wasmtime::component::__internal::String,
476
#[component(name = "a7")]
477
pub a7: wasmtime::component::__internal::String,
478
#[component(name = "a8")]
479
pub a8: wasmtime::component::__internal::String,
480
#[component(name = "a9")]
481
pub a9: wasmtime::component::__internal::String,
482
#[component(name = "a10")]
483
pub a10: wasmtime::component::__internal::String,
484
#[component(name = "a11")]
485
pub a11: wasmtime::component::__internal::String,
486
#[component(name = "a12")]
487
pub a12: wasmtime::component::__internal::String,
488
#[component(name = "a13")]
489
pub a13: wasmtime::component::__internal::String,
490
#[component(name = "a14")]
491
pub a14: wasmtime::component::__internal::String,
492
#[component(name = "a15")]
493
pub a15: wasmtime::component::__internal::String,
494
#[component(name = "a16")]
495
pub a16: wasmtime::component::__internal::String,
496
#[component(name = "a17")]
497
pub a17: wasmtime::component::__internal::String,
498
#[component(name = "a18")]
499
pub a18: wasmtime::component::__internal::String,
500
#[component(name = "a19")]
501
pub a19: wasmtime::component::__internal::String,
502
#[component(name = "a20")]
503
pub a20: wasmtime::component::__internal::String,
504
}
505
impl core::fmt::Debug for BigStruct {
506
fn fmt(
507
&self,
508
f: &mut core::fmt::Formatter<'_>,
509
) -> core::fmt::Result {
510
f.debug_struct("BigStruct")
511
.field("a1", &self.a1)
512
.field("a2", &self.a2)
513
.field("a3", &self.a3)
514
.field("a4", &self.a4)
515
.field("a5", &self.a5)
516
.field("a6", &self.a6)
517
.field("a7", &self.a7)
518
.field("a8", &self.a8)
519
.field("a9", &self.a9)
520
.field("a10", &self.a10)
521
.field("a11", &self.a11)
522
.field("a12", &self.a12)
523
.field("a13", &self.a13)
524
.field("a14", &self.a14)
525
.field("a15", &self.a15)
526
.field("a16", &self.a16)
527
.field("a17", &self.a17)
528
.field("a18", &self.a18)
529
.field("a19", &self.a19)
530
.field("a20", &self.a20)
531
.finish()
532
}
533
}
534
const _: () = {
535
assert!(
536
160 == < BigStruct as wasmtime::component::ComponentType
537
>::SIZE32
538
);
539
assert!(
540
4 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32
541
);
542
};
543
pub struct Guest {
544
many_args: wasmtime::component::Func,
545
big_argument: wasmtime::component::Func,
546
}
547
#[derive(Clone)]
548
pub struct GuestIndices {
549
many_args: wasmtime::component::ComponentExportIndex,
550
big_argument: wasmtime::component::ComponentExportIndex,
551
}
552
impl GuestIndices {
553
/// Constructor for [`GuestIndices`] which takes a
554
/// [`Component`](wasmtime::component::Component) as input and can be executed
555
/// before instantiation.
556
///
557
/// This constructor can be used to front-load string lookups to find exports
558
/// within a component.
559
pub fn new<_T>(
560
_instance_pre: &wasmtime::component::InstancePre<_T>,
561
) -> wasmtime::Result<GuestIndices> {
562
let instance = _instance_pre
563
.component()
564
.get_export_index(None, "foo:foo/manyarg")
565
.ok_or_else(|| {
566
anyhow::anyhow!(
567
"no exported instance named `foo:foo/manyarg`"
568
)
569
})?;
570
let mut lookup = move |name| {
571
_instance_pre
572
.component()
573
.get_export_index(Some(&instance), name)
574
.ok_or_else(|| {
575
anyhow::anyhow!(
576
"instance export `foo:foo/manyarg` does \
577
not have export `{name}`"
578
)
579
})
580
};
581
let _ = &mut lookup;
582
let many_args = lookup("many-args")?;
583
let big_argument = lookup("big-argument")?;
584
Ok(GuestIndices {
585
many_args,
586
big_argument,
587
})
588
}
589
pub fn load(
590
&self,
591
mut store: impl wasmtime::AsContextMut,
592
instance: &wasmtime::component::Instance,
593
) -> wasmtime::Result<Guest> {
594
let _instance = instance;
595
let _instance_pre = _instance.instance_pre(&store);
596
let _instance_type = _instance_pre.instance_type();
597
let mut store = store.as_context_mut();
598
let _ = &mut store;
599
let many_args = *_instance
600
.get_typed_func::<
601
(
602
u64,
603
u64,
604
u64,
605
u64,
606
u64,
607
u64,
608
u64,
609
u64,
610
u64,
611
u64,
612
u64,
613
u64,
614
u64,
615
u64,
616
u64,
617
u64,
618
),
619
(),
620
>(&mut store, &self.many_args)?
621
.func();
622
let big_argument = *_instance
623
.get_typed_func::<
624
(&BigStruct,),
625
(),
626
>(&mut store, &self.big_argument)?
627
.func();
628
Ok(Guest { many_args, big_argument })
629
}
630
}
631
impl Guest {
632
pub async fn call_many_args<S: wasmtime::AsContextMut>(
633
&self,
634
mut store: S,
635
arg0: u64,
636
arg1: u64,
637
arg2: u64,
638
arg3: u64,
639
arg4: u64,
640
arg5: u64,
641
arg6: u64,
642
arg7: u64,
643
arg8: u64,
644
arg9: u64,
645
arg10: u64,
646
arg11: u64,
647
arg12: u64,
648
arg13: u64,
649
arg14: u64,
650
arg15: u64,
651
) -> wasmtime::Result<()>
652
where
653
<S as wasmtime::AsContext>::Data: Send,
654
{
655
let callee = unsafe {
656
wasmtime::component::TypedFunc::<
657
(
658
u64,
659
u64,
660
u64,
661
u64,
662
u64,
663
u64,
664
u64,
665
u64,
666
u64,
667
u64,
668
u64,
669
u64,
670
u64,
671
u64,
672
u64,
673
u64,
674
),
675
(),
676
>::new_unchecked(self.many_args)
677
};
678
let () = callee
679
.call_async(
680
store.as_context_mut(),
681
(
682
arg0,
683
arg1,
684
arg2,
685
arg3,
686
arg4,
687
arg5,
688
arg6,
689
arg7,
690
arg8,
691
arg9,
692
arg10,
693
arg11,
694
arg12,
695
arg13,
696
arg14,
697
arg15,
698
),
699
)
700
.await?;
701
callee.post_return_async(store.as_context_mut()).await?;
702
Ok(())
703
}
704
pub async fn call_big_argument<S: wasmtime::AsContextMut>(
705
&self,
706
mut store: S,
707
arg0: &BigStruct,
708
) -> wasmtime::Result<()>
709
where
710
<S as wasmtime::AsContext>::Data: Send,
711
{
712
let callee = unsafe {
713
wasmtime::component::TypedFunc::<
714
(&BigStruct,),
715
(),
716
>::new_unchecked(self.big_argument)
717
};
718
let () = callee
719
.call_async(store.as_context_mut(), (arg0,))
720
.await?;
721
callee.post_return_async(store.as_context_mut()).await?;
722
Ok(())
723
}
724
}
725
}
726
}
727
}
728
}
729
730