Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/floats.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,
179
for<'a> D::Data<'a>: foo::foo::floats::Host,
180
T: 'static,
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 {}
197
impl<_T: ?Sized> HostWithStore for _T
198
where
199
_T: wasmtime::component::HasData,
200
{}
201
pub trait Host {
202
fn f32_param(&mut self, x: f32) -> ();
203
fn f64_param(&mut self, x: f64) -> ();
204
fn f32_result(&mut self) -> f32;
205
fn f64_result(&mut self) -> f64;
206
}
207
impl<_T: Host + ?Sized> Host for &mut _T {
208
fn f32_param(&mut self, x: f32) -> () {
209
Host::f32_param(*self, x)
210
}
211
fn f64_param(&mut self, x: f64) -> () {
212
Host::f64_param(*self, x)
213
}
214
fn f32_result(&mut self) -> f32 {
215
Host::f32_result(*self)
216
}
217
fn f64_result(&mut self) -> f64 {
218
Host::f64_result(*self)
219
}
220
}
221
pub fn add_to_linker<T, D>(
222
linker: &mut wasmtime::component::Linker<T>,
223
host_getter: fn(&mut T) -> D::Data<'_>,
224
) -> wasmtime::Result<()>
225
where
226
D: HostWithStore,
227
for<'a> D::Data<'a>: Host,
228
T: 'static,
229
{
230
let mut inst = linker.instance("foo:foo/floats")?;
231
inst.func_wrap(
232
"f32-param",
233
move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f32,)| {
234
let host = &mut host_getter(caller.data_mut());
235
let r = Host::f32_param(host, arg0);
236
Ok(r)
237
},
238
)?;
239
inst.func_wrap(
240
"f64-param",
241
move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f64,)| {
242
let host = &mut host_getter(caller.data_mut());
243
let r = Host::f64_param(host, arg0);
244
Ok(r)
245
},
246
)?;
247
inst.func_wrap(
248
"f32-result",
249
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
250
let host = &mut host_getter(caller.data_mut());
251
let r = Host::f32_result(host);
252
Ok((r,))
253
},
254
)?;
255
inst.func_wrap(
256
"f64-result",
257
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
258
let host = &mut host_getter(caller.data_mut());
259
let r = Host::f64_result(host);
260
Ok((r,))
261
},
262
)?;
263
Ok(())
264
}
265
}
266
}
267
}
268
pub mod exports {
269
pub mod foo {
270
pub mod foo {
271
#[allow(clippy::all)]
272
pub mod floats {
273
#[allow(unused_imports)]
274
use wasmtime::component::__internal::{anyhow, Box};
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
anyhow::anyhow!(
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
anyhow::anyhow!(
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 fn call_f32_param<S: wasmtime::AsContextMut>(
361
&self,
362
mut store: S,
363
arg0: f32,
364
) -> wasmtime::Result<()> {
365
let callee = unsafe {
366
wasmtime::component::TypedFunc::<
367
(f32,),
368
(),
369
>::new_unchecked(self.f32_param)
370
};
371
let () = callee.call(store.as_context_mut(), (arg0,))?;
372
callee.post_return(store.as_context_mut())?;
373
Ok(())
374
}
375
pub fn call_f64_param<S: wasmtime::AsContextMut>(
376
&self,
377
mut store: S,
378
arg0: f64,
379
) -> wasmtime::Result<()> {
380
let callee = unsafe {
381
wasmtime::component::TypedFunc::<
382
(f64,),
383
(),
384
>::new_unchecked(self.f64_param)
385
};
386
let () = callee.call(store.as_context_mut(), (arg0,))?;
387
callee.post_return(store.as_context_mut())?;
388
Ok(())
389
}
390
pub fn call_f32_result<S: wasmtime::AsContextMut>(
391
&self,
392
mut store: S,
393
) -> wasmtime::Result<f32> {
394
let callee = unsafe {
395
wasmtime::component::TypedFunc::<
396
(),
397
(f32,),
398
>::new_unchecked(self.f32_result)
399
};
400
let (ret0,) = callee.call(store.as_context_mut(), ())?;
401
callee.post_return(store.as_context_mut())?;
402
Ok(ret0)
403
}
404
pub fn call_f64_result<S: wasmtime::AsContextMut>(
405
&self,
406
mut store: S,
407
) -> wasmtime::Result<f64> {
408
let callee = unsafe {
409
wasmtime::component::TypedFunc::<
410
(),
411
(f64,),
412
>::new_unchecked(self.f64_result)
413
};
414
let (ret0,) = callee.call(store.as_context_mut(), ())?;
415
callee.post_return(store.as_context_mut())?;
416
Ok(ret0)
417
}
418
}
419
}
420
}
421
}
422
}
423
424