Skip to content

Commit 3ed3078

Browse files
committed
WIP: bindgen GetHost
1 parent 7a979cd commit 3ed3078

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5611
-959
lines changed

crates/component-macro/tests/expanded/char.rs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
pub struct TheWorld {
22
interface0: exports::foo::foo::chars::Guest,
33
}
4+
pub trait TheWorldGetHost<T>: Send + Sync + 'static {
5+
fn get_host<'a>(&self, data: &'a mut T) -> impl foo::foo::chars::Host;
6+
}
7+
impl<T, U, F> TheWorldGetHost<T> for F
8+
where
9+
U: foo::foo::chars::Host,
10+
F: Fn(&mut T) -> &mut U + Send + Sync + 'static,
11+
{
12+
fn get_host<'a>(&self, data: &'a mut T) -> impl foo::foo::chars::Host {
13+
self(data)
14+
}
15+
}
416
const _: () = {
517
#[allow(unused_imports)]
618
use wasmtime::component::__internal::anyhow;
719
impl TheWorld {
20+
pub fn add_to_linker_get_host<T>(
21+
linker: &mut wasmtime::component::Linker<T>,
22+
host_getter: impl TheWorldGetHost<T>,
23+
) -> wasmtime::Result<()> {
24+
foo::foo::chars::add_to_linker_get_host(linker, host_getter)?;
25+
Ok(())
26+
}
827
pub fn add_to_linker<T, U>(
928
linker: &mut wasmtime::component::Linker<T>,
1029
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
1130
) -> wasmtime::Result<()>
1231
where
1332
U: foo::foo::chars::Host,
1433
{
15-
foo::foo::chars::add_to_linker(linker, get)?;
16-
Ok(())
34+
Self::add_to_linker_get_host(linker, get)
1735
}
1836
/// Instantiates the provided `module` using the specified
1937
/// parameters, wrapping up the result in a structure that
@@ -77,35 +95,63 @@ pub mod foo {
7795
/// A function that returns a character
7896
fn return_char(&mut self) -> char;
7997
}
80-
pub fn add_to_linker<T, U>(
98+
pub trait GetHost<T>: Send + Sync + 'static {
99+
fn get_host<'a>(&self, data: &'a mut T) -> impl Host;
100+
}
101+
pub fn add_to_linker_get_host<T>(
81102
linker: &mut wasmtime::component::Linker<T>,
82-
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
83-
) -> wasmtime::Result<()>
84-
where
85-
U: Host,
86-
{
103+
host_getter: impl GetHost<T>,
104+
) -> wasmtime::Result<()> {
87105
let mut inst = linker.instance("foo:foo/chars")?;
88106
inst.func_wrap(
89107
"take-char",
90108
move |
91109
mut caller: wasmtime::StoreContextMut<'_, T>,
92110
(arg0,): (char,)|
93111
{
94-
let host = get(caller.data_mut());
112+
let host = &mut host_getter.get_host(caller.data_mut());
95113
let r = Host::take_char(host, arg0);
96114
Ok(r)
97115
},
98116
)?;
99117
inst.func_wrap(
100118
"return-char",
101119
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
102-
let host = get(caller.data_mut());
120+
let host = &mut host_getter.get_host(caller.data_mut());
103121
let r = Host::return_char(host);
104122
Ok((r,))
105123
},
106124
)?;
107125
Ok(())
108126
}
127+
impl<T, U, F> GetHost<T> for F
128+
where
129+
U: Host,
130+
F: Fn(&mut T) -> &mut U + Send + Sync + 'static,
131+
{
132+
fn get_host<'a>(&self, data: &'a mut T) -> impl Host {
133+
self(data)
134+
}
135+
}
136+
pub fn add_to_linker<T, U>(
137+
linker: &mut wasmtime::component::Linker<T>,
138+
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
139+
) -> wasmtime::Result<()>
140+
where
141+
U: Host,
142+
{
143+
add_to_linker_get_host(linker, get)
144+
}
145+
impl<T: Host + ?Sized> Host for &mut T {
146+
/// A function that accepts a character
147+
fn take_char(&mut self, x: char) -> () {
148+
Host::take_char(&mut (*self), x)
149+
}
150+
/// A function that returns a character
151+
fn return_char(&mut self) -> char {
152+
Host::return_char(&mut (*self))
153+
}
154+
}
109155
}
110156
}
111157
}

crates/component-macro/tests/expanded/char_async.rs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
pub struct TheWorld {
22
interface0: exports::foo::foo::chars::Guest,
33
}
4+
pub trait TheWorldGetHost<T>: Send + Sync + 'static {
5+
fn get_host<'a>(&self, data: &'a mut T) -> impl foo::foo::chars::Host + Send;
6+
}
7+
impl<T, U, F> TheWorldGetHost<T> for F
8+
where
9+
U: foo::foo::chars::Host + Send,
10+
F: Fn(&mut T) -> &mut U + Send + Sync + 'static,
11+
{
12+
fn get_host<'a>(&self, data: &'a mut T) -> impl foo::foo::chars::Host + Send {
13+
self(data)
14+
}
15+
}
416
const _: () = {
517
#[allow(unused_imports)]
618
use wasmtime::component::__internal::anyhow;
719
impl TheWorld {
20+
pub fn add_to_linker_get_host<T>(
21+
linker: &mut wasmtime::component::Linker<T>,
22+
host_getter: impl TheWorldGetHost<T>,
23+
) -> wasmtime::Result<()>
24+
where
25+
T: Send,
26+
{
27+
foo::foo::chars::add_to_linker_get_host(linker, host_getter)?;
28+
Ok(())
29+
}
830
pub fn add_to_linker<T, U>(
931
linker: &mut wasmtime::component::Linker<T>,
1032
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
1133
) -> wasmtime::Result<()>
1234
where
13-
U: foo::foo::chars::Host + Send,
1435
T: Send,
36+
U: foo::foo::chars::Host + Send,
1537
{
16-
foo::foo::chars::add_to_linker(linker, get)?;
17-
Ok(())
38+
Self::add_to_linker_get_host(linker, get)
1839
}
1940
/// Instantiates the provided `module` using the specified
2041
/// parameters, wrapping up the result in a structure that
@@ -79,13 +100,15 @@ pub mod foo {
79100
/// A function that returns a character
80101
async fn return_char(&mut self) -> char;
81102
}
82-
pub fn add_to_linker<T, U>(
103+
pub trait GetHost<T>: Send + Sync + 'static {
104+
fn get_host<'a>(&self, data: &'a mut T) -> impl Host;
105+
}
106+
pub fn add_to_linker_get_host<T>(
83107
linker: &mut wasmtime::component::Linker<T>,
84-
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
108+
host_getter: impl GetHost<T>,
85109
) -> wasmtime::Result<()>
86110
where
87111
T: Send,
88-
U: Host + Send,
89112
{
90113
let mut inst = linker.instance("foo:foo/chars")?;
91114
inst.func_wrap_async(
@@ -94,21 +117,52 @@ pub mod foo {
94117
mut caller: wasmtime::StoreContextMut<'_, T>,
95118
(arg0,): (char,)|
96119
wasmtime::component::__internal::Box::new(async move {
97-
let host = get(caller.data_mut());
120+
let host = &mut host_getter.get_host(caller.data_mut());
98121
let r = Host::take_char(host, arg0).await;
99122
Ok(r)
100123
}),
101124
)?;
102125
inst.func_wrap_async(
103126
"return-char",
104127
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| wasmtime::component::__internal::Box::new(async move {
105-
let host = get(caller.data_mut());
128+
let host = &mut host_getter.get_host(caller.data_mut());
106129
let r = Host::return_char(host).await;
107130
Ok((r,))
108131
}),
109132
)?;
110133
Ok(())
111134
}
135+
impl<T, U, F> GetHost<T> for F
136+
where
137+
U: Host + Send,
138+
T: Send,
139+
F: Fn(&mut T) -> &mut U + Send + Sync + 'static,
140+
{
141+
fn get_host<'a>(&self, data: &'a mut T) -> impl Host {
142+
self(data)
143+
}
144+
}
145+
pub fn add_to_linker<T, U>(
146+
linker: &mut wasmtime::component::Linker<T>,
147+
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
148+
) -> wasmtime::Result<()>
149+
where
150+
U: Host + Send,
151+
T: Send,
152+
{
153+
add_to_linker_get_host(linker, get)
154+
}
155+
#[wasmtime::component::__internal::async_trait]
156+
impl<T: Host + ?Sized + Send> Host for &mut T {
157+
/// A function that accepts a character
158+
async fn take_char(&mut self, x: char) -> () {
159+
Host::take_char(&mut (*self), x).await
160+
}
161+
/// A function that returns a character
162+
async fn return_char(&mut self) -> char {
163+
Host::return_char(&mut (*self)).await
164+
}
165+
}
112166
}
113167
}
114168
}

0 commit comments

Comments
 (0)