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