Skip to content

Commit

Permalink
feat: allow construction of guest-defined resources
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
  • Loading branch information
rvolosatovs committed Dec 14, 2023
1 parent 3b055d4 commit 6269937
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 114 deletions.
8 changes: 5 additions & 3 deletions crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub trait WasiHttpView: Send {
std::time::Duration::from_millis(600 * 1000),
);
let incoming_req = HostIncomingRequest::new(self, parts, Some(body));
Ok(self.table().push(incoming_req)?)
Ok(self.table().push_host(incoming_req)?)
}

fn new_response_outparam(
Expand All @@ -56,7 +56,7 @@ pub trait WasiHttpView: Send {
Result<hyper::Response<HyperOutgoingBody>, types::ErrorCode>,
>,
) -> wasmtime::Result<Resource<HostResponseOutparam>> {
let id = self.table().push(HostResponseOutparam { result })?;
let id = self.table().push_host(HostResponseOutparam { result })?;
Ok(id)
}

Expand Down Expand Up @@ -134,7 +134,9 @@ pub fn default_send_request(
Ok(resp)
});

let fut = view.table().push(HostFutureIncomingResponse::new(handle))?;
let fut = view
.table()
.push_host(HostFutureIncomingResponse::new(handle))?;

Ok(fut)
}
Expand Down
38 changes: 19 additions & 19 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {
fn new(&mut self) -> wasmtime::Result<Resource<HostFields>> {
let id = self
.table()
.push(HostFields::Owned {
.push_host(HostFields::Owned {
fields: hyper::HeaderMap::new(),
})
.context("[new_fields] pushing fields")?;
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {

let id = self
.table()
.push(HostFields::Owned { fields })
.push_host(HostFields::Owned { fields })
.context("[new_fields] pushing fields")?;

Ok(Ok(id))
Expand Down Expand Up @@ -269,7 +269,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {

let id = self
.table()
.push(HostFields::Owned { fields })
.push_host(HostFields::Owned { fields })
.context("[fields_clone] pushing fields")?;

Ok(id)
Expand Down Expand Up @@ -329,7 +329,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostIncomingRequest for T {
.headers
}

let headers = self.table().push_child(
let headers = self.table().push_host_child(
HostFields::Ref {
parent: id.rep(),
get_fields,
Expand All @@ -347,7 +347,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostIncomingRequest for T {
let req = self.table().get_mut(&id)?;
match req.body.take() {
Some(body) => {
let id = self.table().push(body)?;
let id = self.table().push_host(body)?;
Ok(Ok(id))
}

Expand All @@ -369,7 +369,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingRequest for T {
let headers = move_fields(self.table(), headers)?;

self.table()
.push(HostOutgoingRequest {
.push_host(HostOutgoingRequest {
path_with_query: None,
authority: None,
method: types::Method::Get,
Expand Down Expand Up @@ -404,7 +404,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingRequest for T {

// The output stream will necessarily outlive the request, because we could be still
// writing to the stream after `outgoing-handler.handle` is called.
let outgoing_body = self.table().push(host_body)?;
let outgoing_body = self.table().push_host(host_body)?;

Ok(Ok(outgoing_body))
}
Expand Down Expand Up @@ -535,7 +535,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingRequest for T {
.headers
}

let id = self.table().push_child(
let id = self.table().push_host_child(
HostFields::Ref {
parent: request.rep(),
get_fields,
Expand Down Expand Up @@ -600,7 +600,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostIncomingResponse for T {
&mut elem.downcast_mut::<HostIncomingResponse>().unwrap().headers
}

let id = self.table().push_child(
let id = self.table().push_host_child(
HostFields::Ref {
parent: response.rep(),
get_fields,
Expand All @@ -622,7 +622,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostIncomingResponse for T {

match r.body.take() {
Some(body) => {
let id = self.table().push(body)?;
let id = self.table().push_host(body)?;
Ok(Ok(id))
}

Expand Down Expand Up @@ -672,7 +672,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFutureTrailers for T {

remove_forbidden_headers(self, &mut fields);

let ts = self.table().push(HostFields::Owned { fields })?;
let ts = self.table().push_host(HostFields::Owned { fields })?;

Ok(Some(Ok(Ok(Some(ts)))))
}
Expand All @@ -687,7 +687,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostIncomingBody for T {

if let Some(stream) = body.take_stream() {
let stream = InputStream::Host(Box::new(stream));
let stream = self.table().push_child(stream, &id)?;
let stream = self.table().push_host_child(stream, &id)?;
return Ok(Ok(stream));
}

Expand All @@ -699,7 +699,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostIncomingBody for T {
id: Resource<HostIncomingBody>,
) -> wasmtime::Result<Resource<HostFutureTrailers>> {
let body = self.table().delete(id)?;
let trailers = self.table().push(body.into_future_trailers())?;
let trailers = self.table().push_host(body.into_future_trailers())?;
Ok(trailers)
}

Expand All @@ -716,7 +716,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingResponse for T {
) -> wasmtime::Result<Resource<HostOutgoingResponse>> {
let fields = move_fields(self.table(), headers)?;

let id = self.table().push(HostOutgoingResponse {
let id = self.table().push_host(HostOutgoingResponse {
status: http::StatusCode::OK,
headers: fields,
body: None,
Expand Down Expand Up @@ -744,7 +744,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingResponse for T {

resp.body.replace(body);

let id = self.table().push(host)?;
let id = self.table().push_host(host)?;

Ok(Ok(id))
}
Expand Down Expand Up @@ -783,7 +783,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingResponse for T {
&mut resp.headers
}

Ok(self.table().push_child(
Ok(self.table().push_host_child(
HostFields::Ref {
parent: id.rep(),
get_fields,
Expand Down Expand Up @@ -834,7 +834,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFutureIncomingResponse f

remove_forbidden_headers(self, &mut parts.headers);

let resp = self.table().push(HostIncomingResponse {
let resp = self.table().push_host(HostIncomingResponse {
status: parts.status.as_u16(),
headers: parts.headers,
body: Some({
Expand Down Expand Up @@ -863,7 +863,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingBody for T {
) -> wasmtime::Result<Result<Resource<OutputStream>, ()>> {
let body = self.table().get_mut(&id)?;
if let Some(stream) = body.body_output_stream.take() {
let id = self.table().push_child(stream, &id)?;
let id = self.table().push_host_child(stream, &id)?;
Ok(Ok(id))
} else {
Ok(Err(()))
Expand Down Expand Up @@ -894,7 +894,7 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostOutgoingBody for T {

impl<T: WasiHttpView> crate::bindings::http::types::HostRequestOptions for T {
fn new(&mut self) -> wasmtime::Result<Resource<types::RequestOptions>> {
let id = self.table().push(types::RequestOptions::default())?;
let id = self.table().push_host(types::RequestOptions::default())?;
Ok(id)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-http/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async fn do_wasi_http_hash_all(override_send_request: bool) -> Result<()> {
});
Ok(view
.table()
.push(HostFutureIncomingResponse::Ready(response))?)
.push_host(HostFutureIncomingResponse::Ready(response))?)
},
) as RequestSender)
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi/src/preview2/host/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ fn subscribe_to_duration(
duration: tokio::time::Duration,
) -> anyhow::Result<Resource<Pollable>> {
let sleep = if duration.is_zero() {
table.push(Deadline::Past)?
table.push_host(Deadline::Past)?
} else if let Some(deadline) = tokio::time::Instant::now().checked_add(duration) {
// NB: this resource created here is not actually exposed to wasm, it's
// only an internal implementation detail used to match the signature
// expected by `subscribe`.
table.push(Deadline::Instant(deadline))?
table.push_host(Deadline::Instant(deadline))?
} else {
// If the user specifies a time so far in the future we can't
// represent it, wait forever rather than trap.
table.push(Deadline::Never)?
table.push_host(Deadline::Never)?
};
subscribe(table, sleep)
}
Expand Down
16 changes: 8 additions & 8 deletions crates/wasi/src/preview2/host/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T: WasiView> preopens::Host for T {
for (dir, name) in self.ctx().preopens.clone() {
let fd = self
.table_mut()
.push(Descriptor::Dir(dir))
.push_host(Descriptor::Dir(dir))
.with_context(|| format!("failed to push preopen {name}"))?;
results.push((fd, name));
}
Expand Down Expand Up @@ -333,7 +333,7 @@ impl<T: WasiView> HostDescriptor for T {
Err(ReaddirError::Io(e)) => Err(e.into()),
Err(ReaddirError::IllegalSequence) => Err(ErrorCode::IllegalByteSequence.into()),
});
Ok(table.push(ReaddirIterator::new(entries))?)
Ok(table.push_host(ReaddirIterator::new(entries))?)
}

async fn sync(&mut self, fd: Resource<types::Descriptor>) -> FsResult<()> {
Expand Down Expand Up @@ -583,10 +583,10 @@ impl<T: WasiView> HostDescriptor for T {

match opened {
OpenResult::Dir(dir) => {
Ok(table.push(Descriptor::Dir(Dir::new(dir, d.perms, d.file_perms)))?)
Ok(table.push_host(Descriptor::Dir(Dir::new(dir, d.perms, d.file_perms)))?)
}

OpenResult::File(file) => Ok(table.push(Descriptor::File(File::new(
OpenResult::File(file) => Ok(table.push_host(Descriptor::File(File::new(
file,
mask_file_perms(d.file_perms, flags),
)))?),
Expand Down Expand Up @@ -713,7 +713,7 @@ impl<T: WasiView> HostDescriptor for T {
let reader = FileInputStream::new(clone, offset);

// Insert the stream view into the table. Trap if the table is full.
let index = self.table_mut().push(InputStream::File(reader))?;
let index = self.table_mut().push_host(InputStream::File(reader))?;

Ok(index)
}
Expand All @@ -738,7 +738,7 @@ impl<T: WasiView> HostDescriptor for T {
let writer: OutputStream = Box::new(writer);

// Insert the stream view into the table. Trap if the table is full.
let index = self.table_mut().push(writer)?;
let index = self.table_mut().push_host(writer)?;

Ok(index)
}
Expand All @@ -761,7 +761,7 @@ impl<T: WasiView> HostDescriptor for T {
let appender: OutputStream = Box::new(appender);

// Insert the stream view into the table. Trap if the table is full.
let index = self.table_mut().push(appender)?;
let index = self.table_mut().push_host(appender)?;

Ok(index)
}
Expand Down Expand Up @@ -1061,7 +1061,7 @@ mod test {
fn table_readdir_works() {
let mut table = ResourceTable::new();
let ix = table
.push(ReaddirIterator::new(std::iter::empty()))
.push_host(ReaddirIterator::new(std::iter::empty()))
.unwrap();
let _ = table.get(&ix).unwrap();
table.delete(ix).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/host/instance_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<T: WasiView> instance_network::Host for T {
socket_addr_check: self.ctx().socket_addr_check.clone(),
allow_ip_name_lookup: self.ctx().allowed_network_uses.ip_name_lookup,
};
let network = self.table_mut().push(network)?;
let network = self.table_mut().push_host(network)?;
Ok(network)
}
}
6 changes: 3 additions & 3 deletions crates/wasi/src/preview2/host/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<T: WasiView> streams::Host for T {
match err {
StreamError::Closed => Ok(streams::StreamError::Closed),
StreamError::LastOperationFailed(e) => Ok(streams::StreamError::LastOperationFailed(
self.table_mut().push(e)?,
self.table_mut().push_host(e)?,
)),
StreamError::Trap(e) => Err(e),
}
Expand Down Expand Up @@ -303,13 +303,13 @@ pub mod sync {
fn flush(&mut self, stream: Resource<OutputStream>) -> StreamResult<()> {
Ok(AsyncHostOutputStream::flush(
self,
Resource::new_borrow(stream.rep()),
Resource::new_host_borrow(stream.rep()),
)?)
}

fn blocking_flush(&mut self, stream: Resource<OutputStream>) -> StreamResult<()> {
in_tokio(async {
AsyncHostOutputStream::blocking_flush(self, Resource::new_borrow(stream.rep()))
AsyncHostOutputStream::blocking_flush(self, Resource::new_host_borrow(stream.rep()))
.await
})
}
Expand Down
10 changes: 5 additions & 5 deletions crates/wasi/src/preview2/host/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ impl<T: WasiView> crate::preview2::host::tcp::tcp::HostTcpSocket for T {

socket.tcp_state = TcpState::Connected;
let (input, output) = socket.as_split();
let input_stream = self.table_mut().push_child(input, &this)?;
let output_stream = self.table_mut().push_child(output, &this)?;
let input_stream = self.table_mut().push_host_child(input, &this)?;
let output_stream = self.table_mut().push_host_child(output, &this)?;

Ok((input_stream, output_stream))
}
Expand Down Expand Up @@ -288,9 +288,9 @@ impl<T: WasiView> crate::preview2::host::tcp::tcp::HostTcpSocket for T {
let (input, output) = tcp_socket.as_split();
let output: OutputStream = output;

let tcp_socket = self.table_mut().push(tcp_socket)?;
let input_stream = self.table_mut().push_child(input, &tcp_socket)?;
let output_stream = self.table_mut().push_child(output, &tcp_socket)?;
let tcp_socket = self.table_mut().push_host(tcp_socket)?;
let input_stream = self.table_mut().push_host_child(input, &tcp_socket)?;
let output_stream = self.table_mut().push_host_child(output, &tcp_socket)?;

Ok((tcp_socket, input_stream, output_stream))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/host/tcp_create_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<T: WasiView> tcp_create_socket::Host for T {
address_family: IpAddressFamily,
) -> SocketResult<Resource<TcpSocket>> {
let socket = TcpSocket::new(address_family.into())?;
let socket = self.table_mut().push(socket)?;
let socket = self.table_mut().push_host(socket)?;
Ok(socket)
}
}
4 changes: 2 additions & 2 deletions crates/wasi/src/preview2/host/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ impl<T: WasiView> udp::HostUdpSocket for T {
};

Ok((
self.table_mut().push_child(incoming_stream, &this)?,
self.table_mut().push_child(outgoing_stream, &this)?,
self.table_mut().push_host_child(incoming_stream, &this)?,
self.table_mut().push_host_child(outgoing_stream, &this)?,
))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/host/udp_create_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<T: WasiView> udp_create_socket::Host for T {
address_family: IpAddressFamily,
) -> SocketResult<Resource<UdpSocket>> {
let socket = UdpSocket::new(address_family.into())?;
let socket = self.table_mut().push(socket)?;
let socket = self.table_mut().push_host(socket)?;
Ok(socket)
}
}
4 changes: 3 additions & 1 deletion crates/wasi/src/preview2/ip_name_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ impl<T: WasiView> Host for T {
}

let task = spawn_blocking(move || blocking_resolve(&host));
let resource = self.table_mut().push(ResolveAddressStream::Waiting(task))?;
let resource = self
.table_mut()
.push_host(ResolveAddressStream::Waiting(task))?;
Ok(resource)
}
}
Expand Down
Loading

0 comments on commit 6269937

Please sign in to comment.