Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Mar 22, 2024
1 parent 668e816 commit 8494d71
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 209 deletions.
16 changes: 11 additions & 5 deletions crates/wasi-http/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ wasmtime::component::bindgen!({

pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView,
{
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
Expand Down Expand Up @@ -54,11 +54,17 @@ pub mod sync {

pub fn add_to_linker<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
where
T: WasiHttpView + wasmtime_wasi::WasiView + bindings::http::types::Host,
T: WasiHttpView + wasmtime_wasi::WasiView,
{
// TODO: this shouldn't be required, but the adapter unconditionally pulls in all of these
// dependencies.
wasmtime_wasi::command::sync::add_to_linker(l)?;
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::sync_io::io::poll::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::sync_io::io::streams::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::io::error::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stdin::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stdout::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::cli::stderr::add_to_linker(l, |t| t)?;
wasmtime_wasi::bindings::random::random::add_to_linker(l, |t| t)?;

add_only_http_to_linker(l)?;

Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-http/tests/all/async_.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::command::Command;
use wasmtime_wasi::bindings::Command;

foreach_http!(assert_test_exists);

Expand All @@ -13,7 +13,7 @@ async fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi::command::add_to_linker(&mut linker)?;
wasmtime_wasi::add_async_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::add_only_http_to_linker(&mut linker)?;
let (command, _instance) = Command::instantiate_async(&mut store, &component, &linker).await?;
let result = command.wasi_cli_run().call_run(&mut store).await?;
Expand Down
5 changes: 3 additions & 2 deletions crates/wasi-http/tests/all/sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use test_programs_artifacts::*;
use wasmtime_wasi::command::sync::Command;
use wasmtime_wasi::bindings::sync_io::Command;

foreach_http!(assert_test_exists);

Expand All @@ -12,7 +12,8 @@ fn run(path: &str, server: &Server) -> Result<()> {
let component = Component::from_file(&engine, path)?;
let mut store = store(&engine, server);
let mut linker = Linker::new(&engine);
wasmtime_wasi_http::proxy::sync::add_to_linker(&mut linker)?;
wasmtime_wasi::add_sync_to_linker(&mut linker)?;
wasmtime_wasi_http::proxy::sync::add_only_http_to_linker(&mut linker)?;
let (command, _instance) = Command::instantiate(&mut store, &component, &linker)?;
let result = command.wasi_cli_run().call_run(&mut store)?;
result.map_err(|()| anyhow::anyhow!("run returned an error"))
Expand Down
193 changes: 98 additions & 95 deletions crates/wasi/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ pub mod sync_io {

wasmtime::component::bindgen!({
path: "wit",
interfaces: "
import wasi:io/poll@0.2.0;
import wasi:io/streams@0.2.0;
import wasi:filesystem/types@0.2.0;
",
world: "wasi:cli/command",
tracing: true,
trappable_error_type: {
"wasi:io/streams/stream-error" => StreamError,
"wasi:filesystem/types/error-code" => FsError,
},
with: {
// This interface comes from the outer module, as it's
// These interfaces comes from the outer module, as it's
// sync/async agnostic.
"wasi:clocks": crate::bindings::clocks,
"wasi:random": crate::bindings::random,
"wasi:sockets": crate::bindings::sockets,
"wasi:cli": crate::bindings::cli,
"wasi:io/error": crate::bindings::io::error,

// Configure the resource types of the bound interfaces here
// to be the same as the async versions of the resources, that
Expand All @@ -31,99 +31,102 @@ pub mod sync_io {
"wasi:io/poll/pollable": super::super::io::poll::Pollable,
"wasi:io/streams/input-stream": super::super::io::streams::InputStream,
"wasi:io/streams/output-stream": super::super::io::streams::OutputStream,
"wasi:io/error/error": super::super::io::error::Error,
}
});
}
pub use self::_internal::wasi::{filesystem, io};
pub use self::_internal::Command;
}

wasmtime::component::bindgen!({
path: "wit",
world: "wasi:cli/imports",
tracing: true,
async: {
// Only these functions are `async` and everything else is sync
// meaning that it basically doesn't need to block. These functions
// are the only ones that need to block.
//
// Note that at this time `only_imports` works on function names
// which in theory can be shared across interfaces, so this may
// need fancier syntax in the future.
only_imports: [
"[method]descriptor.access-at",
"[method]descriptor.advise",
"[method]descriptor.change-directory-permissions-at",
"[method]descriptor.change-file-permissions-at",
"[method]descriptor.create-directory-at",
"[method]descriptor.get-flags",
"[method]descriptor.get-type",
"[method]descriptor.is-same-object",
"[method]descriptor.link-at",
"[method]descriptor.lock-exclusive",
"[method]descriptor.lock-shared",
"[method]descriptor.metadata-hash",
"[method]descriptor.metadata-hash-at",
"[method]descriptor.open-at",
"[method]descriptor.read",
"[method]descriptor.read-directory",
"[method]descriptor.readlink-at",
"[method]descriptor.remove-directory-at",
"[method]descriptor.rename-at",
"[method]descriptor.set-size",
"[method]descriptor.set-times",
"[method]descriptor.set-times-at",
"[method]descriptor.stat",
"[method]descriptor.stat-at",
"[method]descriptor.symlink-at",
"[method]descriptor.sync",
"[method]descriptor.sync-data",
"[method]descriptor.try-lock-exclusive",
"[method]descriptor.try-lock-shared",
"[method]descriptor.unlink-file-at",
"[method]descriptor.unlock",
"[method]descriptor.write",
"[method]input-stream.read",
"[method]input-stream.blocking-read",
"[method]input-stream.blocking-skip",
"[method]input-stream.skip",
"[method]output-stream.forward",
"[method]output-stream.splice",
"[method]output-stream.blocking-splice",
"[method]output-stream.blocking-flush",
"[method]output-stream.blocking-write",
"[method]output-stream.blocking-write-and-flush",
"[method]output-stream.blocking-write-zeroes-and-flush",
"[method]directory-entry-stream.read-directory-entry",
"poll",
"[method]pollable.block",
"[method]pollable.ready",
],
},
trappable_error_type: {
"wasi:io/streams/stream-error" => crate::StreamError,
"wasi:filesystem/types/error-code" => crate::FsError,
"wasi:sockets/network/error-code" => crate::SocketError,
},
with: {
// Configure all resources to be concrete types defined in this crate,
// so that way we get to use nice typed helper methods with
// `ResourceTable`.
"wasi:sockets/network/network": super::network::Network,
"wasi:sockets/tcp/tcp-socket": super::tcp::TcpSocket,
"wasi:sockets/udp/udp-socket": super::udp::UdpSocket,
"wasi:sockets/udp/incoming-datagram-stream": super::udp::IncomingDatagramStream,
"wasi:sockets/udp/outgoing-datagram-stream": super::udp::OutgoingDatagramStream,
"wasi:sockets/ip-name-lookup/resolve-address-stream": super::ip_name_lookup::ResolveAddressStream,
"wasi:filesystem/types/directory-entry-stream": super::filesystem::ReaddirIterator,
"wasi:filesystem/types/descriptor": super::filesystem::Descriptor,
"wasi:io/streams/input-stream": super::stream::InputStream,
"wasi:io/streams/output-stream": super::stream::OutputStream,
"wasi:io/error/error": super::stream::Error,
"wasi:io/poll/pollable": super::poll::Pollable,
"wasi:cli/terminal-input/terminal-input": super::stdio::TerminalInput,
"wasi:cli/terminal-output/terminal-output": super::stdio::TerminalOutput,
},
});
mod async_io {
wasmtime::component::bindgen!({
path: "wit",
world: "wasi:cli/command",
tracing: true,
async: {
// Only these functions are `async` and everything else is sync
// meaning that it basically doesn't need to block. These functions
// are the only ones that need to block.
//
// Note that at this time `only_imports` works on function names
// which in theory can be shared across interfaces, so this may
// need fancier syntax in the future.
only_imports: [
"[method]descriptor.access-at",
"[method]descriptor.advise",
"[method]descriptor.change-directory-permissions-at",
"[method]descriptor.change-file-permissions-at",
"[method]descriptor.create-directory-at",
"[method]descriptor.get-flags",
"[method]descriptor.get-type",
"[method]descriptor.is-same-object",
"[method]descriptor.link-at",
"[method]descriptor.lock-exclusive",
"[method]descriptor.lock-shared",
"[method]descriptor.metadata-hash",
"[method]descriptor.metadata-hash-at",
"[method]descriptor.open-at",
"[method]descriptor.read",
"[method]descriptor.read-directory",
"[method]descriptor.readlink-at",
"[method]descriptor.remove-directory-at",
"[method]descriptor.rename-at",
"[method]descriptor.set-size",
"[method]descriptor.set-times",
"[method]descriptor.set-times-at",
"[method]descriptor.stat",
"[method]descriptor.stat-at",
"[method]descriptor.symlink-at",
"[method]descriptor.sync",
"[method]descriptor.sync-data",
"[method]descriptor.try-lock-exclusive",
"[method]descriptor.try-lock-shared",
"[method]descriptor.unlink-file-at",
"[method]descriptor.unlock",
"[method]descriptor.write",
"[method]input-stream.read",
"[method]input-stream.blocking-read",
"[method]input-stream.blocking-skip",
"[method]input-stream.skip",
"[method]output-stream.forward",
"[method]output-stream.splice",
"[method]output-stream.blocking-splice",
"[method]output-stream.blocking-flush",
"[method]output-stream.blocking-write",
"[method]output-stream.blocking-write-and-flush",
"[method]output-stream.blocking-write-zeroes-and-flush",
"[method]directory-entry-stream.read-directory-entry",
"poll",
"[method]pollable.block",
"[method]pollable.ready",
],
},
trappable_error_type: {
"wasi:io/streams/stream-error" => crate::StreamError,
"wasi:filesystem/types/error-code" => crate::FsError,
"wasi:sockets/network/error-code" => crate::SocketError,
},
with: {
// Configure all resources to be concrete types defined in this crate,
// so that way we get to use nice typed helper methods with
// `ResourceTable`.
"wasi:sockets/network/network": crate::network::Network,
"wasi:sockets/tcp/tcp-socket": crate::tcp::TcpSocket,
"wasi:sockets/udp/udp-socket": crate::udp::UdpSocket,
"wasi:sockets/udp/incoming-datagram-stream": crate::udp::IncomingDatagramStream,
"wasi:sockets/udp/outgoing-datagram-stream": crate::udp::OutgoingDatagramStream,
"wasi:sockets/ip-name-lookup/resolve-address-stream": crate::ip_name_lookup::ResolveAddressStream,
"wasi:filesystem/types/directory-entry-stream": crate::filesystem::ReaddirIterator,
"wasi:filesystem/types/descriptor": crate::filesystem::Descriptor,
"wasi:io/streams/input-stream": crate::stream::InputStream,
"wasi:io/streams/output-stream": crate::stream::OutputStream,
"wasi:io/error/error": crate::stream::Error,
"wasi:io/poll/pollable": crate::poll::Pollable,
"wasi:cli/terminal-input/terminal-input": crate::stdio::TerminalInput,
"wasi:cli/terminal-output/terminal-output": crate::stdio::TerminalOutput,
},
});
}

pub use wasi::*;
pub use self::async_io::wasi::*;
pub use self::async_io::Command;
91 changes: 0 additions & 91 deletions crates/wasi/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1 @@
use crate::WasiView;

wasmtime::component::bindgen!({
world: "wasi:cli/command",
tracing: true,
async: true,
with: { "wasi": crate::bindings },
});

pub fn add_to_linker<T: WasiView>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()> {
crate::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
crate::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
crate::bindings::filesystem::types::add_to_linker(l, |t| t)?;
crate::bindings::filesystem::preopens::add_to_linker(l, |t| t)?;
crate::bindings::io::error::add_to_linker(l, |t| t)?;
crate::bindings::io::poll::add_to_linker(l, |t| t)?;
crate::bindings::io::streams::add_to_linker(l, |t| t)?;
crate::bindings::random::random::add_to_linker(l, |t| t)?;
crate::bindings::random::insecure::add_to_linker(l, |t| t)?;
crate::bindings::random::insecure_seed::add_to_linker(l, |t| t)?;
crate::bindings::cli::exit::add_to_linker(l, |t| t)?;
crate::bindings::cli::environment::add_to_linker(l, |t| t)?;
crate::bindings::cli::stdin::add_to_linker(l, |t| t)?;
crate::bindings::cli::stdout::add_to_linker(l, |t| t)?;
crate::bindings::cli::stderr::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_input::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_output::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_stdin::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_stdout::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_stderr::add_to_linker(l, |t| t)?;
crate::bindings::sockets::tcp::add_to_linker(l, |t| t)?;
crate::bindings::sockets::tcp_create_socket::add_to_linker(l, |t| t)?;
crate::bindings::sockets::udp::add_to_linker(l, |t| t)?;
crate::bindings::sockets::udp_create_socket::add_to_linker(l, |t| t)?;
crate::bindings::sockets::instance_network::add_to_linker(l, |t| t)?;
crate::bindings::sockets::network::add_to_linker(l, |t| t)?;
crate::bindings::sockets::ip_name_lookup::add_to_linker(l, |t| t)?;
Ok(())
}

pub mod sync {
use crate::WasiView;

wasmtime::component::bindgen!({
world: "wasi:cli/command",
tracing: true,
async: false,
with: {
// Map interfaces with synchronous funtions to their synchronous
// counterparts...
"wasi:filesystem": crate::bindings::sync_io::filesystem,
"wasi:io": crate::bindings::sync_io::io,

// ... and everything else is not-async and so goes through the
// top-level bindings.
"wasi": crate::bindings
},
});

pub fn add_to_linker<T: WasiView>(
l: &mut wasmtime::component::Linker<T>,
) -> anyhow::Result<()> {
crate::bindings::clocks::wall_clock::add_to_linker(l, |t| t)?;
crate::bindings::clocks::monotonic_clock::add_to_linker(l, |t| t)?;
crate::bindings::sync_io::filesystem::types::add_to_linker(l, |t| t)?;
crate::bindings::filesystem::preopens::add_to_linker(l, |t| t)?;
crate::bindings::io::error::add_to_linker(l, |t| t)?;
crate::bindings::sync_io::io::poll::add_to_linker(l, |t| t)?;
crate::bindings::sync_io::io::streams::add_to_linker(l, |t| t)?;
crate::bindings::random::random::add_to_linker(l, |t| t)?;
crate::bindings::random::insecure::add_to_linker(l, |t| t)?;
crate::bindings::random::insecure_seed::add_to_linker(l, |t| t)?;
crate::bindings::cli::exit::add_to_linker(l, |t| t)?;
crate::bindings::cli::environment::add_to_linker(l, |t| t)?;
crate::bindings::cli::stdin::add_to_linker(l, |t| t)?;
crate::bindings::cli::stdout::add_to_linker(l, |t| t)?;
crate::bindings::cli::stderr::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_input::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_output::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_stdin::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_stdout::add_to_linker(l, |t| t)?;
crate::bindings::cli::terminal_stderr::add_to_linker(l, |t| t)?;
crate::bindings::sockets::tcp::add_to_linker(l, |t| t)?;
crate::bindings::sockets::tcp_create_socket::add_to_linker(l, |t| t)?;
crate::bindings::sockets::udp::add_to_linker(l, |t| t)?;
crate::bindings::sockets::udp_create_socket::add_to_linker(l, |t| t)?;
crate::bindings::sockets::instance_network::add_to_linker(l, |t| t)?;
crate::bindings::sockets::network::add_to_linker(l, |t| t)?;
crate::bindings::sockets::ip_name_lookup::add_to_linker(l, |t| t)?;
Ok(())
}
}
Loading

0 comments on commit 8494d71

Please sign in to comment.