Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/many-arguments_concurrent.rs
3093 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
fn many_args<T: Send>(
277
accessor: &wasmtime::component::Accessor<T, Self>,
278
a1: u64,
279
a2: u64,
280
a3: u64,
281
a4: u64,
282
a5: u64,
283
a6: u64,
284
a7: u64,
285
a8: u64,
286
a9: u64,
287
a10: u64,
288
a11: u64,
289
a12: u64,
290
a13: u64,
291
a14: u64,
292
a15: u64,
293
a16: u64,
294
) -> impl ::core::future::Future<Output = ()> + Send;
295
fn big_argument<T: Send>(
296
accessor: &wasmtime::component::Accessor<T, Self>,
297
x: BigStruct,
298
) -> impl ::core::future::Future<Output = ()> + Send;
299
}
300
pub trait Host: Send {}
301
impl<_T: Host + ?Sized + Send> Host for &mut _T {}
302
pub fn add_to_linker<T, D>(
303
linker: &mut wasmtime::component::Linker<T>,
304
host_getter: fn(&mut T) -> D::Data<'_>,
305
) -> wasmtime::Result<()>
306
where
307
D: HostWithStore,
308
for<'a> D::Data<'a>: Host,
309
T: 'static + Send,
310
{
311
let mut inst = linker.instance("foo:foo/manyarg")?;
312
inst.func_wrap_concurrent(
313
"many-args",
314
move |
315
caller: &wasmtime::component::Accessor<T>,
316
(
317
arg0,
318
arg1,
319
arg2,
320
arg3,
321
arg4,
322
arg5,
323
arg6,
324
arg7,
325
arg8,
326
arg9,
327
arg10,
328
arg11,
329
arg12,
330
arg13,
331
arg14,
332
arg15,
333
): (
334
u64,
335
u64,
336
u64,
337
u64,
338
u64,
339
u64,
340
u64,
341
u64,
342
u64,
343
u64,
344
u64,
345
u64,
346
u64,
347
u64,
348
u64,
349
u64,
350
)|
351
{
352
wasmtime::component::__internal::Box::pin(async move {
353
let host = &caller.with_getter(host_getter);
354
let r = <D as HostWithStore>::many_args(
355
host,
356
arg0,
357
arg1,
358
arg2,
359
arg3,
360
arg4,
361
arg5,
362
arg6,
363
arg7,
364
arg8,
365
arg9,
366
arg10,
367
arg11,
368
arg12,
369
arg13,
370
arg14,
371
arg15,
372
)
373
.await;
374
Ok(r)
375
})
376
},
377
)?;
378
inst.func_wrap_concurrent(
379
"big-argument",
380
move |
381
caller: &wasmtime::component::Accessor<T>,
382
(arg0,): (BigStruct,)|
383
{
384
wasmtime::component::__internal::Box::pin(async move {
385
let host = &caller.with_getter(host_getter);
386
let r = <D as HostWithStore>::big_argument(host, arg0).await;
387
Ok(r)
388
})
389
},
390
)?;
391
Ok(())
392
}
393
}
394
}
395
}
396
pub mod exports {
397
pub mod foo {
398
pub mod foo {
399
#[allow(clippy::all)]
400
pub mod manyarg {
401
#[allow(unused_imports)]
402
use wasmtime::component::__internal::Box;
403
#[derive(wasmtime::component::ComponentType)]
404
#[derive(wasmtime::component::Lift)]
405
#[derive(wasmtime::component::Lower)]
406
#[component(record)]
407
#[derive(Clone)]
408
pub struct BigStruct {
409
#[component(name = "a1")]
410
pub a1: wasmtime::component::__internal::String,
411
#[component(name = "a2")]
412
pub a2: wasmtime::component::__internal::String,
413
#[component(name = "a3")]
414
pub a3: wasmtime::component::__internal::String,
415
#[component(name = "a4")]
416
pub a4: wasmtime::component::__internal::String,
417
#[component(name = "a5")]
418
pub a5: wasmtime::component::__internal::String,
419
#[component(name = "a6")]
420
pub a6: wasmtime::component::__internal::String,
421
#[component(name = "a7")]
422
pub a7: wasmtime::component::__internal::String,
423
#[component(name = "a8")]
424
pub a8: wasmtime::component::__internal::String,
425
#[component(name = "a9")]
426
pub a9: wasmtime::component::__internal::String,
427
#[component(name = "a10")]
428
pub a10: wasmtime::component::__internal::String,
429
#[component(name = "a11")]
430
pub a11: wasmtime::component::__internal::String,
431
#[component(name = "a12")]
432
pub a12: wasmtime::component::__internal::String,
433
#[component(name = "a13")]
434
pub a13: wasmtime::component::__internal::String,
435
#[component(name = "a14")]
436
pub a14: wasmtime::component::__internal::String,
437
#[component(name = "a15")]
438
pub a15: wasmtime::component::__internal::String,
439
#[component(name = "a16")]
440
pub a16: wasmtime::component::__internal::String,
441
#[component(name = "a17")]
442
pub a17: wasmtime::component::__internal::String,
443
#[component(name = "a18")]
444
pub a18: wasmtime::component::__internal::String,
445
#[component(name = "a19")]
446
pub a19: wasmtime::component::__internal::String,
447
#[component(name = "a20")]
448
pub a20: wasmtime::component::__internal::String,
449
}
450
impl core::fmt::Debug for BigStruct {
451
fn fmt(
452
&self,
453
f: &mut core::fmt::Formatter<'_>,
454
) -> core::fmt::Result {
455
f.debug_struct("BigStruct")
456
.field("a1", &self.a1)
457
.field("a2", &self.a2)
458
.field("a3", &self.a3)
459
.field("a4", &self.a4)
460
.field("a5", &self.a5)
461
.field("a6", &self.a6)
462
.field("a7", &self.a7)
463
.field("a8", &self.a8)
464
.field("a9", &self.a9)
465
.field("a10", &self.a10)
466
.field("a11", &self.a11)
467
.field("a12", &self.a12)
468
.field("a13", &self.a13)
469
.field("a14", &self.a14)
470
.field("a15", &self.a15)
471
.field("a16", &self.a16)
472
.field("a17", &self.a17)
473
.field("a18", &self.a18)
474
.field("a19", &self.a19)
475
.field("a20", &self.a20)
476
.finish()
477
}
478
}
479
const _: () = {
480
assert!(
481
160 == < BigStruct as wasmtime::component::ComponentType
482
>::SIZE32
483
);
484
assert!(
485
4 == < BigStruct as wasmtime::component::ComponentType >::ALIGN32
486
);
487
};
488
#[derive(Clone)]
489
pub struct Guest {
490
many_args: wasmtime::component::Func,
491
big_argument: wasmtime::component::Func,
492
}
493
#[derive(Clone)]
494
pub struct GuestIndices {
495
many_args: wasmtime::component::ComponentExportIndex,
496
big_argument: wasmtime::component::ComponentExportIndex,
497
}
498
impl GuestIndices {
499
/// Constructor for [`GuestIndices`] which takes a
500
/// [`Component`](wasmtime::component::Component) as input and can be executed
501
/// before instantiation.
502
///
503
/// This constructor can be used to front-load string lookups to find exports
504
/// within a component.
505
pub fn new<_T>(
506
_instance_pre: &wasmtime::component::InstancePre<_T>,
507
) -> wasmtime::Result<GuestIndices> {
508
let instance = _instance_pre
509
.component()
510
.get_export_index(None, "foo:foo/manyarg")
511
.ok_or_else(|| {
512
wasmtime::format_err!(
513
"no exported instance named `foo:foo/manyarg`"
514
)
515
})?;
516
let mut lookup = move |name| {
517
_instance_pre
518
.component()
519
.get_export_index(Some(&instance), name)
520
.ok_or_else(|| {
521
wasmtime::format_err!(
522
"instance export `foo:foo/manyarg` does \
523
not have export `{name}`"
524
)
525
})
526
};
527
let _ = &mut lookup;
528
let many_args = lookup("many-args")?;
529
let big_argument = lookup("big-argument")?;
530
Ok(GuestIndices {
531
many_args,
532
big_argument,
533
})
534
}
535
pub fn load(
536
&self,
537
mut store: impl wasmtime::AsContextMut,
538
instance: &wasmtime::component::Instance,
539
) -> wasmtime::Result<Guest> {
540
let _instance = instance;
541
let _instance_pre = _instance.instance_pre(&store);
542
let _instance_type = _instance_pre.instance_type();
543
let mut store = store.as_context_mut();
544
let _ = &mut store;
545
let many_args = *_instance
546
.get_typed_func::<
547
(
548
u64,
549
u64,
550
u64,
551
u64,
552
u64,
553
u64,
554
u64,
555
u64,
556
u64,
557
u64,
558
u64,
559
u64,
560
u64,
561
u64,
562
u64,
563
u64,
564
),
565
(),
566
>(&mut store, &self.many_args)?
567
.func();
568
let big_argument = *_instance
569
.get_typed_func::<
570
(&BigStruct,),
571
(),
572
>(&mut store, &self.big_argument)?
573
.func();
574
Ok(Guest { many_args, big_argument })
575
}
576
}
577
impl Guest {
578
pub async fn call_many_args<_T, _D>(
579
&self,
580
accessor: &wasmtime::component::Accessor<_T, _D>,
581
arg0: u64,
582
arg1: u64,
583
arg2: u64,
584
arg3: u64,
585
arg4: u64,
586
arg5: u64,
587
arg6: u64,
588
arg7: u64,
589
arg8: u64,
590
arg9: u64,
591
arg10: u64,
592
arg11: u64,
593
arg12: u64,
594
arg13: u64,
595
arg14: u64,
596
arg15: u64,
597
) -> wasmtime::Result<()>
598
where
599
_T: Send,
600
_D: wasmtime::component::HasData,
601
{
602
let callee = unsafe {
603
wasmtime::component::TypedFunc::<
604
(
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
u64,
619
u64,
620
u64,
621
),
622
(),
623
>::new_unchecked(self.many_args)
624
};
625
let ((), _) = callee
626
.call_concurrent(
627
accessor,
628
(
629
arg0,
630
arg1,
631
arg2,
632
arg3,
633
arg4,
634
arg5,
635
arg6,
636
arg7,
637
arg8,
638
arg9,
639
arg10,
640
arg11,
641
arg12,
642
arg13,
643
arg14,
644
arg15,
645
),
646
)
647
.await?;
648
Ok(())
649
}
650
pub async fn call_big_argument<_T, _D>(
651
&self,
652
accessor: &wasmtime::component::Accessor<_T, _D>,
653
arg0: BigStruct,
654
) -> wasmtime::Result<()>
655
where
656
_T: Send,
657
_D: wasmtime::component::HasData,
658
{
659
let callee = unsafe {
660
wasmtime::component::TypedFunc::<
661
(BigStruct,),
662
(),
663
>::new_unchecked(self.big_argument)
664
};
665
let ((), _) = callee.call_concurrent(accessor, (arg0,)).await?;
666
Ok(())
667
}
668
}
669
}
670
}
671
}
672
}
673
674