Skip to content

Commit

Permalink
more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed Sep 15, 2024
1 parent 226fa06 commit fc9b6bb
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 234 deletions.
29 changes: 25 additions & 4 deletions crates/wit-component/src/encoding/wit/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ impl InterfaceEncoder<'_> {
Self::encode_instance_contents(self, interface)?;
let iface = &self.resolve.interfaces[interface];
let mut instance = self.pop_instance();
for (name, nest) in &iface.nested {
self.encode_nested(name, nest.id, &mut instance)?;
for nest in &iface.nested {
self.encode_nested(&self.interface_path(nest.id), nest.id, &mut instance)?;
}

let idx = self.outer.type_count();
Expand All @@ -240,6 +240,23 @@ impl InterfaceEncoder<'_> {
Ok(idx)
}

fn interface_path(&self, iface: InterfaceId) -> String {
let iface = &self.resolve.interfaces[iface];
let pkg_id = iface.package.unwrap();
let pkg = &self.resolve.packages[pkg_id];
if let Some(v) = &pkg.name.version {
format!(
"{}:{}/{}@{}",
pkg.name.namespace,
pkg.name.name,
iface.name.as_ref().unwrap(),
v.to_string()
)
} else {
format!("{}/{}", pkg.name.to_string(), iface.name.as_ref().unwrap())
}
}

fn encode_nested<'a>(
&mut self,
name: &str,
Expand All @@ -251,9 +268,13 @@ impl InterfaceEncoder<'_> {
let ty = instance.ty();
let nested_instance = &mut inst.pop_instance();
let iface = &self.resolve.interfaces[iface_id];
for (deep_name, deep_nest) in &iface.nested {
for deep_nest in &iface.nested {
let mut inst = InterfaceEncoder::new(&self.resolve);
inst.encode_nested(deep_name, deep_nest.id, nested_instance)?;
inst.encode_nested(
&self.interface_path(deep_nest.id),
deep_nest.id,
nested_instance,
)?;
}
ty.instance(&nested_instance);
instance.export(name, ComponentTypeRef::Instance(instance.type_count() - 1));
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl WitPrinter {
fn print_interface(&mut self, resolve: &Resolve, id: InterfaceId) -> Result<()> {
let prev_items = mem::replace(&mut self.any_items, false);
let interface = &resolve.interfaces[id];
for (_, nest) in &interface.nested {
for nest in &interface.nested {
self.print_stability(&nest.stability);
self.print_docs(&nest.docs);
self.output.push_str("nest ");
Expand Down
23 changes: 23 additions & 0 deletions crates/wit-component/tests/interfaces/multi-package-nest.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(component
(type (;0;)
(component
(type (;0;)
(instance
(type (;0;)
(instance
(type (;0;) string)
(export (;1;) "t" (type (eq 0)))
)
)
(export (;0;) "foo:other/i" (instance (type 0)))
)
)
(export (;0;) "foo:bar/i1" (instance (type 0)))
)
)
(export (;1;) "i1" (type 0))
(@custom "package-docs" "\00{}")
(@producers
(processed-by "wit-component" "$CARGO_PKG_VERSION")
)
)
11 changes: 11 additions & 0 deletions crates/wit-component/tests/interfaces/multi-package-nest.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package foo:bar;

package foo:other {
interface i {
type t = string;
}
}

interface i1 {
nest foo:other/i;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package foo:bar;

interface i1 {
nest foo:other/i;
}

2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces/nested.wat
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
)
)
(export (;1;) "something" (type 0))
(@custom "package-docs" "\01{\22interfaces\22:{\22something\22:{\22types\22:{\22my-record\22:{\22stability\22:{\22stable\22:{\22since\22:\221.0.0\22}}}},\22nested\22:{\22foo:nestee/[email protected]\22:{\22docs\22:{\22contents\22:\22nesting can be documented\22},\22stability\22:\22unknown\22},\22foo:nestee/[email protected]\22:{\22docs\22:{\22contents\22:null},\22stability\22:{\22stable\22:{\22since\22:\221.0.0\22}}}}}}}")
(@custom "package-docs" "\01{\22interfaces\22:{\22something\22:{\22types\22:{\22my-record\22:{\22stability\22:{\22stable\22:{\22since\22:\221.0.0\22}}}},\22nested\22:{\22things\22:{\22docs\22:{\22contents\22:\22nesting can be documented\22},\22stability\22:\22unknown\22},\22more\22:{\22docs\22:{\22contents\22:null},\22stability\22:{\22stable\22:{\22since\22:\221.0.0\22}}}}}}}")
(@producers
(processed-by "wit-component" "$CARGO_PKG_VERSION")
)
Expand Down
26 changes: 18 additions & 8 deletions crates/wit-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,23 @@ struct Nest<'a> {
from: UsePath<'a>,
}

impl<'a> Nest<'a> {
fn parse(
tokens: &mut Tokenizer<'a>,
docs: Docs<'a>,
attributes: Vec<Attribute<'a>>,
) -> Result<Self> {
tokens.eat(Token::Nest)?;
let path = UsePath::parse(tokens)?;
tokens.expect_semicolon()?;
Ok(Self {
docs,
attributes,
from: path,
})
}
}

struct Use<'a> {
attributes: Vec<Attribute<'a>>,
from: UsePath<'a>,
Expand Down Expand Up @@ -998,14 +1015,7 @@ impl<'a> InterfaceItem<'a> {
}
Some((_span, Token::Use)) => Use::parse(tokens, attributes).map(InterfaceItem::Use),
Some((_span, Token::Nest)) => {
tokens.eat(Token::Nest)?;
let path = UsePath::parse(tokens)?;
tokens.expect_semicolon()?;
Ok(InterfaceItem::Nest(Nest {
docs,
attributes,
from: path,
}))
Nest::parse(tokens, docs, attributes).map(InterfaceItem::Nest)
}
other => Err(err_expected(tokens, "`type`, `resource` or `func`", other).into()),
}
Expand Down
59 changes: 10 additions & 49 deletions crates/wit-parser/src/ast/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::{ParamList, ResultList, WorldOrInterface};
use crate::ast::toposort::toposort;
use crate::ast::UsePath;
use crate::*;
use anyhow::bail;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -312,7 +311,7 @@ impl<'a> Resolver<'a> {
});
self.interfaces.alloc(Interface {
name: None,
nested: IndexMap::new(),
nested: Vec::new(),
types: IndexMap::new(),
docs: Docs::default(),
stability: Default::default(),
Expand Down Expand Up @@ -842,54 +841,16 @@ impl<'a> Resolver<'a> {
}
ast::InterfaceItem::TypeDef(_) => {}
ast::InterfaceItem::Nest(n) => {
if let ast::Nest {
from:
UsePath::Package {
id: package_id,
name,
},
attributes,
docs,
} = n
{
let nest = self
.foreign_deps
.get(&PackageName {
namespace: package_id.namespace.name.to_string(),
name: package_id.name.name.to_string(),
version: package_id.clone().version.map(|v| v.1),
})
.unwrap()
.get(&name.name)
.unwrap();
let nested_id = if let AstItem::Interface(id) = nest {
id
} else {
bail!("Expected interface item")
};
let stability = self.stability(attributes)?;
let docs = self.docs(&docs);
let full_name = if let Some(v) = &package_id.version {
format!(
"{}:{}/{}@{}",
package_id.namespace.name,
package_id.name.name,
name.name,
v.1.to_string()
)
} else {
format!("{}/{}", package_id.package_name(), name.name)
};
let (item, name, span) = self.resolve_ast_item_path(&n.from)?;
let nested_id = self.extract_iface_from_item(&item, &name, span)?;

self.interfaces[interface_id].nested.insert(
full_name.clone(),
crate::Nest {
id: *nested_id,
docs,
stability,
},
);
}
let stability = self.stability(&n.attributes)?;
let docs = self.docs(&n.docs);
self.interfaces[interface_id].nested.push(crate::Nest {
id: nested_id,
docs,
stability,
});
}
}
}
Expand Down
Loading

0 comments on commit fc9b6bb

Please sign in to comment.