Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/char_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::chars::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::chars::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::chars::GuestIndices::new(_instance_pre)?;
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::chars::HostWithStore + Send,
177
for<'a> D::Data<'a>: foo::foo::chars::Host + Send,
178
T: 'static + Send,
179
{
180
foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;
181
Ok(())
182
}
183
pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {
184
&self.interface0
185
}
186
}
187
};
188
pub mod foo {
189
pub mod foo {
190
#[allow(clippy::all)]
191
pub mod chars {
192
#[allow(unused_imports)]
193
use wasmtime::component::__internal::{anyhow, 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
/// A function that accepts a character
201
fn take_char(
202
&mut self,
203
x: char,
204
) -> impl ::core::future::Future<Output = ()> + Send;
205
/// A function that returns a character
206
fn return_char(
207
&mut self,
208
) -> impl ::core::future::Future<Output = char> + Send;
209
}
210
impl<_T: Host + ?Sized + Send> Host for &mut _T {
211
/// A function that accepts a character
212
fn take_char(
213
&mut self,
214
x: char,
215
) -> impl ::core::future::Future<Output = ()> + Send {
216
async move { Host::take_char(*self, x).await }
217
}
218
/// A function that returns a character
219
fn return_char(
220
&mut self,
221
) -> impl ::core::future::Future<Output = char> + Send {
222
async move { Host::return_char(*self).await }
223
}
224
}
225
pub fn add_to_linker<T, D>(
226
linker: &mut wasmtime::component::Linker<T>,
227
host_getter: fn(&mut T) -> D::Data<'_>,
228
) -> wasmtime::Result<()>
229
where
230
D: HostWithStore,
231
for<'a> D::Data<'a>: Host,
232
T: 'static + Send,
233
{
234
let mut inst = linker.instance("foo:foo/chars")?;
235
inst.func_wrap_async(
236
"take-char",
237
move |
238
mut caller: wasmtime::StoreContextMut<'_, T>,
239
(arg0,): (char,)|
240
{
241
use tracing::Instrument;
242
let span = tracing::span!(
243
tracing::Level::TRACE, "wit-bindgen import", module =
244
"chars", function = "take-char",
245
);
246
wasmtime::component::__internal::Box::new(
247
async move {
248
tracing::event!(
249
tracing::Level::TRACE, x = tracing::field::debug(& arg0),
250
"call"
251
);
252
let host = &mut host_getter(caller.data_mut());
253
let r = Host::take_char(host, arg0).await;
254
tracing::event!(
255
tracing::Level::TRACE, result = tracing::field::debug(& r),
256
"return"
257
);
258
Ok(r)
259
}
260
.instrument(span),
261
)
262
},
263
)?;
264
inst.func_wrap_async(
265
"return-char",
266
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
267
use tracing::Instrument;
268
let span = tracing::span!(
269
tracing::Level::TRACE, "wit-bindgen import", module =
270
"chars", function = "return-char",
271
);
272
wasmtime::component::__internal::Box::new(
273
async move {
274
tracing::event!(tracing::Level::TRACE, "call");
275
let host = &mut host_getter(caller.data_mut());
276
let r = Host::return_char(host).await;
277
tracing::event!(
278
tracing::Level::TRACE, result = tracing::field::debug(& r),
279
"return"
280
);
281
Ok((r,))
282
}
283
.instrument(span),
284
)
285
},
286
)?;
287
Ok(())
288
}
289
}
290
}
291
}
292
pub mod exports {
293
pub mod foo {
294
pub mod foo {
295
#[allow(clippy::all)]
296
pub mod chars {
297
#[allow(unused_imports)]
298
use wasmtime::component::__internal::{anyhow, Box};
299
pub struct Guest {
300
take_char: wasmtime::component::Func,
301
return_char: wasmtime::component::Func,
302
}
303
#[derive(Clone)]
304
pub struct GuestIndices {
305
take_char: wasmtime::component::ComponentExportIndex,
306
return_char: wasmtime::component::ComponentExportIndex,
307
}
308
impl GuestIndices {
309
/// Constructor for [`GuestIndices`] which takes a
310
/// [`Component`](wasmtime::component::Component) as input and can be executed
311
/// before instantiation.
312
///
313
/// This constructor can be used to front-load string lookups to find exports
314
/// within a component.
315
pub fn new<_T>(
316
_instance_pre: &wasmtime::component::InstancePre<_T>,
317
) -> wasmtime::Result<GuestIndices> {
318
let instance = _instance_pre
319
.component()
320
.get_export_index(None, "foo:foo/chars")
321
.ok_or_else(|| {
322
anyhow::anyhow!(
323
"no exported instance named `foo:foo/chars`"
324
)
325
})?;
326
let mut lookup = move |name| {
327
_instance_pre
328
.component()
329
.get_export_index(Some(&instance), name)
330
.ok_or_else(|| {
331
anyhow::anyhow!(
332
"instance export `foo:foo/chars` does \
333
not have export `{name}`"
334
)
335
})
336
};
337
let _ = &mut lookup;
338
let take_char = lookup("take-char")?;
339
let return_char = lookup("return-char")?;
340
Ok(GuestIndices {
341
take_char,
342
return_char,
343
})
344
}
345
pub fn load(
346
&self,
347
mut store: impl wasmtime::AsContextMut,
348
instance: &wasmtime::component::Instance,
349
) -> wasmtime::Result<Guest> {
350
let _instance = instance;
351
let _instance_pre = _instance.instance_pre(&store);
352
let _instance_type = _instance_pre.instance_type();
353
let mut store = store.as_context_mut();
354
let _ = &mut store;
355
let take_char = *_instance
356
.get_typed_func::<(char,), ()>(&mut store, &self.take_char)?
357
.func();
358
let return_char = *_instance
359
.get_typed_func::<
360
(),
361
(char,),
362
>(&mut store, &self.return_char)?
363
.func();
364
Ok(Guest { take_char, return_char })
365
}
366
}
367
impl Guest {
368
/// A function that accepts a character
369
pub async fn call_take_char<S: wasmtime::AsContextMut>(
370
&self,
371
mut store: S,
372
arg0: char,
373
) -> wasmtime::Result<()>
374
where
375
<S as wasmtime::AsContext>::Data: Send,
376
{
377
use tracing::Instrument;
378
let span = tracing::span!(
379
tracing::Level::TRACE, "wit-bindgen export", module =
380
"foo:foo/chars", function = "take-char",
381
);
382
let callee = unsafe {
383
wasmtime::component::TypedFunc::<
384
(char,),
385
(),
386
>::new_unchecked(self.take_char)
387
};
388
let () = callee
389
.call_async(store.as_context_mut(), (arg0,))
390
.instrument(span.clone())
391
.await?;
392
callee
393
.post_return_async(store.as_context_mut())
394
.instrument(span)
395
.await?;
396
Ok(())
397
}
398
/// A function that returns a character
399
pub async fn call_return_char<S: wasmtime::AsContextMut>(
400
&self,
401
mut store: S,
402
) -> wasmtime::Result<char>
403
where
404
<S as wasmtime::AsContext>::Data: Send,
405
{
406
use tracing::Instrument;
407
let span = tracing::span!(
408
tracing::Level::TRACE, "wit-bindgen export", module =
409
"foo:foo/chars", function = "return-char",
410
);
411
let callee = unsafe {
412
wasmtime::component::TypedFunc::<
413
(),
414
(char,),
415
>::new_unchecked(self.return_char)
416
};
417
let (ret0,) = callee
418
.call_async(store.as_context_mut(), ())
419
.instrument(span.clone())
420
.await?;
421
callee
422
.post_return_async(store.as_context_mut())
423
.instrument(span)
424
.await?;
425
Ok(ret0)
426
}
427
}
428
}
429
}
430
}
431
}
432
433