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