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