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