-
Notifications
You must be signed in to change notification settings - Fork 348
Implement canonical interface names in wit-component #2622
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
Open
chenyan2002
wants to merge
4
commits into
bytecodealliance:main
Choose a base branch
from
chenyan2002:canon-ver-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,16 @@ use wit_parser::*; | |
| /// The binary returned can be [`decode`d](crate::decode) to recover the WIT | ||
| /// package provided. | ||
| pub fn encode(resolve: &Resolve, package: PackageId) -> Result<Vec<u8>> { | ||
| let mut component = encode_component(resolve, package)?; | ||
| encode_with_options(resolve, package, false) | ||
| } | ||
|
|
||
| /// Same as [`encode`] but with an option to emit canonical interface names. | ||
| pub fn encode_with_options( | ||
| resolve: &Resolve, | ||
| package: PackageId, | ||
| canonical_names: bool, | ||
| ) -> Result<Vec<u8>> { | ||
| let mut component = encode_component_with_options(resolve, package, canonical_names)?; | ||
| component.raw_custom_section(&crate::base_producers().raw_custom_section()); | ||
| Ok(component.finish()) | ||
| } | ||
|
|
@@ -48,11 +57,16 @@ pub fn encode(resolve: &Resolve, package: PackageId) -> Result<Vec<u8>> { | |
| /// | ||
| /// The binary returned can be [`decode`d](crate::decode) to recover the WIT | ||
| /// package provided. | ||
| pub fn encode_component(resolve: &Resolve, package: PackageId) -> Result<ComponentBuilder> { | ||
| pub fn encode_component_with_options( | ||
| resolve: &Resolve, | ||
| package: PackageId, | ||
| canonical_names: bool, | ||
| ) -> Result<ComponentBuilder> { | ||
| let mut encoder = Encoder { | ||
| component: ComponentBuilder::default(), | ||
| resolve, | ||
| package, | ||
| canonical_names, | ||
| }; | ||
| encoder.run()?; | ||
|
|
||
|
|
@@ -67,6 +81,15 @@ pub fn encode_component(resolve: &Resolve, package: PackageId) -> Result<Compone | |
|
|
||
| /// Encodes a `world` as a component type. | ||
| pub fn encode_world(resolve: &Resolve, world_id: WorldId) -> Result<ComponentType> { | ||
| encode_world_with_options(resolve, world_id, false) | ||
| } | ||
|
|
||
| /// Same as [`encode_world`] but with an option to emit canonical names. | ||
| pub fn encode_world_with_options( | ||
| resolve: &Resolve, | ||
| world_id: WorldId, | ||
| canonical_names: bool, | ||
| ) -> Result<ComponentType> { | ||
| let mut component = InterfaceEncoder::new(resolve); | ||
| let world = &resolve.worlds[world_id]; | ||
| log::trace!("encoding world {}", world.name); | ||
|
|
@@ -93,9 +116,10 @@ pub fn encode_world(resolve: &Resolve, world_id: WorldId) -> Result<ComponentTyp | |
| continue; | ||
| } | ||
| }; | ||
| component | ||
| .outer | ||
| .import(component_extern_name(resolve, key, import), ty); | ||
| component.outer.import( | ||
| component_extern_name(resolve, key, import, canonical_names), | ||
| ty, | ||
| ); | ||
| } | ||
| // Encode the exports | ||
| for (key, export) in world.exports.iter() { | ||
|
|
@@ -113,9 +137,10 @@ pub fn encode_world(resolve: &Resolve, world_id: WorldId) -> Result<ComponentTyp | |
| } | ||
| WorldItem::Type { .. } => unreachable!(), | ||
| }; | ||
| component | ||
| .outer | ||
| .export(component_extern_name(resolve, key, export), ty); | ||
| component.outer.export( | ||
| component_extern_name(resolve, key, export, canonical_names), | ||
| ty, | ||
| ); | ||
| } | ||
|
|
||
| Ok(component.outer) | ||
|
|
@@ -125,19 +150,40 @@ fn component_extern_name( | |
| resolve: &Resolve, | ||
| key: &WorldKey, | ||
| item: &WorldItem, | ||
| canonical_names: bool, | ||
| ) -> wasm_encoder::ComponentExternName<'static> { | ||
| ComponentExternName { | ||
| name: resolve.name_world_key(key).into(), | ||
| implements: resolve.implements_value(key, item).map(|s| s.into()), | ||
| external_id: resolve.external_id_value(key, item).map(|s| s.into()), | ||
| version_suffix: None, | ||
| if canonical_names { | ||
| let name = resolve.name_canon_world_key(key); | ||
| let mut implements = resolve.implements_value(key, item); | ||
| let version_suffix = match (&implements, key, item) { | ||
| (Some(_), _, WorldItem::Interface { id, .. }) => { | ||
| implements = resolve.canon_id_of(*id); | ||
| resolve.version_suffix_of(*id) | ||
| } | ||
| (None, WorldKey::Interface(id), _) => resolve.version_suffix_of(*id), | ||
| _ => None, | ||
| }; | ||
| ComponentExternName { | ||
| name: name.into(), | ||
| implements: implements.map(|s| s.into()), | ||
| external_id: resolve.external_id_value(key, item).map(|s| s.into()), | ||
| version_suffix: version_suffix.map(|s| s.into()), | ||
| } | ||
| } else { | ||
| ComponentExternName { | ||
| name: resolve.name_world_key(key).into(), | ||
| implements: resolve.implements_value(key, item).map(|s| s.into()), | ||
| external_id: resolve.external_id_value(key, item).map(|s| s.into()), | ||
| version_suffix: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct Encoder<'a> { | ||
| component: ComponentBuilder, | ||
| resolve: &'a Resolve, | ||
| package: PackageId, | ||
| canonical_names: bool, | ||
| } | ||
|
|
||
| impl Encoder<'_> { | ||
|
|
@@ -153,7 +199,8 @@ impl Encoder<'_> { | |
| // For each `world` encode it directly as a component and then create a | ||
| // wrapper component that exports that component. | ||
| for (name, &world) in self.resolve.packages[self.package].worlds.iter() { | ||
| let component_ty = encode_world(self.resolve, world)?; | ||
| let component_ty = | ||
| encode_world_with_options(self.resolve, world, self.canonical_names)?; | ||
|
|
||
| let world = &self.resolve.worlds[world]; | ||
| let mut wrapper = ComponentType::new(); | ||
|
|
@@ -197,11 +244,15 @@ impl Encoder<'_> { | |
| for interface in interfaces { | ||
| encoder.interface = Some(interface); | ||
| let iface = &self.resolve.interfaces[interface]; | ||
| let name = self.resolve.id_of(interface).unwrap(); | ||
| let name = if self.canonical_names { | ||
| self.resolve.canon_id_of(interface).unwrap() | ||
| } else { | ||
| self.resolve.id_of(interface).unwrap() | ||
| }; | ||
| if interface == id { | ||
| let idx = encoder.encode_instance(interface)?; | ||
| log::trace!("exporting self as {idx}"); | ||
| encoder.outer.export(name, ComponentTypeRef::Instance(idx)); | ||
| encoder.outer.export(&name, ComponentTypeRef::Instance(idx)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it might be wrong and/or untested -- if |
||
| } else { | ||
| encoder.push_instance(); | ||
| for (_, id) in iface.types.iter() { | ||
|
|
@@ -212,7 +263,7 @@ impl Encoder<'_> { | |
| encoder.outer.ty().instance(&instance); | ||
| encoder.import_map.insert(interface, encoder.instances); | ||
| encoder.instances += 1; | ||
| encoder.outer.import(name, ComponentTypeRef::Instance(idx)); | ||
| encoder.outer.import(&name, ComponentTypeRef::Instance(idx)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like I've seen this similar block in a few different locations throughout this diff, all pretty similar but also somewhat different too. Could there be a helper for constructing a
ComponentExternNamewhich takes all of this into account? I'd find it helpful for example to see all the logic in one place for "what are we gonna call this" as opposed to reviewing it spread out amongst a number of locations.Ideally as well the inputs to the function wouldn't be a string-y name but rather things like
WorldKeyorInterfaceId/&Functionor similar.