Skip to content

Add 'optional' imports/exports - #775

Open
lukewagner wants to merge 1 commit into
mainfrom
optional
Open

lukewagner wants to merge 1 commit into
mainfrom
optional

Conversation

@lukewagner

Copy link
Copy Markdown
Member

This PR adds the ability to declare function, value, instance and resource types as optional (in both WIT and in the CM type constructors), allowing optional imports and exports of these sorts of definitions. At the ABI level, there is a new optional.present built-in that generates a global i32 boolean that core wasm can import to test whether a given optional definition is present or not. If a function is not present and called, it traps, so that the burden is on the guest code to not do that. The other details are explained with examples in a new Text.md#-optionality section.

There's already some motivation listed in #555, but to restate and expand here, the goals are to:

  • support feature-testing to allow creating portable components that run across different hosts and across old/new versions of the same host
  • make it easy to upstream support for various WIT/WASI in standard library, package and framework code that doesn't/can't know the target world without forcing unwanted imports on all components that use the standard library/package/framework
  • be able to integrate with the feature-testing mechanisms of the language, host and toolchain

A valid question is why we can't just use the existing (option T) value type. Some answers are:

  • we need optional on functions, types and instances, which aren't first-class values (or values at all)
  • for feature-testing, we don't want to have to think about weird cases like (option (option T)) or (list (option T))
  • when importing an optional function, we want to get a funcidx (that can henceforth be used like a regular function for lifting/lowering), not a valueidx (and similarly for each other optional sort).

Due that last bullet, it seems like we need optional to go on the {function,value,instance} type constructor (and, for resource types, on the sub type bound) b/c we're talking about a special "not present" value/type in the respective index space.

Also in the PR:

  • an optional (type $T) ascription added to instance definitions that allows more coercion involving optional than would otherwise be sound to add to pure subtyping.
  • JS API + ESM integration. There are some TODOs in the ESM-integration to figure out whether we can push optionality "deeper" into ESM which I expect we'll have to look into asynchronously and discuss with ESM folks.
  • WAST tests that cover and expand on the example fragments in the new Text.md#-optionality section.

(I'm planning to keep this PR open for a while until someone gets a chance to work on an experimental implementation since it seems like this feature would benefit from that.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant