Browse Source

switch preview2 add_to_linker to use AsHost, parent AsWasi trait impls all of them

pch/resource_table_2
Pat Hickey 11 months ago
parent
commit
0c4568ccfe
  1. 161
      crates/wasi/src/preview2/command.rs
  2. 10
      crates/wasi/src/preview2/ctx.rs

161
crates/wasi/src/preview2/command.rs

@ -26,42 +26,86 @@ wasmtime::component::bindgen!({
},
});
pub fn add_to_linker<T: Send>(
pub trait AsWasi: Send + 'static {
fn as_wasi(&mut self) -> WasiView<'_>;
}
macro_rules! impl_as_wasi {
($p:path) => {
impl<T: AsWasi> $p for T {
type Ctx<'a> = WasiView<'a> where T: 'a;
fn as_host(&mut self) -> WasiView<'_> {
self.as_wasi()
}
}
};
}
impl_as_wasi!(crate::preview2::bindings::clocks::wall_clock::AsHost);
impl_as_wasi!(crate::preview2::bindings::clocks::monotonic_clock::AsHost);
impl_as_wasi!(crate::preview2::bindings::filesystem::types::AsHost);
impl_as_wasi!(crate::preview2::bindings::filesystem::preopens::AsHost);
impl_as_wasi!(crate::preview2::bindings::io::error::AsHost);
impl_as_wasi!(crate::preview2::bindings::io::poll::AsHost);
impl_as_wasi!(crate::preview2::bindings::io::streams::AsHost);
impl_as_wasi!(crate::preview2::bindings::random::random::AsHost);
impl_as_wasi!(crate::preview2::bindings::random::insecure::AsHost);
impl_as_wasi!(crate::preview2::bindings::random::insecure_seed::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::exit::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::environment::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::stdin::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::stdout::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::stderr::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::terminal_input::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::terminal_output::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::terminal_stdin::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::terminal_stdout::AsHost);
impl_as_wasi!(crate::preview2::bindings::cli::terminal_stderr::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::tcp::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::tcp_create_socket::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::udp::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::udp_create_socket::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::instance_network::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::network::AsHost);
impl_as_wasi!(crate::preview2::bindings::sockets::ip_name_lookup::AsHost);
impl_as_wasi!(crate::preview2::bindings::sync_io::filesystem::types::AsHost);
impl_as_wasi!(crate::preview2::bindings::sync_io::io::poll::AsHost);
impl_as_wasi!(crate::preview2::bindings::sync_io::io::streams::AsHost);
pub fn add_to_linker<T: AsWasi + Send>(
l: &mut wasmtime::component::Linker<T>,
f: impl Fn(&mut T) -> WasiView + Send + Sync + Copy,
) -> anyhow::Result<()> {
crate::preview2::bindings::clocks::wall_clock::add_to_linker(l, f)?;
crate::preview2::bindings::clocks::monotonic_clock::add_to_linker(l, f)?;
crate::preview2::bindings::filesystem::types::add_to_linker(l, f)?;
crate::preview2::bindings::filesystem::preopens::add_to_linker(l, f)?;
crate::preview2::bindings::io::error::add_to_linker(l, f)?;
crate::preview2::bindings::io::poll::add_to_linker(l, f)?;
crate::preview2::bindings::io::streams::add_to_linker(l, f)?;
crate::preview2::bindings::random::random::add_to_linker(l, f)?;
crate::preview2::bindings::random::insecure::add_to_linker(l, f)?;
crate::preview2::bindings::random::insecure_seed::add_to_linker(l, f)?;
crate::preview2::bindings::cli::exit::add_to_linker(l, f)?;
crate::preview2::bindings::cli::environment::add_to_linker(l, f)?;
crate::preview2::bindings::cli::stdin::add_to_linker(l, f)?;
crate::preview2::bindings::cli::stdout::add_to_linker(l, f)?;
crate::preview2::bindings::cli::stderr::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_input::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_output::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_stdin::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_stdout::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_stderr::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::tcp::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::tcp_create_socket::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::udp::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::udp_create_socket::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::instance_network::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::network::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::ip_name_lookup::add_to_linker(l, f)?;
crate::preview2::bindings::clocks::wall_clock::add_to_linker(l)?;
crate::preview2::bindings::clocks::monotonic_clock::add_to_linker(l)?;
crate::preview2::bindings::filesystem::types::add_to_linker(l)?;
crate::preview2::bindings::filesystem::preopens::add_to_linker(l)?;
crate::preview2::bindings::io::error::add_to_linker(l)?;
crate::preview2::bindings::io::poll::add_to_linker(l)?;
crate::preview2::bindings::io::streams::add_to_linker(l)?;
crate::preview2::bindings::random::random::add_to_linker(l)?;
crate::preview2::bindings::random::insecure::add_to_linker(l)?;
crate::preview2::bindings::random::insecure_seed::add_to_linker(l)?;
crate::preview2::bindings::cli::exit::add_to_linker(l)?;
crate::preview2::bindings::cli::environment::add_to_linker(l)?;
crate::preview2::bindings::cli::stdin::add_to_linker(l)?;
crate::preview2::bindings::cli::stdout::add_to_linker(l)?;
crate::preview2::bindings::cli::stderr::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_input::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_output::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_stdin::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_stdout::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_stderr::add_to_linker(l)?;
crate::preview2::bindings::sockets::tcp::add_to_linker(l)?;
crate::preview2::bindings::sockets::tcp_create_socket::add_to_linker(l)?;
crate::preview2::bindings::sockets::udp::add_to_linker(l)?;
crate::preview2::bindings::sockets::udp_create_socket::add_to_linker(l)?;
crate::preview2::bindings::sockets::instance_network::add_to_linker(l)?;
crate::preview2::bindings::sockets::network::add_to_linker(l)?;
crate::preview2::bindings::sockets::ip_name_lookup::add_to_linker(l)?;
Ok(())
}
pub mod sync {
use crate::preview2::WasiView;
wasmtime::component::bindgen!({
world: "wasi:cli/command",
@ -90,37 +134,36 @@ pub mod sync {
},
});
pub fn add_to_linker<T: Send>(
pub fn add_to_linker<T: super::AsWasi + Send>(
l: &mut wasmtime::component::Linker<T>,
f: impl Fn(&mut T) -> WasiView + Send + Sync + Copy,
) -> anyhow::Result<()> {
crate::preview2::bindings::clocks::wall_clock::add_to_linker(l, f)?;
crate::preview2::bindings::clocks::monotonic_clock::add_to_linker(l, f)?;
crate::preview2::bindings::sync_io::filesystem::types::add_to_linker(l, f)?;
crate::preview2::bindings::filesystem::preopens::add_to_linker(l, f)?;
crate::preview2::bindings::io::error::add_to_linker(l, f)?;
crate::preview2::bindings::sync_io::io::poll::add_to_linker(l, f)?;
crate::preview2::bindings::sync_io::io::streams::add_to_linker(l, f)?;
crate::preview2::bindings::random::random::add_to_linker(l, f)?;
crate::preview2::bindings::random::insecure::add_to_linker(l, f)?;
crate::preview2::bindings::random::insecure_seed::add_to_linker(l, f)?;
crate::preview2::bindings::cli::exit::add_to_linker(l, f)?;
crate::preview2::bindings::cli::environment::add_to_linker(l, f)?;
crate::preview2::bindings::cli::stdin::add_to_linker(l, f)?;
crate::preview2::bindings::cli::stdout::add_to_linker(l, f)?;
crate::preview2::bindings::cli::stderr::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_input::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_output::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_stdin::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_stdout::add_to_linker(l, f)?;
crate::preview2::bindings::cli::terminal_stderr::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::tcp::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::tcp_create_socket::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::udp::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::udp_create_socket::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::instance_network::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::network::add_to_linker(l, f)?;
crate::preview2::bindings::sockets::ip_name_lookup::add_to_linker(l, f)?;
crate::preview2::bindings::clocks::wall_clock::add_to_linker(l)?;
crate::preview2::bindings::clocks::monotonic_clock::add_to_linker(l)?;
crate::preview2::bindings::sync_io::filesystem::types::add_to_linker(l)?;
crate::preview2::bindings::filesystem::preopens::add_to_linker(l)?;
crate::preview2::bindings::io::error::add_to_linker(l)?;
crate::preview2::bindings::sync_io::io::poll::add_to_linker(l)?;
crate::preview2::bindings::sync_io::io::streams::add_to_linker(l)?;
crate::preview2::bindings::random::random::add_to_linker(l)?;
crate::preview2::bindings::random::insecure::add_to_linker(l)?;
crate::preview2::bindings::random::insecure_seed::add_to_linker(l)?;
crate::preview2::bindings::cli::exit::add_to_linker(l)?;
crate::preview2::bindings::cli::environment::add_to_linker(l)?;
crate::preview2::bindings::cli::stdin::add_to_linker(l)?;
crate::preview2::bindings::cli::stdout::add_to_linker(l)?;
crate::preview2::bindings::cli::stderr::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_input::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_output::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_stdin::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_stdout::add_to_linker(l)?;
crate::preview2::bindings::cli::terminal_stderr::add_to_linker(l)?;
crate::preview2::bindings::sockets::tcp::add_to_linker(l)?;
crate::preview2::bindings::sockets::tcp_create_socket::add_to_linker(l)?;
crate::preview2::bindings::sockets::udp::add_to_linker(l)?;
crate::preview2::bindings::sockets::udp_create_socket::add_to_linker(l)?;
crate::preview2::bindings::sockets::instance_network::add_to_linker(l)?;
crate::preview2::bindings::sockets::network::add_to_linker(l)?;
crate::preview2::bindings::sockets::ip_name_lookup::add_to_linker(l)?;
Ok(())
}
}

10
crates/wasi/src/preview2/ctx.rs

@ -302,11 +302,6 @@ impl WasiCtxBuilder {
}
}
pub struct WasiView<'a> {
pub table: &'a mut ResourceTable,
pub ctx: &'a mut WasiCtx,
}
pub struct WasiCtx {
pub(crate) random: Box<dyn RngCore + Send + Sync>,
pub(crate) insecure_random: Box<dyn RngCore + Send + Sync>,
@ -322,3 +317,8 @@ pub struct WasiCtx {
pub(crate) pool: Pool,
pub(crate) allow_ip_name_lookup: bool,
}
pub struct WasiView<'a> {
pub table: &'a mut ResourceTable,
pub ctx: &'a mut WasiCtx,
}

Loading…
Cancel
Save