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