Description
We propose to move logic from wit-bindgen
into the wit-parser
crate that determines whether an interface, type, or function in a Resolve
structure is either imported or exported, and label those structures as such.
- Add an attribute to the
Interface
,Function
, andTypeDef
structs to indicate whether it is imported or exported. - The tree of imports and exports in a
World
would be duplicated (if necessary) and have the relevant import/export attribute. - Modify the JSON serialization of
Resolve
(e.g.wasm-tools component wit -j ...
) to include the import/export attribute.
Context
This is a followup from a conversation in Zulip regarding imported and exported types in a Resolve
.
Currently, any types (represented as a TypeDef
), functions (Function
), and interfaces (Interface
) are represented once in the Resolve
data structure (and its JSON representation). This means that each bindings generator that depends on the wit-parser
crate must reimplement the heuristic to determine when to use an imported type versus an exported type.
Per @alexcrichton:
For given interface utils
, and a type fd
defined in a different interface types
:
What ends up happening here is a bit subtle and it's generally related to WIT conventions. The CM itself has no ambiguity, the problem arises when WIT is mapped back to the component model. To answer your question the cases are:
- If
utils
is imported, then it uses the importedfd
- If
utils
is exported, andfd
is not exported, then it uses an importedfd
- If
utils
is exported, andfd
is exported, then it will use the exportedfd
there's also implicit insertion of interfaces to handle here too, for example
wasm-tools component wit ./my-wit
will show the "elaborated" version of a world. For example if you doexport utils;
that'll implicitly insertimport fd;
. If you haveexport fd; export utils;
, however, then no implicit insertion happens andutils
uses the exportedfd
.
This would simplify the implementation of bindings generators, such as wit-bindgen-go, which could then depend on the import/export resolution in wit-parser
.