-
Notifications
You must be signed in to change notification settings - Fork 244
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
feat: preliminary support for WIT templates #964
Draft
dicej
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
dicej:wit-templates
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.
Draft
Changes from all commits
Commits
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 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 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 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::{Interface, InterfaceId, Resolve, WorldItem}; | ||
use anyhow::{anyhow, Result}; | ||
use id_arena::Arena; | ||
use indexmap::IndexMap; | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
pub type Substitutions = HashMap<String, HashMap<String, HashSet<String>>>; | ||
|
||
pub fn expand(resolve: &mut Resolve, mut substitutions: Substitutions) -> Result<()> { | ||
let mut new_interfaces = resolve.interfaces.clone(); | ||
for (_, world) in &mut resolve.worlds { | ||
let mut subs = substitutions.remove(&world.name).unwrap_or_default(); | ||
expand_interfaces( | ||
&world.name, | ||
"imports", | ||
&mut world.imports, | ||
&mut new_interfaces, | ||
&mut subs, | ||
)?; | ||
expand_interfaces( | ||
&world.name, | ||
"exports", | ||
&mut world.exports, | ||
&mut new_interfaces, | ||
&mut subs, | ||
)?; | ||
} | ||
|
||
if !substitutions.is_empty() { | ||
log::warn!("unused substitutions were provided: {substitutions:?}",); | ||
} | ||
|
||
resolve.interfaces = new_interfaces; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn expand_interfaces( | ||
world_name: &str, | ||
desc: &str, | ||
items: &mut IndexMap<String, WorldItem>, | ||
new_interfaces: &mut Arena<Interface>, | ||
substitutions: &mut HashMap<String, HashSet<String>>, | ||
) -> Result<()> { | ||
for (name, item) in items { | ||
if let WorldItem::Interface(interface) = item { | ||
if new_interfaces[*interface].wildcard.is_some() { | ||
let new_interface = expand_interface( | ||
*interface, | ||
new_interfaces, | ||
substitutions.remove(name).ok_or_else(|| { | ||
anyhow!( | ||
"world {world_name} {desc} item {name} contains wildcards \ | ||
but no substitutions were provided", | ||
) | ||
})?, | ||
); | ||
*interface = new_interfaces.alloc(new_interface); | ||
} | ||
} | ||
} | ||
|
||
if !substitutions.is_empty() { | ||
log::warn!("unused substitutions were provided for world {world_name}: {substitutions:?}",); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn expand_interface( | ||
interface: InterfaceId, | ||
new_interfaces: &Arena<Interface>, | ||
substitutions: HashSet<String>, | ||
) -> Interface { | ||
let mut new_interface = new_interfaces[interface].clone(); | ||
// Make the expanded interface anonymous; otherwise the generated component type will fail to validate due to | ||
// the existence of multiple interfaces with the same name. | ||
// | ||
// TODO: implement something like | ||
// https://github.com/WebAssembly/component-model/issues/172#issuecomment-1466939890, which may entail changes | ||
// to how interfaces are modeled in WIT, `Resolve`, and the component model. | ||
new_interface.name = None; | ||
let function = new_interface.wildcard.take().unwrap(); | ||
for var_name in substitutions { | ||
let mut new_func = function.clone(); | ||
new_func.name = var_name.clone(); | ||
assert!(new_interface.functions.insert(var_name, new_func).is_none()); | ||
} | ||
new_interface | ||
} |
This file contains 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 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 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
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 think we'll want to handle
prev.is_some()
(indicating multiple*
appears textually) here and return a concrete syntax error positioned at the span of the wildcard.