A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Remove some async annotations in bindings generation (#12042)
Alex Crichtonb570b4fc8222
diff --git a/crates/wasi-http/src/p3/bindings.rs b/crates/wasi-http/src/p3/bindings.rsindex b3a2280df5..82c9fb57a3 100644--- a/crates/wasi-http/src/p3/bindings.rs+++ b/crates/wasi-http/src/p3/bindings.rs@@ -6,13 +6,13 @@ mod generated {path: "src/p3/wit",world: "wasi:http/proxy",imports: {- "wasi:http/handler.handle": async | store | trappable | tracing,+ "wasi:http/handler.handle": store | trappable | tracing,"wasi:http/types.[drop]request": store | trappable | tracing,"wasi:http/types.[drop]response": store | trappable | tracing,- "wasi:http/types.[static]request.consume-body": async | store | trappable | tracing,- "wasi:http/types.[static]request.new": async | store | trappable | tracing,- "wasi:http/types.[static]response.consume-body": async | store | trappable | tracing,- "wasi:http/types.[static]response.new": async | store | trappable | tracing,+ "wasi:http/types.[static]request.consume-body": store | trappable | tracing,+ "wasi:http/types.[static]request.new": store | trappable | tracing,+ "wasi:http/types.[static]response.consume-body": store | trappable | tracing,+ "wasi:http/types.[static]response.new": store | trappable | tracing,default: trappable | tracing,},exports: { default: async | store | task_exit },diff --git a/crates/wasi-http/src/p3/host/types.rs b/crates/wasi-http/src/p3/host/types.rsindex d0ea83ba06..c23225a4d1 100644--- a/crates/wasi-http/src/p3/host/types.rs+++ b/crates/wasi-http/src/p3/host/types.rs@@ -14,7 +14,7 @@ use http::header::CONTENT_LENGTH;use std::sync::Arc;use tokio::sync::oneshot;use wasmtime::component::{- Access, Accessor, FutureProducer, FutureReader, Resource, ResourceTable, StreamReader,+ Access, FutureProducer, FutureReader, Resource, ResourceTable, StreamReader,};use wasmtime::{AsContextMut, StoreContextMut};@@ -312,57 +312,55 @@ impl HostFields for WasiHttpCtxView<'_> {}impl HostRequestWithStore for WasiHttp {- async fn new<T>(- store: &Accessor<T, Self>,+ fn new<T>(+ mut store: Access<T, Self>,headers: Resource<Headers>,contents: Option<StreamReader<u8>>,trailers: FutureReader<Result<Option<Resource<Trailers>>, ErrorCode>>,options: Option<Resource<RequestOptions>>,) -> wasmtime::Result<(Resource<Request>, FutureReader<Result<(), ErrorCode>>)> {- store.with(|mut store| {- let (result_tx, result_rx) = oneshot::channel();- let body = match contents- .map(|rx| rx.try_into::<HostBodyStreamProducer<T>>(store.as_context_mut()))- {- Some(Ok(mut producer)) => Body::Host {- body: mem::take(&mut producer.body),- result_tx,- },- Some(Err(rx)) => Body::Guest {- contents_rx: Some(rx),- trailers_rx: trailers,- result_tx,- },- None => Body::Guest {- contents_rx: None,- trailers_rx: trailers,- result_tx,- },- };- let WasiHttpCtxView { table, .. } = store.get();- let headers = delete_fields(table, headers)?;- let options = options- .map(|options| delete_request_options(table, options))- .transpose()?;- let req = Request {- method: http::Method::GET,- scheme: None,- authority: None,- path_with_query: None,- headers: headers.into(),- options: options.map(Into::into),- body,- };- let req = table.push(req).context("failed to push request to table")?;- Ok((- req,- FutureReader::new(&mut store, GuestBodyResultProducer::Receiver(result_rx)),- ))- })- }-- async fn consume_body<T>(- store: &Accessor<T, Self>,+ let (result_tx, result_rx) = oneshot::channel();+ let body = match contents+ .map(|rx| rx.try_into::<HostBodyStreamProducer<T>>(store.as_context_mut()))+ {+ Some(Ok(mut producer)) => Body::Host {+ body: mem::take(&mut producer.body),+ result_tx,+ },+ Some(Err(rx)) => Body::Guest {+ contents_rx: Some(rx),+ trailers_rx: trailers,+ result_tx,+ },+ None => Body::Guest {+ contents_rx: None,+ trailers_rx: trailers,+ result_tx,+ },+ };+ let WasiHttpCtxView { table, .. } = store.get();+ let headers = delete_fields(table, headers)?;+ let options = options+ .map(|options| delete_request_options(table, options))+ .transpose()?;+ let req = Request {+ method: http::Method::GET,+ scheme: None,+ authority: None,+ path_with_query: None,+ headers: headers.into(),+ options: options.map(Into::into),+ body,+ };+ let req = table.push(req).context("failed to push request to table")?;+ Ok((+ req,+ FutureReader::new(&mut store, GuestBodyResultProducer::Receiver(result_rx)),+ ))+ }++ fn consume_body<T>(+ mut store: Access<T, Self>,req: Resource<Request>,fut: FutureReader<Result<(), ErrorCode>>,) -> wasmtime::Result<(@@ -370,14 +368,12 @@ impl HostRequestWithStore for WasiHttp {FutureReader<Result<Option<Resource<Trailers>>, ErrorCode>>,)> {let getter = store.getter();- store.with(|mut store| {- let Request { body, .. } = store- .get()- .table- .delete(req)- .context("failed to delete request from table")?;- Ok(body.consume(store, fut, getter))- })+ let Request { body, .. } = store+ .get()+ .table+ .delete(req)+ .context("failed to delete request from table")?;+ Ok(body.consume(store, fut, getter))}fn drop<T>(mut store: Access<'_, T, Self>, req: Resource<Request>) -> wasmtime::Result<()> {@@ -602,51 +598,49 @@ impl HostRequestOptions for WasiHttpCtxView<'_> {}impl HostResponseWithStore for WasiHttp {- async fn new<T>(- store: &Accessor<T, Self>,+ fn new<T>(+ mut store: Access<T, Self>,headers: Resource<Headers>,contents: Option<StreamReader<u8>>,trailers: FutureReader<Result<Option<Resource<Trailers>>, ErrorCode>>,) -> wasmtime::Result<(Resource<Response>, FutureReader<Result<(), ErrorCode>>)> {- store.with(|mut store| {- let (result_tx, result_rx) = oneshot::channel();- let body = match contents- .map(|rx| rx.try_into::<HostBodyStreamProducer<T>>(store.as_context_mut()))- {- Some(Ok(mut producer)) => Body::Host {- body: mem::take(&mut producer.body),- result_tx,- },- Some(Err(rx)) => Body::Guest {- contents_rx: Some(rx),- trailers_rx: trailers,- result_tx,- },- None => Body::Guest {- contents_rx: None,- trailers_rx: trailers,- result_tx,- },- };- let WasiHttpCtxView { table, .. } = store.get();- let headers = delete_fields(table, headers)?;- let res = Response {- status: http::StatusCode::OK,- headers: headers.into(),- body,- };- let res = table- .push(res)- .context("failed to push response to table")?;- Ok((- res,- FutureReader::new(&mut store, GuestBodyResultProducer::Receiver(result_rx)),- ))- })- }-- async fn consume_body<T>(- store: &Accessor<T, Self>,+ let (result_tx, result_rx) = oneshot::channel();+ let body = match contents+ .map(|rx| rx.try_into::<HostBodyStreamProducer<T>>(store.as_context_mut()))+ {+ Some(Ok(mut producer)) => Body::Host {+ body: mem::take(&mut producer.body),+ result_tx,+ },+ Some(Err(rx)) => Body::Guest {+ contents_rx: Some(rx),+ trailers_rx: trailers,+ result_tx,+ },+ None => Body::Guest {+ contents_rx: None,+ trailers_rx: trailers,+ result_tx,+ },+ };+ let WasiHttpCtxView { table, .. } = store.get();+ let headers = delete_fields(table, headers)?;+ let res = Response {+ status: http::StatusCode::OK,+ headers: headers.into(),+ body,+ };+ let res = table+ .push(res)+ .context("failed to push response to table")?;+ Ok((+ res,+ FutureReader::new(&mut store, GuestBodyResultProducer::Receiver(result_rx)),+ ))+ }++ fn consume_body<T>(+ mut store: Access<T, Self>,res: Resource<Response>,fut: FutureReader<Result<(), ErrorCode>>,) -> wasmtime::Result<(@@ -654,14 +648,12 @@ impl HostResponseWithStore for WasiHttp {FutureReader<Result<Option<Resource<Trailers>>, ErrorCode>>,)> {let getter = store.getter();- store.with(|mut store| {- let Response { body, .. } = store- .get()- .table- .delete(res)- .context("failed to delete response from table")?;- Ok(body.consume(store, fut, getter))- })+ let Response { body, .. } = store+ .get()+ .table+ .delete(res)+ .context("failed to delete response from table")?;+ Ok(body.consume(store, fut, getter))}fn drop<T>(mut store: Access<'_, T, Self>, res: Resource<Response>) -> wasmtime::Result<()> {diff --git a/crates/wasi/src/p3/bindings.rs b/crates/wasi/src/p3/bindings.rsindex 53f0d0d536..4e4af16320 100644--- a/crates/wasi/src/p3/bindings.rs+++ b/crates/wasi/src/p3/bindings.rs@@ -79,10 +79,10 @@ mod generated {path: "src/p3/wit",world: "wasi:cli/command",imports: {- "wasi:cli/stdin": async | store | tracing | trappable,- "wasi:cli/stdout": async | store | tracing | trappable,- "wasi:cli/stderr": async | store | tracing | trappable,- "wasi:filesystem/types.[method]descriptor.read-via-stream": async | store | tracing | trappable,+ "wasi:cli/stdin": store | tracing | trappable,+ "wasi:cli/stdout": store | tracing | trappable,+ "wasi:cli/stderr": store | tracing | trappable,+ "wasi:filesystem/types.[method]descriptor.read-via-stream": store | tracing | trappable,"wasi:sockets/types.[method]tcp-socket.bind": async | store | tracing | trappable,"wasi:sockets/types.[method]tcp-socket.listen": async | store | tracing | trappable,"wasi:sockets/types.[method]tcp-socket.receive": async | store | tracing | trappable,diff --git a/crates/wasi/src/p3/cli/host.rs b/crates/wasi/src/p3/cli/host.rsindex 7ab3a1047e..2b413cc1ee 100644--- a/crates/wasi/src/p3/cli/host.rs+++ b/crates/wasi/src/p3/cli/host.rs@@ -15,7 +15,7 @@ use std::io::{self, Cursor};use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};use tokio::sync::oneshot;use wasmtime::component::{- Accessor, Destination, FutureReader, Resource, Source, StreamConsumer, StreamProducer,+ Access, Accessor, Destination, FutureReader, Resource, Source, StreamConsumer, StreamProducer,StreamReader, StreamResult,};use wasmtime::{AsContextMut as _, StoreContextMut};@@ -193,27 +193,25 @@ impl terminal_stderr::Host for WasiCliCtxView<'_> {}impl stdin::HostWithStore for WasiCli {- async fn read_via_stream<U>(- store: &Accessor<U, Self>,+ fn read_via_stream<U>(+ mut store: Access<U, Self>,) -> wasmtime::Result<(StreamReader<u8>, FutureReader<Result<(), ErrorCode>>)> {- store.with(|mut store| {- let rx = store.get().ctx.stdin.async_stream();- let (result_tx, result_rx) = oneshot::channel();- let stream = StreamReader::new(- &mut store,- InputStreamProducer {- rx: Box::into_pin(rx),- result_tx: Some(result_tx),- },- );- let future = FutureReader::new(&mut store, async {- anyhow::Ok(match result_rx.await {- Ok(err) => Err(err),- Err(_) => Ok(()),- })- });- Ok((stream, future))- })+ let rx = store.get().ctx.stdin.async_stream();+ let (result_tx, result_rx) = oneshot::channel();+ let stream = StreamReader::new(+ &mut store,+ InputStreamProducer {+ rx: Box::into_pin(rx),+ result_tx: Some(result_tx),+ },+ );+ let future = FutureReader::new(&mut store, async {+ anyhow::Ok(match result_rx.await {+ Ok(err) => Err(err),+ Err(_) => Ok(()),+ })+ });+ Ok((stream, future))}}diff --git a/crates/wasi/src/p3/filesystem/host.rs b/crates/wasi/src/p3/filesystem/host.rsindex edeabf6a25..682fffb752 100644--- a/crates/wasi/src/p3/filesystem/host.rs+++ b/crates/wasi/src/p3/filesystem/host.rs@@ -19,7 +19,7 @@ use tokio::sync::{mpsc, oneshot};use tokio::task::{JoinHandle, spawn_blocking};use wasmtime::StoreContextMut;use wasmtime::component::{- Accessor, Destination, FutureReader, Resource, ResourceTable, Source, StreamConsumer,+ Access, Accessor, Destination, FutureReader, Resource, ResourceTable, Source, StreamConsumer,StreamProducer, StreamReader, StreamResult,};@@ -485,37 +485,35 @@ impl types::Host for WasiFilesystemCtxView<'_> {}impl types::HostDescriptorWithStore for WasiFilesystem {- async fn read_via_stream<U>(- store: &Accessor<U, Self>,+ fn read_via_stream<U>(+ mut store: Access<U, Self>,fd: Resource<Descriptor>,offset: Filesize,) -> wasmtime::Result<(StreamReader<u8>, FutureReader<Result<(), ErrorCode>>)> {- store.with(|mut store| {- let file = get_file(store.get().table, &fd)?;- if !file.perms.contains(FilePerms::READ) {- return Ok((- StreamReader::new(&mut store, iter::empty()),- FutureReader::new(&mut store, async {- anyhow::Ok(Err(ErrorCode::NotPermitted))- }),- ));- }+ let file = get_file(store.get().table, &fd)?;+ if !file.perms.contains(FilePerms::READ) {+ return Ok((+ StreamReader::new(&mut store, iter::empty()),+ FutureReader::new(&mut store, async {+ anyhow::Ok(Err(ErrorCode::NotPermitted))+ }),+ ));+ }- let file = file.clone();- let (result_tx, result_rx) = oneshot::channel();- Ok((- StreamReader::new(- &mut store,- ReadStreamProducer {- file,- offset,- result: Some(result_tx),- task: None,- },- ),- FutureReader::new(&mut store, result_rx),- ))- })+ let file = file.clone();+ let (result_tx, result_rx) = oneshot::channel();+ Ok((+ StreamReader::new(+ &mut store,+ ReadStreamProducer {+ file,+ offset,+ result: Some(result_tx),+ task: None,+ },+ ),+ FutureReader::new(&mut store, result_rx),+ ))}async fn write_via_stream<U>(diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex f1420c24e3..59383acfc8 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -243,6 +243,12 @@ where.as_context_mut().spawn_with_accessor(accessor, task)}++ /// Returns the getter this accessor is using to project from `T` into+ /// `D::Data`.+ pub fn getter(&self) -> fn(&mut T) -> D::Data<'_> {+ self.get_data+ }}impl<'a, T, D> AsContext for Access<'a, T, D>