Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/floats_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::floats::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::floats::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::floats::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::floats::HostWithStore + Send,
179
for<'a> D::Data<'a>: foo::foo::floats::Host + Send,
180
T: 'static + Send,
181
{
182
foo::foo::floats::add_to_linker::<T, D>(linker, host_getter)?;
183
Ok(())
184
}
185
pub fn foo_foo_floats(&self) -> &exports::foo::foo::floats::Guest {
186
&self.interface0
187
}
188
}
189
};
190
pub mod foo {
191
pub mod foo {
192
#[allow(clippy::all)]
193
pub mod floats {
194
#[allow(unused_imports)]
195
use wasmtime::component::__internal::{anyhow, Box};
196
pub trait HostWithStore: wasmtime::component::HasData + Send {}
197
impl<_T: ?Sized> HostWithStore for _T
198
where
199
_T: wasmtime::component::HasData + Send,
200
{}
201
pub trait Host: Send {
202
fn f32_param(
203
&mut self,
204
x: f32,
205
) -> impl ::core::future::Future<Output = ()> + Send;
206
fn f64_param(
207
&mut self,
208
x: f64,
209
) -> impl ::core::future::Future<Output = ()> + Send;
210
fn f32_result(
211
&mut self,
212
) -> impl ::core::future::Future<Output = f32> + Send;
213
fn f64_result(
214
&mut self,
215
) -> impl ::core::future::Future<Output = f64> + Send;
216
}
217
impl<_T: Host + ?Sized + Send> Host for &mut _T {
218
fn f32_param(
219
&mut self,
220
x: f32,
221
) -> impl ::core::future::Future<Output = ()> + Send {
222
async move { Host::f32_param(*self, x).await }
223
}
224
fn f64_param(
225
&mut self,
226
x: f64,
227
) -> impl ::core::future::Future<Output = ()> + Send {
228
async move { Host::f64_param(*self, x).await }
229
}
230
fn f32_result(
231
&mut self,
232
) -> impl ::core::future::Future<Output = f32> + Send {
233
async move { Host::f32_result(*self).await }
234
}
235
fn f64_result(
236
&mut self,
237
) -> impl ::core::future::Future<Output = f64> + Send {
238
async move { Host::f64_result(*self).await }
239
}
240
}
241
pub fn add_to_linker<T, D>(
242
linker: &mut wasmtime::component::Linker<T>,
243
host_getter: fn(&mut T) -> D::Data<'_>,
244
) -> wasmtime::Result<()>
245
where
246
D: HostWithStore,
247
for<'a> D::Data<'a>: Host,
248
T: 'static + Send,
249
{
250
let mut inst = linker.instance("foo:foo/floats")?;
251
inst.func_wrap_async(
252
"f32-param",
253
move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f32,)| {
254
use tracing::Instrument;
255
let span = tracing::span!(
256
tracing::Level::TRACE, "wit-bindgen import", module =
257
"floats", function = "f32-param",
258
);
259
wasmtime::component::__internal::Box::new(
260
async move {
261
tracing::event!(
262
tracing::Level::TRACE, x = tracing::field::debug(& arg0),
263
"call"
264
);
265
let host = &mut host_getter(caller.data_mut());
266
let r = Host::f32_param(host, arg0).await;
267
tracing::event!(
268
tracing::Level::TRACE, result = tracing::field::debug(& r),
269
"return"
270
);
271
Ok(r)
272
}
273
.instrument(span),
274
)
275
},
276
)?;
277
inst.func_wrap_async(
278
"f64-param",
279
move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f64,)| {
280
use tracing::Instrument;
281
let span = tracing::span!(
282
tracing::Level::TRACE, "wit-bindgen import", module =
283
"floats", function = "f64-param",
284
);
285
wasmtime::component::__internal::Box::new(
286
async move {
287
tracing::event!(
288
tracing::Level::TRACE, x = tracing::field::debug(& arg0),
289
"call"
290
);
291
let host = &mut host_getter(caller.data_mut());
292
let r = Host::f64_param(host, arg0).await;
293
tracing::event!(
294
tracing::Level::TRACE, result = tracing::field::debug(& r),
295
"return"
296
);
297
Ok(r)
298
}
299
.instrument(span),
300
)
301
},
302
)?;
303
inst.func_wrap_async(
304
"f32-result",
305
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
306
use tracing::Instrument;
307
let span = tracing::span!(
308
tracing::Level::TRACE, "wit-bindgen import", module =
309
"floats", function = "f32-result",
310
);
311
wasmtime::component::__internal::Box::new(
312
async move {
313
tracing::event!(tracing::Level::TRACE, "call");
314
let host = &mut host_getter(caller.data_mut());
315
let r = Host::f32_result(host).await;
316
tracing::event!(
317
tracing::Level::TRACE, result = tracing::field::debug(& r),
318
"return"
319
);
320
Ok((r,))
321
}
322
.instrument(span),
323
)
324
},
325
)?;
326
inst.func_wrap_async(
327
"f64-result",
328
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
329
use tracing::Instrument;
330
let span = tracing::span!(
331
tracing::Level::TRACE, "wit-bindgen import", module =
332
"floats", function = "f64-result",
333
);
334
wasmtime::component::__internal::Box::new(
335
async move {
336
tracing::event!(tracing::Level::TRACE, "call");
337
let host = &mut host_getter(caller.data_mut());
338
let r = Host::f64_result(host).await;
339
tracing::event!(
340
tracing::Level::TRACE, result = tracing::field::debug(& r),
341
"return"
342
);
343
Ok((r,))
344
}
345
.instrument(span),
346
)
347
},
348
)?;
349
Ok(())
350
}
351
}
352
}
353
}
354
pub mod exports {
355
pub mod foo {
356
pub mod foo {
357
#[allow(clippy::all)]
358
pub mod floats {
359
#[allow(unused_imports)]
360
use wasmtime::component::__internal::{anyhow, Box};
361
pub struct Guest {
362
f32_param: wasmtime::component::Func,
363
f64_param: wasmtime::component::Func,
364
f32_result: wasmtime::component::Func,
365
f64_result: wasmtime::component::Func,
366
}
367
#[derive(Clone)]
368
pub struct GuestIndices {
369
f32_param: wasmtime::component::ComponentExportIndex,
370
f64_param: wasmtime::component::ComponentExportIndex,
371
f32_result: wasmtime::component::ComponentExportIndex,
372
f64_result: wasmtime::component::ComponentExportIndex,
373
}
374
impl GuestIndices {
375
/// Constructor for [`GuestIndices`] which takes a
376
/// [`Component`](wasmtime::component::Component) as input and can be executed
377
/// before instantiation.
378
///
379
/// This constructor can be used to front-load string lookups to find exports
380
/// within a component.
381
pub fn new<_T>(
382
_instance_pre: &wasmtime::component::InstancePre<_T>,
383
) -> wasmtime::Result<GuestIndices> {
384
let instance = _instance_pre
385
.component()
386
.get_export_index(None, "foo:foo/floats")
387
.ok_or_else(|| {
388
anyhow::anyhow!(
389
"no exported instance named `foo:foo/floats`"
390
)
391
})?;
392
let mut lookup = move |name| {
393
_instance_pre
394
.component()
395
.get_export_index(Some(&instance), name)
396
.ok_or_else(|| {
397
anyhow::anyhow!(
398
"instance export `foo:foo/floats` does \
399
not have export `{name}`"
400
)
401
})
402
};
403
let _ = &mut lookup;
404
let f32_param = lookup("f32-param")?;
405
let f64_param = lookup("f64-param")?;
406
let f32_result = lookup("f32-result")?;
407
let f64_result = lookup("f64-result")?;
408
Ok(GuestIndices {
409
f32_param,
410
f64_param,
411
f32_result,
412
f64_result,
413
})
414
}
415
pub fn load(
416
&self,
417
mut store: impl wasmtime::AsContextMut,
418
instance: &wasmtime::component::Instance,
419
) -> wasmtime::Result<Guest> {
420
let _instance = instance;
421
let _instance_pre = _instance.instance_pre(&store);
422
let _instance_type = _instance_pre.instance_type();
423
let mut store = store.as_context_mut();
424
let _ = &mut store;
425
let f32_param = *_instance
426
.get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)?
427
.func();
428
let f64_param = *_instance
429
.get_typed_func::<(f64,), ()>(&mut store, &self.f64_param)?
430
.func();
431
let f32_result = *_instance
432
.get_typed_func::<(), (f32,)>(&mut store, &self.f32_result)?
433
.func();
434
let f64_result = *_instance
435
.get_typed_func::<(), (f64,)>(&mut store, &self.f64_result)?
436
.func();
437
Ok(Guest {
438
f32_param,
439
f64_param,
440
f32_result,
441
f64_result,
442
})
443
}
444
}
445
impl Guest {
446
pub async fn call_f32_param<S: wasmtime::AsContextMut>(
447
&self,
448
mut store: S,
449
arg0: f32,
450
) -> wasmtime::Result<()>
451
where
452
<S as wasmtime::AsContext>::Data: Send,
453
{
454
use tracing::Instrument;
455
let span = tracing::span!(
456
tracing::Level::TRACE, "wit-bindgen export", module =
457
"foo:foo/floats", function = "f32-param",
458
);
459
let callee = unsafe {
460
wasmtime::component::TypedFunc::<
461
(f32,),
462
(),
463
>::new_unchecked(self.f32_param)
464
};
465
let () = callee
466
.call_async(store.as_context_mut(), (arg0,))
467
.instrument(span.clone())
468
.await?;
469
callee
470
.post_return_async(store.as_context_mut())
471
.instrument(span)
472
.await?;
473
Ok(())
474
}
475
pub async fn call_f64_param<S: wasmtime::AsContextMut>(
476
&self,
477
mut store: S,
478
arg0: f64,
479
) -> wasmtime::Result<()>
480
where
481
<S as wasmtime::AsContext>::Data: Send,
482
{
483
use tracing::Instrument;
484
let span = tracing::span!(
485
tracing::Level::TRACE, "wit-bindgen export", module =
486
"foo:foo/floats", function = "f64-param",
487
);
488
let callee = unsafe {
489
wasmtime::component::TypedFunc::<
490
(f64,),
491
(),
492
>::new_unchecked(self.f64_param)
493
};
494
let () = callee
495
.call_async(store.as_context_mut(), (arg0,))
496
.instrument(span.clone())
497
.await?;
498
callee
499
.post_return_async(store.as_context_mut())
500
.instrument(span)
501
.await?;
502
Ok(())
503
}
504
pub async fn call_f32_result<S: wasmtime::AsContextMut>(
505
&self,
506
mut store: S,
507
) -> wasmtime::Result<f32>
508
where
509
<S as wasmtime::AsContext>::Data: Send,
510
{
511
use tracing::Instrument;
512
let span = tracing::span!(
513
tracing::Level::TRACE, "wit-bindgen export", module =
514
"foo:foo/floats", function = "f32-result",
515
);
516
let callee = unsafe {
517
wasmtime::component::TypedFunc::<
518
(),
519
(f32,),
520
>::new_unchecked(self.f32_result)
521
};
522
let (ret0,) = callee
523
.call_async(store.as_context_mut(), ())
524
.instrument(span.clone())
525
.await?;
526
callee
527
.post_return_async(store.as_context_mut())
528
.instrument(span)
529
.await?;
530
Ok(ret0)
531
}
532
pub async fn call_f64_result<S: wasmtime::AsContextMut>(
533
&self,
534
mut store: S,
535
) -> wasmtime::Result<f64>
536
where
537
<S as wasmtime::AsContext>::Data: Send,
538
{
539
use tracing::Instrument;
540
let span = tracing::span!(
541
tracing::Level::TRACE, "wit-bindgen export", module =
542
"foo:foo/floats", function = "f64-result",
543
);
544
let callee = unsafe {
545
wasmtime::component::TypedFunc::<
546
(),
547
(f64,),
548
>::new_unchecked(self.f64_result)
549
};
550
let (ret0,) = callee
551
.call_async(store.as_context_mut(), ())
552
.instrument(span.clone())
553
.await?;
554
callee
555
.post_return_async(store.as_context_mut())
556
.instrument(span)
557
.await?;
558
Ok(ret0)
559
}
560
}
561
}
562
}
563
}
564
}
565
566