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