-
Notifications
You must be signed in to change notification settings - Fork 245
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
Nested interfaces in wit #1624
base: main
Are you sure you want to change the base?
Nested interfaces in wit #1624
Changes from all commits
af8879c
403d1a3
e91ccd5
c9ee248
9906c2e
9cd7738
e834356
9d58da3
4728637
9f33021
48c6c86
226fa06
fc9b6bb
09737ba
ba550f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,11 +272,15 @@ impl InterfaceEncoder<'_> { | |
} | ||
} | ||
|
||
fn encode_instance(&mut self, interface: InterfaceId) -> Result<u32> { | ||
fn encode_instance_contents(&mut self, interface: InterfaceId) -> Result<InstanceType> { | ||
self.push_instance(); | ||
let iface = &self.resolve.interfaces[interface]; | ||
let mut type_order = IndexSet::new(); | ||
for (_, id) in iface.types.iter() { | ||
let ty = &self.resolve.types[*id]; | ||
if let TypeOwner::Interface(iface_id) = ty.owner { | ||
self.interface = Some(iface_id); | ||
} | ||
self.encode_valtype(self.resolve, &Type::Id(*id))?; | ||
type_order.insert(*id); | ||
} | ||
|
@@ -311,14 +315,45 @@ impl InterfaceEncoder<'_> { | |
.unwrap() | ||
.export(name, ComponentTypeRef::Func(ty)); | ||
} | ||
let instance = self.pop_instance(); | ||
let mut instance = self.pop_instance(); | ||
for nest in &iface.nested { | ||
let ty = instance.ty(); | ||
ty.instance(&self.encode_instance_contents(nest.id)?); | ||
instance.export( | ||
&self.interface_path(nest.id), | ||
ComponentTypeRef::Instance(instance.type_count() - 1), | ||
); | ||
} | ||
|
||
Ok(instance) | ||
} | ||
|
||
fn encode_instance(&mut self, interface: InterfaceId) -> Result<u32> { | ||
let instance = self.encode_instance_contents(interface)?; | ||
let idx = self.outer.type_count(); | ||
self.outer.ty().instance(&instance); | ||
self.import_map.insert(interface, self.instances); | ||
self.instances += 1; | ||
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()) | ||
} | ||
} | ||
Comment on lines
+340
to
+355
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. For this I think |
||
|
||
fn push_instance(&mut self) { | ||
assert!(self.ty.is_none()); | ||
assert!(self.saved_types.is_none()); | ||
|
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") | ||
) | ||
) |
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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
(component | ||
(type (;0;) | ||
(component | ||
(type (;0;) | ||
(instance | ||
(type (;0;) (record (field "foo" string))) | ||
(export (;1;) "my-record" (type (eq 0))) | ||
(type (;2;) (func (result string))) | ||
(export (;0;) "hello" (func (type 2))) | ||
(type (;3;) | ||
(instance | ||
(type (;0;) (record (field "foo" string))) | ||
(export (;1;) "nest-record" (type (eq 0))) | ||
(type (;2;) (func (result 1))) | ||
(export (;0;) "goodbye" (func (type 2))) | ||
(type (;3;) | ||
(instance | ||
(type (;0;) (record (field "foo" string))) | ||
(export (;1;) "deep-record" (type (eq 0))) | ||
(type (;2;) string) | ||
(export (;3;) "anything" (type (eq 2))) | ||
) | ||
) | ||
(export (;0;) "foo:nestnest/deep" (instance (type 3))) | ||
) | ||
) | ||
(export (;0;) "foo:nestee/[email protected]" (instance (type 3))) | ||
(type (;4;) | ||
(instance | ||
(export (;0;) "foo" (type (sub resource))) | ||
(type (;1;) (borrow 0)) | ||
(type (;2;) (option string)) | ||
(type (;3;) (func (param "self" 1) (result 2))) | ||
(export (;0;) "[method]foo.bar" (func (type 3))) | ||
) | ||
) | ||
(export (;1;) "foo:nestee/[email protected]" (instance (type 4))) | ||
) | ||
) | ||
(export (;0;) "foo:thing/[email protected]" (instance (type 0))) | ||
) | ||
) | ||
(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:{\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") | ||
) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package foo:[email protected]; | ||
|
||
interface things { | ||
//nesting can be documented | ||
@since(version = 1.0.0) | ||
nest foo:nestnest/deep; | ||
record nest-record { | ||
foo: string | ||
} | ||
goodbye: func() -> nest-record; | ||
} | ||
|
||
interface more { | ||
resource foo { | ||
bar: func() -> option<string>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package foo:nestnest; | ||
|
||
interface deep { | ||
record deep-record { | ||
foo: string | ||
} | ||
type anything = string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package foo:[email protected]; | ||
|
||
interface something { | ||
//nesting can be documented | ||
nest foo:nestee/[email protected]; | ||
@since(version = 1.0.0) | ||
nest foo:nestee/[email protected]; | ||
@since(version = 1.0.0) | ||
record my-record { | ||
foo: string | ||
} | ||
|
||
hello: func() -> string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package foo:[email protected]; | ||
|
||
interface something { | ||
/// nesting can be documented | ||
nest foo:nestee/[email protected]; | ||
@since(version = 1.0.0) | ||
nest foo:nestee/[email protected]; | ||
@since(version = 1.0.0) | ||
record my-record { | ||
foo: string, | ||
} | ||
|
||
hello: func() -> string; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,9 @@ impl<'a> DeclList<'a> { | |
Some(&u.names), | ||
WorldOrInterface::Interface, | ||
)?, | ||
InterfaceItem::Nest(n) => { | ||
f(Some(&i.name), &n.from, None, WorldOrInterface::Interface)? | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
@@ -549,6 +552,30 @@ enum InterfaceItem<'a> { | |
TypeDef(TypeDef<'a>), | ||
Func(NamedFunc<'a>), | ||
Use(Use<'a>), | ||
Nest(Nest<'a>), | ||
} | ||
|
||
struct Nest<'a> { | ||
docs: Docs<'a>, | ||
attributes: Vec<Attribute<'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)?; | ||
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. I think you'll want to |
||
let path = UsePath::parse(tokens)?; | ||
tokens.expect_semicolon()?; | ||
Ok(Self { | ||
docs, | ||
attributes, | ||
from: path, | ||
}) | ||
} | ||
} | ||
|
||
struct Use<'a> { | ||
|
@@ -987,6 +1014,9 @@ impl<'a> InterfaceItem<'a> { | |
NamedFunc::parse(tokens, docs, attributes).map(InterfaceItem::Func) | ||
} | ||
Some((_span, Token::Use)) => Use::parse(tokens, attributes).map(InterfaceItem::Use), | ||
Some((_span, Token::Nest)) => { | ||
Nest::parse(tokens, docs, attributes).map(InterfaceItem::Nest) | ||
} | ||
other => Err(err_expected(tokens, "`type`, `resource` or `func`", other).into()), | ||
} | ||
} | ||
|
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 this might be best done a little differently than updating
self.interface
here. What I'm worried about is that this updatesself.interface
but doesn't restore it what it previously was.Looking throughout this code though
push_instance
andpop_instance
might be a good place to hook into? I think it should be ok to move most modifications ofself.interface
into those functions where push takes anInterfaceId
and setsself.interface
and then pop restores the previous value saved during push.