Skip to content

Commit 9ebb379

Browse files
Fixes
1 parent eac3c54 commit 9ebb379

2 files changed

Lines changed: 18 additions & 27 deletions

File tree

crates/c-api/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ wasmtime = { workspace = true, features = ['runtime', 'gc', 'std'] }
2626
wasmtime-c-api-macros = { workspace = true }
2727
log = { workspace = true }
2828
tracing = { workspace = true }
29-
async-trait = { workspace = true }
30-
bytes = { workspace = true }
3129

3230
# Optional dependency for the `wat2wasm` API
3331
wat = { workspace = true, optional = true }
@@ -37,6 +35,8 @@ cap-std = { workspace = true, optional = true }
3735
tokio = { workspace = true, optional = true, features = ["fs"] }
3836
wasmtime-wasi = { workspace = true, optional = true, features = ["p1"] }
3937
wasmtime-wasi-io = { workspace = true, optional = true, features = ["std"] }
38+
async-trait = { workspace = true, optional = true }
39+
bytes = { workspace = true, optional = true }
4040

4141
# Optional dependencies for the `async` feature
4242
futures = { workspace = true, optional = true }
@@ -47,7 +47,7 @@ async = ['wasmtime/async', 'futures']
4747
profiling = ["wasmtime/profiling"]
4848
cache = ["wasmtime/cache"]
4949
parallel-compilation = ['wasmtime/parallel-compilation']
50-
wasi = ['cap-std', 'wasmtime-wasi', 'wasmtime-wasi-io', 'tokio']
50+
wasi = ['cap-std', 'wasmtime-wasi', 'wasmtime-wasi-io', 'tokio', 'async-trait', 'bytes']
5151
logging = ['dep:env_logger']
5252
disable-logging = ["log/max_level_off", "tracing/max_level_off"]
5353
coredump = ["wasmtime/coredump"]

crates/c-api/src/wasi.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,18 @@ struct CustomOutputStreamInner {
160160
}
161161

162162
impl CustomOutputStreamInner {
163-
pub fn write(&self, buf: &[u8]) -> isize {
164-
(self.callback)(self.foreign_data.data, buf.as_ptr(), buf.len())
163+
pub fn raw_write(&self, buf: &[u8]) -> io::Result<usize> {
164+
let wrote = (self.callback)(self.foreign_data.data, buf.as_ptr(), buf.len());
165+
166+
if wrote >= 0 {
167+
Ok(wrote as _)
168+
} else {
169+
Err(io::Error::from_raw_os_error(wrote.abs() as _))
170+
}
165171
}
166172
}
167173

174+
#[derive(Clone)]
168175
pub struct CustomOutputStream {
169176
inner: std::sync::Arc<CustomOutputStreamInner>,
170177
}
@@ -183,14 +190,6 @@ impl CustomOutputStream {
183190
}
184191
}
185192

186-
impl Clone for CustomOutputStream {
187-
fn clone(&self) -> Self {
188-
Self {
189-
inner: self.inner.clone(),
190-
}
191-
}
192-
}
193-
194193
#[async_trait::async_trait]
195194
impl wasmtime_wasi::p2::Pollable for CustomOutputStream {
196195
async fn ready(&mut self) {}
@@ -199,15 +198,12 @@ impl wasmtime_wasi::p2::Pollable for CustomOutputStream {
199198
#[async_trait::async_trait]
200199
impl wasmtime_wasi::p2::OutputStream for CustomOutputStream {
201200
fn write(&mut self, bytes: Bytes) -> Result<(), StreamError> {
202-
let wrote = self.inner.write(&bytes);
203-
204-
if wrote < 0 {
205-
return Err(StreamError::Trap(
206-
io::Error::from_raw_os_error(wrote.abs() as _).into(),
207-
));
208-
}
201+
let wrote = self
202+
.inner
203+
.raw_write(&bytes)
204+
.map_err(|e| StreamError::LastOperationFailed(e.into()))?;
209205

210-
if wrote as usize != bytes.len() {
206+
if wrote != bytes.len() {
211207
return Err(StreamError::Trap(anyhow::anyhow!(
212208
"Partial writes in wasip2 implementation are not allowed"
213209
)));
@@ -229,12 +225,7 @@ impl AsyncWrite for CustomOutputStream {
229225
_cx: &mut Context<'_>,
230226
buf: &[u8],
231227
) -> Poll<io::Result<usize>> {
232-
let wrote = self.inner.write(buf);
233-
Poll::Ready(if wrote >= 0 {
234-
Ok(wrote as _)
235-
} else {
236-
Err(io::Error::from_raw_os_error(wrote.abs() as _))
237-
})
228+
Poll::Ready(self.inner.raw_write(buf))
238229
}
239230
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
240231
Poll::Ready(Ok(()))

0 commit comments

Comments
 (0)