-
Notifications
You must be signed in to change notification settings - Fork 1
/
main copy 2.rs
283 lines (253 loc) · 10.1 KB
/
main copy 2.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
extern crate std;
use anyhow::anyhow;
use wasmtime::{component::*, AsContextMut};
use wasmtime::{Config, Engine, Store};
use crate::example2::component::backend::Scalars;
pub struct Example2 {
interface0: exports::example2::component::front::Front,
}
const _: () = {
impl Example2 {
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: example2::component::backend::Host,
T: example2::component::backend::Scalars + 'static,
{
example2::component::backend::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 fn instantiate<T>(
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(&mut store, component)?;
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 fn instantiate_pre<T>(
mut store: impl wasmtime::AsContextMut<Data = T>,
instance_pre: &wasmtime::component::InstancePre<T>,
) -> wasmtime::Result<(Self, wasmtime::component::Instance)> {
let instance = instance_pre.instantiate(&mut store)?;
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::example2::component::front::Front::new(
&mut __exports
.instance("example2:component/front@0.1.0")
.ok_or_else(|| {
::anyhow::__private::must_use({
let error = ::anyhow::__private::format_err(format_args!(
"exported instance `example2:component/front@0.1.0` not present"
));
error
})
})?,
)?;
Ok(Example2 { interface0 })
}
pub fn example2_component_front(&self) -> &exports::example2::component::front::Front {
&self.interface0
}
}
};
pub mod example2 {
pub mod component {
#[allow(clippy::all)]
pub mod backend {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
pub trait Scalars {
type Resource;
fn new(
store: &mut wasmtime::StoreContextMut<'_, Self>,
) -> wasmtime::Result<wasmtime::component::Resource<Self::Resource>>
where
Self: Sized;
fn method_scalars_get_b(
&self,
self_: wasmtime::component::Resource<Self::Resource>,
) -> wasmtime::Result<u32>
where
Self: Sized;
fn drop(
store: wasmtime::StoreContextMut<'_, Self>,
rep: u32,
) -> wasmtime::Result<()>
where
Self: Sized;
}
pub trait Host {
fn fetch(&mut self, url: String) -> wasmtime::Result<Vec<u8>>;
}
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: Scalars + 'static,
U: Host,
{
let mut inst = linker.instance("example2:component/backend@0.1.0")?;
inst.resource::<T::Resource>("scalars", |store, rep| -> wasmtime::Result<()> {
T::drop(store, rep)
})?;
inst.func_wrap(
"[constructor]scalars",
move |mut caller: wasmtime::StoreContextMut<'_, _>, (): ()| {
let handle = T::new(&mut caller)?;
Ok((handle,))
},
)?;
inst.func_wrap(
"[method]scalars.get-b",
move |mut caller: wasmtime::StoreContextMut<'_, _>,
(arg0,): (wasmtime::component::Resource<T::Resource>,)| {
let r = caller.data_mut().method_scalars_get_b(arg0);
Ok((r?,))
},
)?;
inst.func_wrap(
"fetch",
move |mut caller: wasmtime::StoreContextMut<'_, _>, (arg0,): (String,)| {
let host = get(caller.data_mut());
let r = host.fetch(arg0);
Ok((r?,))
},
)?;
Ok(())
}
}
}
}
pub mod exports {
pub mod example2 {
pub mod component {
#[allow(clippy::all)]
pub mod front {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
pub struct Front {
handle: wasmtime::component::Func,
}
impl Front {
pub fn new(
__exports: &mut wasmtime::component::ExportInstance<'_, '_>,
) -> wasmtime::Result<Front> {
let handle = *__exports
.typed_func::<(wasmtime::component::ResourceAny,), (u32,)>("handle")?
.func();
Ok(Front { handle })
}
pub fn call_handle<R0: 'static, S: wasmtime::AsContextMut>(
&self,
mut store: S,
arg0: wasmtime::component::Resource<R0>,
) -> wasmtime::Result<u32> {
let callee = unsafe {
wasmtime::component::TypedFunc::<
(wasmtime::component::Resource<R0>,),
(u32,),
>::new_unchecked(self.handle)
};
let (ret0,) = callee.call(store.as_context_mut(), (arg0,))?;
callee.post_return(store.as_context_mut())?;
Ok(ret0)
}
}
}
}
}
}
const _: &str = "package example2:component@0.1.0\r\n\r\ninterface backend {\r\n resource scalars {\r\n constructor()\r\n get-b: func() -> u32\r\n\r\n }\r\n\r\n fetch: func(url: string) -> list<u8>\r\n}\r\n\r\ninterface front {\r\n
use backend.{scalars}\r\n\r\n handle: func(x: own<scalars>) -> u32\r\n}\r\n\r\nworld example2 {\r\n import backend\r\n
export front\r\n}";
// USER IMPLEMENTED FROM HERE
#[derive(Debug, Default)]
pub struct Host;
impl crate::example2::component::backend::Host for Host {
fn fetch(&mut self, _url: String) -> wasmtime::Result<Vec<u8>> {
Ok(Vec::new())
}
}
#[derive(Debug, Default)]
pub struct State {
host: Host,
scalars_table: std::collections::HashMap<u32, ImplScalars>,
}
#[derive(Debug)]
pub struct ImplScalars;
impl crate::example2::component::backend::Scalars for State {
type Resource = ImplScalars;
fn new(
store: &mut wasmtime::StoreContextMut<'_, Self>,
) -> wasmtime::Result<wasmtime::component::Resource<Self::Resource>>
where
Self: Sized,
{
let state = store.data_mut();
let handle =
wasmtime::component::Resource::<ImplScalars>::new_own(state.scalars_table.len() as u32);
state.scalars_table.insert(handle.rep(), ImplScalars);
Ok(handle)
}
fn method_scalars_get_b(
&self,
_self_: wasmtime::component::Resource<Self::Resource>,
) -> wasmtime::Result<u32>
where
Self: Sized,
{
Ok(5)
}
fn drop(mut store: wasmtime::StoreContextMut<'_, Self>, rep: u32) -> wasmtime::Result<()> {
let state = store.data_mut();
state.scalars_table.remove(&rep).ok_or(anyhow!(
"tried to drop a resource `{}` that doesn't exist",
rep
))?;
Ok(())
}
}
#[tokio::main]
async fn main() -> wasmtime::Result<()> {
let mut config = Config::new();
config.wasm_component_model(true);
let engine = Engine::new(&config)?;
let component = Component::from_file(&engine, "./example_guest_import.wasm").unwrap();
let mut linker = Linker::new(&engine);
let state = State::default();
let mut store = Store::new(&engine, state);
example2::component::backend::add_to_linker::<State, Host>(&mut linker, |s| &mut s.host)?;
let (bindings, _instance) = Example2::instantiate(&mut store, &component, &linker).unwrap();
let resource = State::new(&mut store.as_context_mut())?;
let test = bindings
.example2_component_front()
.call_handle(&mut store, resource)
.unwrap();
dbg!(test);
Ok(())
}