Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/conventions_tracing_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::conventions::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::conventions::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::conventions::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::conventions::HostWithStore + Send,
177
for<'a> D::Data<'a>: foo::foo::conventions::Host + Send,
178
T: 'static + Send,
179
{
180
foo::foo::conventions::add_to_linker::<T, D>(linker, host_getter)?;
181
Ok(())
182
}
183
pub fn foo_foo_conventions(&self) -> &exports::foo::foo::conventions::Guest {
184
&self.interface0
185
}
186
}
187
};
188
pub mod foo {
189
pub mod foo {
190
#[allow(clippy::all)]
191
pub mod conventions {
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, Copy)]
199
pub struct LudicrousSpeed {
200
#[component(name = "how-fast-are-you-going")]
201
pub how_fast_are_you_going: u32,
202
#[component(name = "i-am-going-extremely-slow")]
203
pub i_am_going_extremely_slow: u64,
204
}
205
impl core::fmt::Debug for LudicrousSpeed {
206
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
207
f.debug_struct("LudicrousSpeed")
208
.field("how-fast-are-you-going", &self.how_fast_are_you_going)
209
.field(
210
"i-am-going-extremely-slow",
211
&self.i_am_going_extremely_slow,
212
)
213
.finish()
214
}
215
}
216
const _: () = {
217
assert!(
218
16 == < LudicrousSpeed as wasmtime::component::ComponentType
219
>::SIZE32
220
);
221
assert!(
222
8 == < LudicrousSpeed as wasmtime::component::ComponentType
223
>::ALIGN32
224
);
225
};
226
pub trait HostWithStore: wasmtime::component::HasData + Send {}
227
impl<_T: ?Sized> HostWithStore for _T
228
where
229
_T: wasmtime::component::HasData + Send,
230
{}
231
pub trait Host: Send {
232
fn kebab_case(
233
&mut self,
234
) -> impl ::core::future::Future<Output = ()> + Send;
235
fn foo(
236
&mut self,
237
x: LudicrousSpeed,
238
) -> impl ::core::future::Future<Output = ()> + Send;
239
fn function_with_dashes(
240
&mut self,
241
) -> impl ::core::future::Future<Output = ()> + Send;
242
fn function_with_no_weird_characters(
243
&mut self,
244
) -> impl ::core::future::Future<Output = ()> + Send;
245
fn apple(&mut self) -> impl ::core::future::Future<Output = ()> + Send;
246
fn apple_pear(
247
&mut self,
248
) -> impl ::core::future::Future<Output = ()> + Send;
249
fn apple_pear_grape(
250
&mut self,
251
) -> impl ::core::future::Future<Output = ()> + Send;
252
fn a0(&mut self) -> impl ::core::future::Future<Output = ()> + Send;
253
/// Comment out identifiers that collide when mapped to snake_case, for now; see
254
/// https://github.com/WebAssembly/component-model/issues/118
255
/// APPLE: func()
256
/// APPLE-pear-GRAPE: func()
257
/// apple-PEAR-grape: func()
258
fn is_xml(&mut self) -> impl ::core::future::Future<Output = ()> + Send;
259
fn explicit(
260
&mut self,
261
) -> impl ::core::future::Future<Output = ()> + Send;
262
fn explicit_kebab(
263
&mut self,
264
) -> impl ::core::future::Future<Output = ()> + Send;
265
/// Identifiers with the same name as keywords are quoted.
266
fn bool(&mut self) -> impl ::core::future::Future<Output = ()> + Send;
267
}
268
impl<_T: Host + ?Sized + Send> Host for &mut _T {
269
fn kebab_case(
270
&mut self,
271
) -> impl ::core::future::Future<Output = ()> + Send {
272
async move { Host::kebab_case(*self).await }
273
}
274
fn foo(
275
&mut self,
276
x: LudicrousSpeed,
277
) -> impl ::core::future::Future<Output = ()> + Send {
278
async move { Host::foo(*self, x).await }
279
}
280
fn function_with_dashes(
281
&mut self,
282
) -> impl ::core::future::Future<Output = ()> + Send {
283
async move { Host::function_with_dashes(*self).await }
284
}
285
fn function_with_no_weird_characters(
286
&mut self,
287
) -> impl ::core::future::Future<Output = ()> + Send {
288
async move { Host::function_with_no_weird_characters(*self).await }
289
}
290
fn apple(&mut self) -> impl ::core::future::Future<Output = ()> + Send {
291
async move { Host::apple(*self).await }
292
}
293
fn apple_pear(
294
&mut self,
295
) -> impl ::core::future::Future<Output = ()> + Send {
296
async move { Host::apple_pear(*self).await }
297
}
298
fn apple_pear_grape(
299
&mut self,
300
) -> impl ::core::future::Future<Output = ()> + Send {
301
async move { Host::apple_pear_grape(*self).await }
302
}
303
fn a0(&mut self) -> impl ::core::future::Future<Output = ()> + Send {
304
async move { Host::a0(*self).await }
305
}
306
/// Comment out identifiers that collide when mapped to snake_case, for now; see
307
/// https://github.com/WebAssembly/component-model/issues/118
308
/// APPLE: func()
309
/// APPLE-pear-GRAPE: func()
310
/// apple-PEAR-grape: func()
311
fn is_xml(&mut self) -> impl ::core::future::Future<Output = ()> + Send {
312
async move { Host::is_xml(*self).await }
313
}
314
fn explicit(
315
&mut self,
316
) -> impl ::core::future::Future<Output = ()> + Send {
317
async move { Host::explicit(*self).await }
318
}
319
fn explicit_kebab(
320
&mut self,
321
) -> impl ::core::future::Future<Output = ()> + Send {
322
async move { Host::explicit_kebab(*self).await }
323
}
324
/// Identifiers with the same name as keywords are quoted.
325
fn bool(&mut self) -> impl ::core::future::Future<Output = ()> + Send {
326
async move { Host::bool(*self).await }
327
}
328
}
329
pub fn add_to_linker<T, D>(
330
linker: &mut wasmtime::component::Linker<T>,
331
host_getter: fn(&mut T) -> D::Data<'_>,
332
) -> wasmtime::Result<()>
333
where
334
D: HostWithStore,
335
for<'a> D::Data<'a>: Host,
336
T: 'static + Send,
337
{
338
let mut inst = linker.instance("foo:foo/conventions")?;
339
inst.func_wrap_async(
340
"kebab-case",
341
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
342
use tracing::Instrument;
343
let span = tracing::span!(
344
tracing::Level::TRACE, "wit-bindgen import", module =
345
"conventions", function = "kebab-case",
346
);
347
wasmtime::component::__internal::Box::new(
348
async move {
349
tracing::event!(tracing::Level::TRACE, "call");
350
let host = &mut host_getter(caller.data_mut());
351
let r = Host::kebab_case(host).await;
352
tracing::event!(
353
tracing::Level::TRACE, result = tracing::field::debug(& r),
354
"return"
355
);
356
Ok(r)
357
}
358
.instrument(span),
359
)
360
},
361
)?;
362
inst.func_wrap_async(
363
"foo",
364
move |
365
mut caller: wasmtime::StoreContextMut<'_, T>,
366
(arg0,): (LudicrousSpeed,)|
367
{
368
use tracing::Instrument;
369
let span = tracing::span!(
370
tracing::Level::TRACE, "wit-bindgen import", module =
371
"conventions", function = "foo",
372
);
373
wasmtime::component::__internal::Box::new(
374
async move {
375
tracing::event!(
376
tracing::Level::TRACE, x = tracing::field::debug(& arg0),
377
"call"
378
);
379
let host = &mut host_getter(caller.data_mut());
380
let r = Host::foo(host, arg0).await;
381
tracing::event!(
382
tracing::Level::TRACE, result = tracing::field::debug(& r),
383
"return"
384
);
385
Ok(r)
386
}
387
.instrument(span),
388
)
389
},
390
)?;
391
inst.func_wrap_async(
392
"function-with-dashes",
393
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
394
use tracing::Instrument;
395
let span = tracing::span!(
396
tracing::Level::TRACE, "wit-bindgen import", module =
397
"conventions", function = "function-with-dashes",
398
);
399
wasmtime::component::__internal::Box::new(
400
async move {
401
tracing::event!(tracing::Level::TRACE, "call");
402
let host = &mut host_getter(caller.data_mut());
403
let r = Host::function_with_dashes(host).await;
404
tracing::event!(
405
tracing::Level::TRACE, result = tracing::field::debug(& r),
406
"return"
407
);
408
Ok(r)
409
}
410
.instrument(span),
411
)
412
},
413
)?;
414
inst.func_wrap_async(
415
"function-with-no-weird-characters",
416
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
417
use tracing::Instrument;
418
let span = tracing::span!(
419
tracing::Level::TRACE, "wit-bindgen import", module =
420
"conventions", function =
421
"function-with-no-weird-characters",
422
);
423
wasmtime::component::__internal::Box::new(
424
async move {
425
tracing::event!(tracing::Level::TRACE, "call");
426
let host = &mut host_getter(caller.data_mut());
427
let r = Host::function_with_no_weird_characters(host).await;
428
tracing::event!(
429
tracing::Level::TRACE, result = tracing::field::debug(& r),
430
"return"
431
);
432
Ok(r)
433
}
434
.instrument(span),
435
)
436
},
437
)?;
438
inst.func_wrap_async(
439
"apple",
440
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
441
use tracing::Instrument;
442
let span = tracing::span!(
443
tracing::Level::TRACE, "wit-bindgen import", module =
444
"conventions", function = "apple",
445
);
446
wasmtime::component::__internal::Box::new(
447
async move {
448
tracing::event!(tracing::Level::TRACE, "call");
449
let host = &mut host_getter(caller.data_mut());
450
let r = Host::apple(host).await;
451
tracing::event!(
452
tracing::Level::TRACE, result = tracing::field::debug(& r),
453
"return"
454
);
455
Ok(r)
456
}
457
.instrument(span),
458
)
459
},
460
)?;
461
inst.func_wrap_async(
462
"apple-pear",
463
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
464
use tracing::Instrument;
465
let span = tracing::span!(
466
tracing::Level::TRACE, "wit-bindgen import", module =
467
"conventions", function = "apple-pear",
468
);
469
wasmtime::component::__internal::Box::new(
470
async move {
471
tracing::event!(tracing::Level::TRACE, "call");
472
let host = &mut host_getter(caller.data_mut());
473
let r = Host::apple_pear(host).await;
474
tracing::event!(
475
tracing::Level::TRACE, result = tracing::field::debug(& r),
476
"return"
477
);
478
Ok(r)
479
}
480
.instrument(span),
481
)
482
},
483
)?;
484
inst.func_wrap_async(
485
"apple-pear-grape",
486
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
487
use tracing::Instrument;
488
let span = tracing::span!(
489
tracing::Level::TRACE, "wit-bindgen import", module =
490
"conventions", function = "apple-pear-grape",
491
);
492
wasmtime::component::__internal::Box::new(
493
async move {
494
tracing::event!(tracing::Level::TRACE, "call");
495
let host = &mut host_getter(caller.data_mut());
496
let r = Host::apple_pear_grape(host).await;
497
tracing::event!(
498
tracing::Level::TRACE, result = tracing::field::debug(& r),
499
"return"
500
);
501
Ok(r)
502
}
503
.instrument(span),
504
)
505
},
506
)?;
507
inst.func_wrap_async(
508
"a0",
509
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
510
use tracing::Instrument;
511
let span = tracing::span!(
512
tracing::Level::TRACE, "wit-bindgen import", module =
513
"conventions", function = "a0",
514
);
515
wasmtime::component::__internal::Box::new(
516
async move {
517
tracing::event!(tracing::Level::TRACE, "call");
518
let host = &mut host_getter(caller.data_mut());
519
let r = Host::a0(host).await;
520
tracing::event!(
521
tracing::Level::TRACE, result = tracing::field::debug(& r),
522
"return"
523
);
524
Ok(r)
525
}
526
.instrument(span),
527
)
528
},
529
)?;
530
inst.func_wrap_async(
531
"is-XML",
532
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
533
use tracing::Instrument;
534
let span = tracing::span!(
535
tracing::Level::TRACE, "wit-bindgen import", module =
536
"conventions", function = "is-XML",
537
);
538
wasmtime::component::__internal::Box::new(
539
async move {
540
tracing::event!(tracing::Level::TRACE, "call");
541
let host = &mut host_getter(caller.data_mut());
542
let r = Host::is_xml(host).await;
543
tracing::event!(
544
tracing::Level::TRACE, result = tracing::field::debug(& r),
545
"return"
546
);
547
Ok(r)
548
}
549
.instrument(span),
550
)
551
},
552
)?;
553
inst.func_wrap_async(
554
"explicit",
555
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
556
use tracing::Instrument;
557
let span = tracing::span!(
558
tracing::Level::TRACE, "wit-bindgen import", module =
559
"conventions", function = "explicit",
560
);
561
wasmtime::component::__internal::Box::new(
562
async move {
563
tracing::event!(tracing::Level::TRACE, "call");
564
let host = &mut host_getter(caller.data_mut());
565
let r = Host::explicit(host).await;
566
tracing::event!(
567
tracing::Level::TRACE, result = tracing::field::debug(& r),
568
"return"
569
);
570
Ok(r)
571
}
572
.instrument(span),
573
)
574
},
575
)?;
576
inst.func_wrap_async(
577
"explicit-kebab",
578
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
579
use tracing::Instrument;
580
let span = tracing::span!(
581
tracing::Level::TRACE, "wit-bindgen import", module =
582
"conventions", function = "explicit-kebab",
583
);
584
wasmtime::component::__internal::Box::new(
585
async move {
586
tracing::event!(tracing::Level::TRACE, "call");
587
let host = &mut host_getter(caller.data_mut());
588
let r = Host::explicit_kebab(host).await;
589
tracing::event!(
590
tracing::Level::TRACE, result = tracing::field::debug(& r),
591
"return"
592
);
593
Ok(r)
594
}
595
.instrument(span),
596
)
597
},
598
)?;
599
inst.func_wrap_async(
600
"bool",
601
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
602
use tracing::Instrument;
603
let span = tracing::span!(
604
tracing::Level::TRACE, "wit-bindgen import", module =
605
"conventions", function = "bool",
606
);
607
wasmtime::component::__internal::Box::new(
608
async move {
609
tracing::event!(tracing::Level::TRACE, "call");
610
let host = &mut host_getter(caller.data_mut());
611
let r = Host::bool(host).await;
612
tracing::event!(
613
tracing::Level::TRACE, result = tracing::field::debug(& r),
614
"return"
615
);
616
Ok(r)
617
}
618
.instrument(span),
619
)
620
},
621
)?;
622
Ok(())
623
}
624
}
625
}
626
}
627
pub mod exports {
628
pub mod foo {
629
pub mod foo {
630
#[allow(clippy::all)]
631
pub mod conventions {
632
#[allow(unused_imports)]
633
use wasmtime::component::__internal::Box;
634
#[derive(wasmtime::component::ComponentType)]
635
#[derive(wasmtime::component::Lift)]
636
#[derive(wasmtime::component::Lower)]
637
#[component(record)]
638
#[derive(Clone, Copy)]
639
pub struct LudicrousSpeed {
640
#[component(name = "how-fast-are-you-going")]
641
pub how_fast_are_you_going: u32,
642
#[component(name = "i-am-going-extremely-slow")]
643
pub i_am_going_extremely_slow: u64,
644
}
645
impl core::fmt::Debug for LudicrousSpeed {
646
fn fmt(
647
&self,
648
f: &mut core::fmt::Formatter<'_>,
649
) -> core::fmt::Result {
650
f.debug_struct("LudicrousSpeed")
651
.field(
652
"how-fast-are-you-going",
653
&self.how_fast_are_you_going,
654
)
655
.field(
656
"i-am-going-extremely-slow",
657
&self.i_am_going_extremely_slow,
658
)
659
.finish()
660
}
661
}
662
const _: () = {
663
assert!(
664
16 == < LudicrousSpeed as wasmtime::component::ComponentType
665
>::SIZE32
666
);
667
assert!(
668
8 == < LudicrousSpeed as wasmtime::component::ComponentType
669
>::ALIGN32
670
);
671
};
672
#[derive(Clone)]
673
pub struct Guest {
674
kebab_case: wasmtime::component::Func,
675
foo: wasmtime::component::Func,
676
function_with_dashes: wasmtime::component::Func,
677
function_with_no_weird_characters: wasmtime::component::Func,
678
apple: wasmtime::component::Func,
679
apple_pear: wasmtime::component::Func,
680
apple_pear_grape: wasmtime::component::Func,
681
a0: wasmtime::component::Func,
682
is_xml: wasmtime::component::Func,
683
explicit: wasmtime::component::Func,
684
explicit_kebab: wasmtime::component::Func,
685
bool: wasmtime::component::Func,
686
}
687
#[derive(Clone)]
688
pub struct GuestIndices {
689
kebab_case: wasmtime::component::ComponentExportIndex,
690
foo: wasmtime::component::ComponentExportIndex,
691
function_with_dashes: wasmtime::component::ComponentExportIndex,
692
function_with_no_weird_characters: wasmtime::component::ComponentExportIndex,
693
apple: wasmtime::component::ComponentExportIndex,
694
apple_pear: wasmtime::component::ComponentExportIndex,
695
apple_pear_grape: wasmtime::component::ComponentExportIndex,
696
a0: wasmtime::component::ComponentExportIndex,
697
is_xml: wasmtime::component::ComponentExportIndex,
698
explicit: wasmtime::component::ComponentExportIndex,
699
explicit_kebab: wasmtime::component::ComponentExportIndex,
700
bool: wasmtime::component::ComponentExportIndex,
701
}
702
impl GuestIndices {
703
/// Constructor for [`GuestIndices`] which takes a
704
/// [`Component`](wasmtime::component::Component) as input and can be executed
705
/// before instantiation.
706
///
707
/// This constructor can be used to front-load string lookups to find exports
708
/// within a component.
709
pub fn new<_T>(
710
_instance_pre: &wasmtime::component::InstancePre<_T>,
711
) -> wasmtime::Result<GuestIndices> {
712
let instance = _instance_pre
713
.component()
714
.get_export_index(None, "foo:foo/conventions")
715
.ok_or_else(|| {
716
wasmtime::format_err!(
717
"no exported instance named `foo:foo/conventions`"
718
)
719
})?;
720
let mut lookup = move |name| {
721
_instance_pre
722
.component()
723
.get_export_index(Some(&instance), name)
724
.ok_or_else(|| {
725
wasmtime::format_err!(
726
"instance export `foo:foo/conventions` does \
727
not have export `{name}`"
728
)
729
})
730
};
731
let _ = &mut lookup;
732
let kebab_case = lookup("kebab-case")?;
733
let foo = lookup("foo")?;
734
let function_with_dashes = lookup("function-with-dashes")?;
735
let function_with_no_weird_characters = lookup(
736
"function-with-no-weird-characters",
737
)?;
738
let apple = lookup("apple")?;
739
let apple_pear = lookup("apple-pear")?;
740
let apple_pear_grape = lookup("apple-pear-grape")?;
741
let a0 = lookup("a0")?;
742
let is_xml = lookup("is-XML")?;
743
let explicit = lookup("explicit")?;
744
let explicit_kebab = lookup("explicit-kebab")?;
745
let bool = lookup("bool")?;
746
Ok(GuestIndices {
747
kebab_case,
748
foo,
749
function_with_dashes,
750
function_with_no_weird_characters,
751
apple,
752
apple_pear,
753
apple_pear_grape,
754
a0,
755
is_xml,
756
explicit,
757
explicit_kebab,
758
bool,
759
})
760
}
761
pub fn load(
762
&self,
763
mut store: impl wasmtime::AsContextMut,
764
instance: &wasmtime::component::Instance,
765
) -> wasmtime::Result<Guest> {
766
let _instance = instance;
767
let _instance_pre = _instance.instance_pre(&store);
768
let _instance_type = _instance_pre.instance_type();
769
let mut store = store.as_context_mut();
770
let _ = &mut store;
771
let kebab_case = *_instance
772
.get_typed_func::<(), ()>(&mut store, &self.kebab_case)?
773
.func();
774
let foo = *_instance
775
.get_typed_func::<
776
(LudicrousSpeed,),
777
(),
778
>(&mut store, &self.foo)?
779
.func();
780
let function_with_dashes = *_instance
781
.get_typed_func::<
782
(),
783
(),
784
>(&mut store, &self.function_with_dashes)?
785
.func();
786
let function_with_no_weird_characters = *_instance
787
.get_typed_func::<
788
(),
789
(),
790
>(&mut store, &self.function_with_no_weird_characters)?
791
.func();
792
let apple = *_instance
793
.get_typed_func::<(), ()>(&mut store, &self.apple)?
794
.func();
795
let apple_pear = *_instance
796
.get_typed_func::<(), ()>(&mut store, &self.apple_pear)?
797
.func();
798
let apple_pear_grape = *_instance
799
.get_typed_func::<
800
(),
801
(),
802
>(&mut store, &self.apple_pear_grape)?
803
.func();
804
let a0 = *_instance
805
.get_typed_func::<(), ()>(&mut store, &self.a0)?
806
.func();
807
let is_xml = *_instance
808
.get_typed_func::<(), ()>(&mut store, &self.is_xml)?
809
.func();
810
let explicit = *_instance
811
.get_typed_func::<(), ()>(&mut store, &self.explicit)?
812
.func();
813
let explicit_kebab = *_instance
814
.get_typed_func::<(), ()>(&mut store, &self.explicit_kebab)?
815
.func();
816
let bool = *_instance
817
.get_typed_func::<(), ()>(&mut store, &self.bool)?
818
.func();
819
Ok(Guest {
820
kebab_case,
821
foo,
822
function_with_dashes,
823
function_with_no_weird_characters,
824
apple,
825
apple_pear,
826
apple_pear_grape,
827
a0,
828
is_xml,
829
explicit,
830
explicit_kebab,
831
bool,
832
})
833
}
834
}
835
impl Guest {
836
pub async fn call_kebab_case<S: wasmtime::AsContextMut>(
837
&self,
838
mut store: S,
839
) -> wasmtime::Result<()>
840
where
841
<S as wasmtime::AsContext>::Data: Send,
842
{
843
use tracing::Instrument;
844
let span = tracing::span!(
845
tracing::Level::TRACE, "wit-bindgen export", module =
846
"foo:foo/conventions", function = "kebab-case",
847
);
848
let callee = unsafe {
849
wasmtime::component::TypedFunc::<
850
(),
851
(),
852
>::new_unchecked(self.kebab_case)
853
};
854
let () = callee
855
.call_async(store.as_context_mut(), ())
856
.instrument(span.clone())
857
.await?;
858
Ok(())
859
}
860
pub async fn call_foo<S: wasmtime::AsContextMut>(
861
&self,
862
mut store: S,
863
arg0: LudicrousSpeed,
864
) -> wasmtime::Result<()>
865
where
866
<S as wasmtime::AsContext>::Data: Send,
867
{
868
use tracing::Instrument;
869
let span = tracing::span!(
870
tracing::Level::TRACE, "wit-bindgen export", module =
871
"foo:foo/conventions", function = "foo",
872
);
873
let callee = unsafe {
874
wasmtime::component::TypedFunc::<
875
(LudicrousSpeed,),
876
(),
877
>::new_unchecked(self.foo)
878
};
879
let () = callee
880
.call_async(store.as_context_mut(), (arg0,))
881
.instrument(span.clone())
882
.await?;
883
Ok(())
884
}
885
pub async fn call_function_with_dashes<S: wasmtime::AsContextMut>(
886
&self,
887
mut store: S,
888
) -> wasmtime::Result<()>
889
where
890
<S as wasmtime::AsContext>::Data: Send,
891
{
892
use tracing::Instrument;
893
let span = tracing::span!(
894
tracing::Level::TRACE, "wit-bindgen export", module =
895
"foo:foo/conventions", function = "function-with-dashes",
896
);
897
let callee = unsafe {
898
wasmtime::component::TypedFunc::<
899
(),
900
(),
901
>::new_unchecked(self.function_with_dashes)
902
};
903
let () = callee
904
.call_async(store.as_context_mut(), ())
905
.instrument(span.clone())
906
.await?;
907
Ok(())
908
}
909
pub async fn call_function_with_no_weird_characters<
910
S: wasmtime::AsContextMut,
911
>(&self, mut store: S) -> wasmtime::Result<()>
912
where
913
<S as wasmtime::AsContext>::Data: Send,
914
{
915
use tracing::Instrument;
916
let span = tracing::span!(
917
tracing::Level::TRACE, "wit-bindgen export", module =
918
"foo:foo/conventions", function =
919
"function-with-no-weird-characters",
920
);
921
let callee = unsafe {
922
wasmtime::component::TypedFunc::<
923
(),
924
(),
925
>::new_unchecked(self.function_with_no_weird_characters)
926
};
927
let () = callee
928
.call_async(store.as_context_mut(), ())
929
.instrument(span.clone())
930
.await?;
931
Ok(())
932
}
933
pub async fn call_apple<S: wasmtime::AsContextMut>(
934
&self,
935
mut store: S,
936
) -> wasmtime::Result<()>
937
where
938
<S as wasmtime::AsContext>::Data: Send,
939
{
940
use tracing::Instrument;
941
let span = tracing::span!(
942
tracing::Level::TRACE, "wit-bindgen export", module =
943
"foo:foo/conventions", function = "apple",
944
);
945
let callee = unsafe {
946
wasmtime::component::TypedFunc::<
947
(),
948
(),
949
>::new_unchecked(self.apple)
950
};
951
let () = callee
952
.call_async(store.as_context_mut(), ())
953
.instrument(span.clone())
954
.await?;
955
Ok(())
956
}
957
pub async fn call_apple_pear<S: wasmtime::AsContextMut>(
958
&self,
959
mut store: S,
960
) -> wasmtime::Result<()>
961
where
962
<S as wasmtime::AsContext>::Data: Send,
963
{
964
use tracing::Instrument;
965
let span = tracing::span!(
966
tracing::Level::TRACE, "wit-bindgen export", module =
967
"foo:foo/conventions", function = "apple-pear",
968
);
969
let callee = unsafe {
970
wasmtime::component::TypedFunc::<
971
(),
972
(),
973
>::new_unchecked(self.apple_pear)
974
};
975
let () = callee
976
.call_async(store.as_context_mut(), ())
977
.instrument(span.clone())
978
.await?;
979
Ok(())
980
}
981
pub async fn call_apple_pear_grape<S: wasmtime::AsContextMut>(
982
&self,
983
mut store: S,
984
) -> wasmtime::Result<()>
985
where
986
<S as wasmtime::AsContext>::Data: Send,
987
{
988
use tracing::Instrument;
989
let span = tracing::span!(
990
tracing::Level::TRACE, "wit-bindgen export", module =
991
"foo:foo/conventions", function = "apple-pear-grape",
992
);
993
let callee = unsafe {
994
wasmtime::component::TypedFunc::<
995
(),
996
(),
997
>::new_unchecked(self.apple_pear_grape)
998
};
999
let () = callee
1000
.call_async(store.as_context_mut(), ())
1001
.instrument(span.clone())
1002
.await?;
1003
Ok(())
1004
}
1005
pub async fn call_a0<S: wasmtime::AsContextMut>(
1006
&self,
1007
mut store: S,
1008
) -> wasmtime::Result<()>
1009
where
1010
<S as wasmtime::AsContext>::Data: Send,
1011
{
1012
use tracing::Instrument;
1013
let span = tracing::span!(
1014
tracing::Level::TRACE, "wit-bindgen export", module =
1015
"foo:foo/conventions", function = "a0",
1016
);
1017
let callee = unsafe {
1018
wasmtime::component::TypedFunc::<
1019
(),
1020
(),
1021
>::new_unchecked(self.a0)
1022
};
1023
let () = callee
1024
.call_async(store.as_context_mut(), ())
1025
.instrument(span.clone())
1026
.await?;
1027
Ok(())
1028
}
1029
/// Comment out identifiers that collide when mapped to snake_case, for now; see
1030
/// https://github.com/WebAssembly/component-model/issues/118
1031
/// APPLE: func()
1032
/// APPLE-pear-GRAPE: func()
1033
/// apple-PEAR-grape: func()
1034
pub async fn call_is_xml<S: wasmtime::AsContextMut>(
1035
&self,
1036
mut store: S,
1037
) -> wasmtime::Result<()>
1038
where
1039
<S as wasmtime::AsContext>::Data: Send,
1040
{
1041
use tracing::Instrument;
1042
let span = tracing::span!(
1043
tracing::Level::TRACE, "wit-bindgen export", module =
1044
"foo:foo/conventions", function = "is-XML",
1045
);
1046
let callee = unsafe {
1047
wasmtime::component::TypedFunc::<
1048
(),
1049
(),
1050
>::new_unchecked(self.is_xml)
1051
};
1052
let () = callee
1053
.call_async(store.as_context_mut(), ())
1054
.instrument(span.clone())
1055
.await?;
1056
Ok(())
1057
}
1058
pub async fn call_explicit<S: wasmtime::AsContextMut>(
1059
&self,
1060
mut store: S,
1061
) -> wasmtime::Result<()>
1062
where
1063
<S as wasmtime::AsContext>::Data: Send,
1064
{
1065
use tracing::Instrument;
1066
let span = tracing::span!(
1067
tracing::Level::TRACE, "wit-bindgen export", module =
1068
"foo:foo/conventions", function = "explicit",
1069
);
1070
let callee = unsafe {
1071
wasmtime::component::TypedFunc::<
1072
(),
1073
(),
1074
>::new_unchecked(self.explicit)
1075
};
1076
let () = callee
1077
.call_async(store.as_context_mut(), ())
1078
.instrument(span.clone())
1079
.await?;
1080
Ok(())
1081
}
1082
pub async fn call_explicit_kebab<S: wasmtime::AsContextMut>(
1083
&self,
1084
mut store: S,
1085
) -> wasmtime::Result<()>
1086
where
1087
<S as wasmtime::AsContext>::Data: Send,
1088
{
1089
use tracing::Instrument;
1090
let span = tracing::span!(
1091
tracing::Level::TRACE, "wit-bindgen export", module =
1092
"foo:foo/conventions", function = "explicit-kebab",
1093
);
1094
let callee = unsafe {
1095
wasmtime::component::TypedFunc::<
1096
(),
1097
(),
1098
>::new_unchecked(self.explicit_kebab)
1099
};
1100
let () = callee
1101
.call_async(store.as_context_mut(), ())
1102
.instrument(span.clone())
1103
.await?;
1104
Ok(())
1105
}
1106
/// Identifiers with the same name as keywords are quoted.
1107
pub async fn call_bool<S: wasmtime::AsContextMut>(
1108
&self,
1109
mut store: S,
1110
) -> wasmtime::Result<()>
1111
where
1112
<S as wasmtime::AsContext>::Data: Send,
1113
{
1114
use tracing::Instrument;
1115
let span = tracing::span!(
1116
tracing::Level::TRACE, "wit-bindgen export", module =
1117
"foo:foo/conventions", function = "bool",
1118
);
1119
let callee = unsafe {
1120
wasmtime::component::TypedFunc::<
1121
(),
1122
(),
1123
>::new_unchecked(self.bool)
1124
};
1125
let () = callee
1126
.call_async(store.as_context_mut(), ())
1127
.instrument(span.clone())
1128
.await?;
1129
Ok(())
1130
}
1131
}
1132
}
1133
}
1134
}
1135
}
1136
1137