Skip to content

Draft a prototype stdio design for wasip3. #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions wit-0.3.0-draft/stdio.wit
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
@since(version = 0.3.0)
interface stdin {
@since(version = 0.3.0)
get-stdin: func() -> stream<u8>;
use stdio.{error};

/// Reads data from a “standard input”.
///
/// This function reads at most `amount` bytes from a
/// “standard input” source.
@since(version = 0.3.0)
read: func(amount: u32) -> result<list<u8>, error>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be async so you can avoid blocking?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Async in Wit is just a hint, so you can avoid blocking either way, and the main question is what the default should be.

In my experience most programs that use stdin use it in a synchronous way, which suggests making it sync by default, but I don't have a strong opinion about it.

Copy link

@programmerjake programmerjake Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many C programs use functions like kbhit or O_NONBLOCK on stdin, imo we should have support in the C library for stuff like that, afaict that requires the C library to use the async version even if most programs just block.

}

@since(version = 0.3.0)
interface stdout {
@since(version = 0.3.0)
set-stdout: func(data: stream<u8>);
use stdio.{error};

/// Writes data to a “standard output”.
///
/// This function writes the bytes of `data` to a “standard output”
/// destination.
///
/// The data may be either text or binary; if it is text, it is recommended
/// to be encoded as UTF-8, to allow terminal implementations on non-UTF-8
/// host environments to transcode it into their native text encoding.
@since(version = 0.3.0)
write: func(data: list<u8>) -> result<_, error>;
}

@since(version = 0.3.0)
interface stderr {
@since(version = 0.3.0)
set-stderr: func(data: stream<u8>);
use stdio.{error};

/// Writes data to a “standard error”.
///
/// This function writes the bytes of `data` to a “standard error”
/// destination.
///
/// As with `stdout.write`, the data may be tierh text or binary, and
/// if it is text, it is recommended to be encoded as UTF-8.
@since(version = 0.3.0)
write: func(data: list<u8>) -> result<_, error>;
}

/// Types common to the stdio interfaces.
@since(version = 0.3.0)
interface stdio {
/// An error code for stdio functions.
@since(version = 0.3.0)
enum error {
/// An I/O error occurred and the operation not completed successfully.
///
/// After a `read` or `write` function has returned `io`, it thereafter
/// always returns `io`.
io,

/// The other end was dropped and so this end must now be dropped.
///
/// For `read`, this means the end of the stream has been reached.
///
/// For `write`, this means that the consumer has no need for further
/// data from this stream. This doesn't signify an error; it just
/// instructs the producer to stop sending data.
///
/// After a `read` or `write` function has returned `dropped`, it
/// thereafter always returns `dropped`.
dropped,
}
}

// FIXME: Provide a way to keep reading after a `dropped`, for the `tail -f` idiom?

// FIXME: Should write return the number of bytes written?

// FIXME: How can we avoid the whole situation that caused wasip2 to have `check-write`?