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