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