forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
char_async.rs
215 lines (215 loc) · 8.79 KB
/
char_async.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
pub struct TheWorld {
interface0: exports::foo::foo::chars::Guest,
}
const _: () = {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
impl TheWorld {
pub fn add_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> wasmtime::Result<()>
where
T: Send,
U: foo::foo::chars::Host + Send,
{
foo::foo::chars::add_to_linker(linker, get)?;
Ok(())
}
/// Instantiates the provided `module` using the specified
/// parameters, wrapping up the result in a structure that
/// translates between wasm and the host.
pub async fn instantiate_async<T: Send>(
mut store: impl wasmtime::AsContextMut<Data = T>,
component: &wasmtime::component::Component,
linker: &wasmtime::component::Linker<T>,
) -> wasmtime::Result<(Self, wasmtime::component::Instance)> {
let instance = linker.instantiate_async(&mut store, component).await?;
Ok((Self::new(store, &instance)?, instance))
}
/// Instantiates a pre-instantiated module using the specified
/// parameters, wrapping up the result in a structure that
/// translates between wasm and the host.
pub async fn instantiate_pre<T: Send>(
mut store: impl wasmtime::AsContextMut<Data = T>,
instance_pre: &wasmtime::component::InstancePre<T>,
) -> wasmtime::Result<(Self, wasmtime::component::Instance)> {
let instance = instance_pre.instantiate_async(&mut store).await?;
Ok((Self::new(store, &instance)?, instance))
}
/// Low-level creation wrapper for wrapping up the exports
/// of the `instance` provided in this structure of wasm
/// exports.
///
/// This function will extract exports from the `instance`
/// defined within `store` and wrap them all up in the
/// returned structure which can be used to interact with
/// the wasm module.
pub fn new(
mut store: impl wasmtime::AsContextMut,
instance: &wasmtime::component::Instance,
) -> wasmtime::Result<Self> {
let mut store = store.as_context_mut();
let mut exports = instance.exports(&mut store);
let mut __exports = exports.root();
let interface0 = exports::foo::foo::chars::Guest::new(
&mut __exports
.instance("foo:foo/chars")
.ok_or_else(|| {
anyhow::anyhow!("exported instance `foo:foo/chars` not present")
})?,
)?;
Ok(TheWorld { interface0 })
}
pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {
&self.interface0
}
}
};
pub mod foo {
pub mod foo {
#[allow(clippy::all)]
pub mod chars {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
pub trait Host: Send {
/// A function that accepts a character
async fn take_char(&mut self, x: char) -> ();
/// A function that returns a character
async fn return_char(&mut self) -> char;
}
pub trait GetHost<
T,
>: Fn(T) -> <Self as GetHost<T>>::Host + Send + Sync + Copy + 'static {
type Host: Host + Send;
}
impl<F, T, O> GetHost<T> for F
where
F: Fn(T) -> O + Send + Sync + Copy + 'static,
O: Host + Send,
{
type Host = O;
}
pub fn add_to_linker_get_host<T>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: impl for<'a> GetHost<&'a mut T>,
) -> wasmtime::Result<()>
where
T: Send,
{
let mut inst = linker.instance("foo:foo/chars")?;
inst.func_wrap_async(
"take-char",
move |
mut caller: wasmtime::StoreContextMut<'_, T>,
(arg0,): (char,)|
wasmtime::component::__internal::Box::new(async move {
let host = &mut host_getter(caller.data_mut());
let r = Host::take_char(host, arg0).await;
Ok(r)
}),
)?;
inst.func_wrap_async(
"return-char",
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move {
let host = &mut host_getter(caller.data_mut());
let r = Host::return_char(host).await;
Ok((r,))
}),
)?;
Ok(())
}
pub fn add_to_linker<T, U>(
linker: &mut wasmtime::component::Linker<T>,
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
) -> wasmtime::Result<()>
where
U: Host + Send,
T: Send,
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
/// A function that accepts a character
async fn take_char(&mut self, x: char) -> () {
Host::take_char(*self, x).await
}
/// A function that returns a character
async fn return_char(&mut self) -> char {
Host::return_char(*self).await
}
}
}
}
}
pub mod exports {
pub mod foo {
pub mod foo {
#[allow(clippy::all)]
pub mod chars {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
pub struct Guest {
take_char: wasmtime::component::Func,
return_char: wasmtime::component::Func,
}
impl Guest {
pub fn new(
__exports: &mut wasmtime::component::ExportInstance<'_, '_>,
) -> wasmtime::Result<Guest> {
let take_char = *__exports
.typed_func::<(char,), ()>("take-char")?
.func();
let return_char = *__exports
.typed_func::<(), (char,)>("return-char")?
.func();
Ok(Guest { take_char, return_char })
}
/// A function that accepts a character
pub async fn call_take_char<S: wasmtime::AsContextMut>(
&self,
mut store: S,
arg0: char,
) -> wasmtime::Result<()>
where
<S as wasmtime::AsContext>::Data: Send,
{
let callee = unsafe {
wasmtime::component::TypedFunc::<
(char,),
(),
>::new_unchecked(self.take_char)
};
let () = callee
.call_async(store.as_context_mut(), (arg0,))
.await?;
callee.post_return_async(store.as_context_mut()).await?;
Ok(())
}
/// A function that returns a character
pub async fn call_return_char<S: wasmtime::AsContextMut>(
&self,
mut store: S,
) -> wasmtime::Result<char>
where
<S as wasmtime::AsContext>::Data: Send,
{
let callee = unsafe {
wasmtime::component::TypedFunc::<
(),
(char,),
>::new_unchecked(self.return_char)
};
let (ret0,) = callee
.call_async(store.as_context_mut(), ())
.await?;
callee.post_return_async(store.as_context_mut()).await?;
Ok(ret0)
}
}
}
}
}
}