MangoPeachGrape opened PR #11965 from MangoPeachGrape:c-api/wasi/custom-callbacks to bytecodealliance:main:
Fixes #11963
I'm quite unsure on some things. I split the implementation out between the wasi and c-api crates, because I would've needed to add some dependencies into c-api crate to implement the required traits, not sure what's better?
Also, I don't particularly like the
Arc<Mutex>, but I guess it's required for the async version?Also also,
const void*vsconst unsigned char*for the buffer? (https://github.com/bytecodealliance/wasmtime/issues/11963#issuecomment-3476575567)
MangoPeachGrape requested wasmtime-wasi-reviewers for a review on PR #11965.
MangoPeachGrape requested alexcrichton for a review on PR #11965.
MangoPeachGrape requested wasmtime-core-reviewers for a review on PR #11965.
alexcrichton submitted PR review:
Thanks! Mind adding a test for this too?
alexcrichton created PR review comment:
We've probably got 1-too-many traits already in the wasmtime-wasi crate, so mind moving this directly into the c-api crate? It should be fine to implement pollable/outputstream/asyncwrite directly on the c-api types and avoid the trait indirection here (should avoid arc/mutex as well I think)
MangoPeachGrape requested pchickey for a review on PR #11965.
MangoPeachGrape requested wasmtime-default-reviewers for a review on PR #11965.
MangoPeachGrape updated PR #11965.
MangoPeachGrape submitted PR review.
MangoPeachGrape created PR review comment:
Moved everything into the c-api crate now. I was able to get rid of the mutex, but unless I'm mistaken, I think the arc is necessary for the clone in
StdoutStream::async_stream()?Also for tests, I see that they are now C++, does that mean I would need to add a C++ API for it to be tested?
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Ah true yeah,
Arcis still required. For tests it's ok to skip C++, but you'll probably be using a C++ wrapper mostly and then in the guts do someting C-like. Would that work ok?
alexcrichton submitted PR review.
alexcrichton created PR review comment:
For WASIp2 I'd then also say that a partial write just gets turned into an error (generated in the bindings here) saying that partial writes aren't supported in WASIp2's C API at this time.
alexcrichton created PR review comment:
For this it's ok to remove the
Optionin the function signature to have this statically be non-null. That can't be reflected in C's type system but it can be reflected in the documentation.
alexcrichton created PR review comment:
For this, WDYT about using a
read/write-syscall-style interface? Something wheressize_tis returned and negative numbers are system errno-like things while positive numbers are how many bytes were written?
MangoPeachGrape submitted PR review.
MangoPeachGrape created PR review comment:
Seems like a good idea, but it seems like
ssize_tis somehow not a standard type... isptrdiff_tthe correct choice then?What do you mean exactly by "errno-like" -- Should negative numbers be just be arbitrarily user specified error codes, as things like
io::Result::from_raw_os_error()is OS specific? If so, how should I create theio::Result, maybeio::ErrorKind::Otherwith "Write failed with error {code}"?
MangoPeachGrape updated PR #11965.
MangoPeachGrape submitted PR review.
MangoPeachGrape created PR review comment:
dataandfinalizerarguments are specified as "An optional ...", I'm wondering if that is enough to indicate that the others are required, or should I add a note that a callback is required?
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Finalizer I'd leave as optional yeah, I'd only change the signature of
callback. For docs it should be fine to say thatcallbackis required to be non-null.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Oh? For me
man 2 writeshows the return beingssize_t...But yeah I'm imaginig that negative errors turn into
io::Errorthings. You should be able to useio::Error::raw_os_errorand then plumb that error around I think? (I may be missing something though)
MangoPeachGrape updated PR #11965.
MangoPeachGrape submitted PR review.
MangoPeachGrape created PR review comment:
Unless I'm completely mistaken,
ssize_tin only a POSIX thing, so it isn't defined on Windows for example?But is using
io::Error::from_raw_os_error(wrote.abs())fine? I guess the embedder will return different codes on different platforms, but that function will map them correctly?
alexcrichton submitted PR review.
alexcrichton created PR review comment:
This'll want to use
LastOperationFailedrather thanTrapto match the behavior ofAsyncWritebelow
alexcrichton created PR review comment:
Mind making these optional and having the
wasifeature activate them?
alexcrichton created PR review comment:
This is fine to use
#[derive(Clone)]for -- that's not used in the wasi crates as it would erroneously place aClonebound on the generics where they were otherwise behind anArcbut that's not a concern here.
alexcrichton created PR review comment:
Mind deduplicating this with the above by having a helper
fn raw_write(&self, bytes: &[u8]) -> io::Result<usize>or similar?
alexcrichton created PR review comment:
Oh oops I didn't realize Windows didn't have that... I think
ptrdiff_tshould match roughly what we're looking for yeah (although it's a bit funky alas...)And yeah
io::Errorshould do the right platform-specific thing.
MangoPeachGrape updated PR #11965.
MangoPeachGrape submitted PR review.
MangoPeachGrape created PR review comment:
What about the
wrote != bytes.len()branch below, should that useTraporLastOperationFailed?
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Oh, sorry, yeah that should be
LastOperationFailedas well
MangoPeachGrape updated PR #11965.
alexcrichton submitted PR review.
alexcrichton has enabled auto merge for PR #11965.
alexcrichton has disabled auto merge for PR #11965.
MangoPeachGrape updated PR #11965.
alexcrichton has enabled auto merge for PR #11965.
alexcrichton has disabled auto merge for PR #11965.
alexcrichton closed without merge PR #11965.
alexcrichton reopened PR #11965.
alexcrichton has enabled auto merge for PR #11965.
MangoPeachGrape commented on PR #11965:
Did I break CI... :laughing:
alexcrichton merged PR #11965.
Last updated: Dec 06 2025 at 06:05 UTC