Repository navigation
Embed named type information in components - #369
Conversation
In writing support for the JS host generator recently I found myself unable to take a `wit-component`-generated component and generate bindings for it, namely because the names of types were not present so it wasn't clear what records and such would be named. Currently `wit-component` has a solution to this in the form of an "interface encoder" which produces a `*.wasm` file which is a theoretical bijection between a component and a `*.wit` interface as defined today. This `wit2wasm` and `wasm2wit` tooling, however, was all geared towards individual interfaces and didn't clearly fit within a full-fledged component produced by `wit-component`. In this PR I've decided to smash all of this together and get everything working in concert at the same time. Specifically in this PR I've done an large number of changes to `wit-component` along the lines of: * The `wit2wasm` tooling is not gone and replaced by `wit-component --types-only`. This was a large shift in the direction of `wit2wasm` as well as it wasn't merely moved but was instead integrated with `wit-component`'s idea of "worlds" where multiple interfaces are expressed simultaneously within a component (e.g. imports, exports, and the default interface). * The `--types-only` output, taken from `wit2wasm` is updated to support simultaneously describing multiple interfaces. Namely imported interfaces are instance imports, exported interfaces are exports of an instance type, and the default export exports types representing all the items in the default interface. * The default output of `wit-component` was updated to always embed type naming information. For example imported instances now also export type names on each individual instance. Exported instances export type names in addition to actual functions. This means that components always have named type information in them now when run through `wit-component`. * The `tests/interfaces.rs` test for components was overhauled to look more like `tests/components.rs`: multiple interfaces are supported. The test itself is still testing the same thing, though, namely it produces a `types-only` component which is verified against `types_only.wat`. The `types_only.wat` component is then additionally decoded using `wasm2wit` and the input `*.wit` files are asserted to all be correctly inferred from the input. Finally a second test was added to additionally produce a `component.wat` which is not `types-only` to check that interfaces can be inferred from full components in addition to types-only components. The "types only" interface of components may not be long-lived in `wit-component` depending on how things shape up over time. This isn't too bad though because the support for this mode is quite minimal. Otherwise there's a lot of effective churn in this PR largely around tests but the changes to Rust source files should all hopefully be more straightforward. One worry I have about this PR is that this is much more strongly leveraging a part of `wit-component` which isn't necessarily "battle-tested" yet. For example the mapping from a set of `*.wit` interfaces onto a component should be a bijection where one can always be generated from the other (modulo documentation at this time) but there are a number of features of `*.wit` which won't be recovered, for example type aliases. I would ideally like to write a fuzzer soon to fuzz-generate an `Interface` and round-trip that through a component to ensure the same interface pops out. This may not be the best thing to prioritize so early on though but I am still fearful that this can otherwise create really subtle and hard-to-understand bugs for users if the low-levels of tooling here have basic issues in them. For now though this should be sufficient to get the JS generator back on track since there's type information present and usable for generating human-readable and meaningful bindings.
|
Also I apologize for the large size of this PR @pchickey I tried to split it up but nothing really fit together well until everything was present. I'm happy to help walk through the PR over video if you'd like. I think most of the size changes are in the tests which should be easy to skim but there's a bunch of nontrivial changes in |
pchickey
left a comment
There was a problem hiding this comment.
Thanks, I like this improvement.
| assert!(prev.is_none()); | ||
| } | ||
| } | ||
| _ => {} |
There was a problem hiding this comment.
Thanks, the improved parser abstraction really improves this implementation
| for (name, ty) in info.imports.iter() { | ||
| // Imports right now are only supported if they're an import of an | ||
| // instance and an instance is self-describing in the interface that it | ||
| // represents. |
There was a problem hiding this comment.
Can you clarify what self-describing means here - I don't think any of the instance imports presently available in the binary format are not self-describing? or is this about future *-imports?
There was a problem hiding this comment.
Oh I just meant that the type of the import is sort of the serialization of the Interface type, I reworded a bit to capture that.
In writing support for the JS host generator recently I found myself unable to take a
wit-component-generated component and generate bindings for it, namely because the names of types were not present so it wasn't clear what records and such would be named.Currently
wit-componenthas a solution to this in the form of an "interface encoder" which produces a*.wasmfile which is a theoretical bijection between a component and a*.witinterface as defined today. Thiswit2wasmandwasm2wittooling, however, was all geared towards individual interfaces and didn't clearly fit within a full-fledged component produced bywit-component. In this PR I've decided to smash all of this together and get everything working in concert at the same time.Specifically in this PR I've done an large number of changes to
wit-componentalong the lines of:The
wit2wasmtooling is not gone and replaced bywit-component --types-only. This was a large shift in the direction ofwit2wasmas well as it wasn't merely moved but was instead integrated withwit-component's idea of "worlds" where multiple interfaces are expressed simultaneously within a component (e.g. imports, exports, and the default interface).The
--types-onlyoutput, taken fromwit2wasmis updated to support simultaneously describing multiple interfaces. Namely imported interfaces are instance imports, exported interfaces are exports of an instance type, and the default export exports types representing all the items in the default interface.The default output of
wit-componentwas updated to always embed type naming information. For example imported instances now also export type names on each individual instance. Exported instances export type names in addition to actual functions. This means that components always have named type information in them now when run throughwit-component.The
tests/interfaces.rstest for components was overhauled to look more liketests/components.rs: multiple interfaces are supported. The test itself is still testing the same thing, though, namely it produces atypes-onlycomponent which is verified againsttypes_only.wat. Thetypes_only.watcomponent is then additionally decoded usingwasm2witand the input*.witfiles are asserted to all be correctly inferred from the input. Finally a second test was added to additionally produce acomponent.watwhich is nottypes-onlyto check that interfaces can be inferred from full components in addition to types-only components.The "types only" interface of components may not be long-lived in
wit-componentdepending on how things shape up over time. This isn't too bad though because the support for this mode is quite minimal. Otherwise there's a lot of effective churn in this PR largely around tests but the changes to Rust source files should all hopefully be more straightforward.One worry I have about this PR is that this is much more strongly leveraging a part of
wit-componentwhich isn't necessarily "battle-tested" yet. For example the mapping from a set of*.witinterfaces onto a component should be a bijection where one can always be generated from the other (modulo documentation at this time) but there are a number of features of*.witwhich won't be recovered, for example type aliases. I would ideally like to write a fuzzer soon to fuzz-generate anInterfaceand round-trip that through a component to ensure the same interface pops out. This may not be the best thing to prioritize so early on though but I am still fearful that this can otherwise create really subtle and hard-to-understand bugs for users if the low-levels of tooling here have basic issues in them.For now though this should be sufficient to get the JS generator back on track since there's type information present and usable for generating human-readable and meaningful bindings.