Skip to content
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

witx should encode ok/error result types #156

Closed
pchickey opened this issue Nov 23, 2019 · 2 comments
Closed

witx should encode ok/error result types #156

pchickey opened this issue Nov 23, 2019 · 2 comments
Labels
tooling Issues related to wasi-tools

Comments

@pchickey
Copy link
Contributor

All of the WASI APIs currently return errno, and take other return values as mutable output buffers (see #155). This makes sense as a C ABI. But, since our goal is language independence, we should encode this property in a way that permits idiomatic representations in other languages.

In effect, all output buffers are valid iff the errno is success, and invalid otherwise. This is captured in an ADT in many languages, e.g. Rust's Result<a, e>.

Currently, code generators like https://github.com/bytecodealliance/wasi/tree/master/crates/generate-raw transform the WASI APIs into idiomatic ones by a special case that knows about this convention. These generators would be more flexible, and useful for non-wasi witx interfaces, if this was not a special case.

One approach is to add new fields in each @interface func def, (@witx result-ok $name $ty) and (@witx result-err $ty). This would desugar to an identical C ABI as we currently have, but make it clear that the (exactly 1) result-err is the error case, and the 0 or more result-ok are the success case.

Another approach is to add full ADT & generics support to witx, and return a result enum like in Rust. That seems like a lot more work, even though I think we do eventually want ADTs and generics in witx.

@sunfishcode
Copy link
Member

I like the idea of encoding higher-level semantics in witx in general. I wonder if result-err is too specialized for this one use case though.

Brainstorm: what if we added four relatively simple things: a discriminated union (variant) type, a unit type, a boolean type, and a non-zero integer type? This wouldn't need generics; it might just look something like:

(typename $error (nonzero u32))
(typename $success (unit))

(typename $errno
  (variant bool
    (case false $success)
    (case true $error)
  )
)

We'd have several other uses for variants too, because all of our unions are really discriminated, eg:

(typename $subscription_u
  (variant $eventtype
    (case $clock $subscription_clock)
    (case $fd_readwrite $subscription_fd_readwrite)
  )
)

and others.

One subtlety: what happens if there's a variant function parameter and the user passes an invalid discriminator? I think we can say that's an EINVAL or so. Similar if the user passes in a zero to a nonzero parameter (if we ever have those).

@sunfishcode sunfishcode added the tooling Issues related to wasi-tools label Feb 19, 2020
@sunfishcode
Copy link
Member

This is implemented in #395, with the new result type in witx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tooling Issues related to wasi-tools
Projects
None yet
Development

No branches or pull requests

2 participants