-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
115 lines (95 loc) · 3.02 KB
/
main.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
use anyhow::anyhow;
use wasmtime::component::*;
use wasmtime::{Config, Engine, Store};
//TODO: Change resources from hashmap to allow for more than one implementation of the same resource type
bindgen!({
path: "./example2.wit",
resources: {
"scalars": ImplScalars
},
resource_mode: Object,
});
#[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,
//TODO: Add proc macro to automatically implement ResourceTable and add table to store state
scalars_table: std::collections::HashMap<u32, ImplScalars>,
}
#[derive(Debug, Default)]
pub struct ImplScalars;
impl crate::example2::component::backend::Scalars for ImplScalars {
fn new() -> wasmtime::Result<Self>
where
Self: Sized,
{
Ok(ImplScalars)
}
fn method_scalars_get_b(&self) -> wasmtime::Result<u32>
where
Self: Sized,
{
Ok(5)
}
}
impl wasmtime::component::ResourceTable<ImplScalars> for State {
fn get_resource(
&self,
handle: wasmtime::component::Resource<ImplScalars>,
) -> wasmtime::Result<&ImplScalars> {
Ok(self
.scalars_table
.get(&handle.rep())
.ok_or(anyhow!("No resource with id `{}`", handle.rep()))?)
}
fn new_resource(
&mut self,
resource: ImplScalars,
) -> wasmtime::Result<wasmtime::component::Resource<ImplScalars>> {
let handle =
wasmtime::component::Resource::<ImplScalars>::new_own(self.scalars_table.len() as u32);
self.scalars_table.insert(handle.rep(), resource);
Ok(handle)
}
fn get_resource_mut(
&mut self,
handle: wasmtime::component::Resource<ImplScalars>,
) -> wasmtime::Result<&mut ImplScalars> {
Ok(self
.scalars_table
.get_mut(&handle.rep())
.ok_or(anyhow!("No resource with id `{}`", handle.rep()))?)
}
fn drop_resource(&mut self, rep: u32) -> wasmtime::Result<()> {
self.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 mut store = Store::new(&engine, State::default());
crate::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 test = bindings
.example2_component_front()
.call_handle(&mut store, ImplScalars)
.unwrap();
dbg!(test);
Ok(())
}