Skip to content

Commit

Permalink
some comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed Sep 12, 2024
1 parent 48c6c86 commit 226fa06
Show file tree
Hide file tree
Showing 59 changed files with 61 additions and 233 deletions.
4 changes: 0 additions & 4 deletions crates/wit-component/src/encoding/wit/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ impl InterfaceEncoder<'_> {
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);
}
Expand Down
96 changes: 48 additions & 48 deletions crates/wit-component/src/encoding/wit/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,6 @@ pub fn encode_component(resolve: &Resolve, package: PackageId) -> Result<Compone
Ok(encoder.component)
}

fn encode_instance_contents(enc: &mut InterfaceEncoder, interface: InterfaceId) -> Result<()> {
enc.push_instance();
let iface = &enc.resolve.interfaces[interface];
let mut type_order = IndexSet::new();
for (_, id) in iface.types.iter() {
let ty = &enc.resolve.types[*id];
if let TypeOwner::Interface(iface_id) = ty.owner {
enc.interface = Some(iface_id);
}
enc.encode_valtype(enc.resolve, &Type::Id(*id))?;
type_order.insert(*id);
}

// Sort functions based on whether or not they're associated with
// resources.
//
// This is done here to ensure that when a WIT package is printed as WIT
// then decoded, or if it's printed as Wasm then decoded, the final
// result is the same. When printing via WIT resource methods are
// attached to the resource types themselves meaning that they'll appear
// intermingled with the rest of the types, namely first before all
// other functions. The purpose of this sort is to perform a stable sort
// over all functions by shuffling the resource-related functions first,
// in order of when their associated resource was encoded, and putting
// freestanding functions last.
//
// Note that this is not actually required for correctness, it's
// basically here to make fuzzing happy.
let mut funcs = iface.functions.iter().collect::<Vec<_>>();
funcs.sort_by_key(|(_name, func)| match func.kind {
FunctionKind::Freestanding => type_order.len(),
FunctionKind::Method(id) | FunctionKind::Constructor(id) | FunctionKind::Static(id) => {
type_order.get_index_of(&id).unwrap()
}
});

for (name, func) in funcs {
let ty = enc.encode_func_type(enc.resolve, func)?;
enc.ty
.as_mut()
.unwrap()
.export(name, ComponentTypeRef::Func(ty));
}
Ok(())
}

struct Encoder<'a> {
component: ComponentBuilder,
resolve: &'a Resolve,
Expand Down Expand Up @@ -225,8 +179,54 @@ impl InterfaceEncoder<'_> {
}
}

fn encode_instance_contents(enc: &mut InterfaceEncoder, interface: InterfaceId) -> Result<()> {
enc.push_instance();
let iface = &enc.resolve.interfaces[interface];
let mut type_order = IndexSet::new();
for (_, id) in iface.types.iter() {
let ty = &enc.resolve.types[*id];
if let TypeOwner::Interface(iface_id) = ty.owner {
enc.interface = Some(iface_id);
}
enc.encode_valtype(enc.resolve, &Type::Id(*id))?;
type_order.insert(*id);
}

// Sort functions based on whether or not they're associated with
// resources.
//
// This is done here to ensure that when a WIT package is printed as WIT
// then decoded, or if it's printed as Wasm then decoded, the final
// result is the same. When printing via WIT resource methods are
// attached to the resource types themselves meaning that they'll appear
// intermingled with the rest of the types, namely first before all
// other functions. The purpose of this sort is to perform a stable sort
// over all functions by shuffling the resource-related functions first,
// in order of when their associated resource was encoded, and putting
// freestanding functions last.
//
// Note that this is not actually required for correctness, it's
// basically here to make fuzzing happy.
let mut funcs = iface.functions.iter().collect::<Vec<_>>();
funcs.sort_by_key(|(_name, func)| match func.kind {
FunctionKind::Freestanding => type_order.len(),
FunctionKind::Method(id) | FunctionKind::Constructor(id) | FunctionKind::Static(id) => {
type_order.get_index_of(&id).unwrap()
}
});

for (name, func) in funcs {
let ty = enc.encode_func_type(enc.resolve, func)?;
enc.ty
.as_mut()
.unwrap()
.export(name, ComponentTypeRef::Func(ty));
}
Ok(())
}

fn encode_instance(&mut self, interface: InterfaceId) -> Result<u32> {
encode_instance_contents(self, interface)?;
Self::encode_instance_contents(self, interface)?;
let iface = &self.resolve.interfaces[interface];
let mut instance = self.pop_instance();
for (name, nest) in &iface.nested {
Expand All @@ -247,7 +247,7 @@ impl InterfaceEncoder<'_> {
instance: &'a mut InstanceType,
) -> Result<()> {
let mut inst = InterfaceEncoder::new(&self.resolve);
encode_instance_contents(&mut inst, iface_id)?;
Self::encode_instance_contents(&mut inst, iface_id)?;
let ty = instance.ty();
let nested_instance = &mut inst.pop_instance();
let iface = &self.resolve.interfaces[iface_id];
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces/doc-comments.wat
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
)
)
(export (;5;) "coverage-world" (type 4))
(@custom "package-docs" "\00{\22docs\22:\22package docs;\22,\22worlds\22:{\22coverage-world\22:{\22docs\22:\22world docs\22,\22interfaces\22:{\22i\22:{\22docs\22:\22world inline interface docs\22,\22funcs\22:{\22f\22:\22inline interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22inline interface typedef docs\22}},\22nested\22:{}}},\22types\22:{\22t\22:{\22docs\22:\22world typedef docs\22}},\22funcs\22:{\22imp\22:\22world func import docs\22,\22exp\22:\22world func export docs\22}}},\22interfaces\22:{\22coverage-iface\22:{\22docs\22:\22interface docs\22,\22funcs\22:{\22[constructor]res\22:\22constructor docs\22,\22[method]res.m\22:\22method docs\22,\22[static]res.s\22:\22static func docs\22,\22f\22:\22interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22basic typedef docs\22},\22r\22:{\22docs\22:\22record typedef docs\22,\22items\22:{\22f1\22:\22record field docs\22}},\22fl\22:{\22items\22:{\22f1\22:\22flag docs\22}},\22v\22:{\22items\22:{\22c1\22:\22variant case docs\22}},\22e\22:{\22items\22:{\22c1\22:\22enum case docs\22}}},\22nested\22:{}},\22other-comment-forms\22:{\22docs\22:\22other comment forms\5cn multi-line block\22,\22funcs\22:{\22multiple-lines-split\22:\22one doc line\5cnnon-doc in the middle\5cnanother doc line\22,\22mixed-forms\22:\22mixed forms; line doc\5cnplus block doc\5cn multi-line\22},\22nested\22:{}}}}")
(@custom "package-docs" "\00{\22docs\22:\22package docs;\22,\22worlds\22:{\22coverage-world\22:{\22docs\22:\22world docs\22,\22interfaces\22:{\22i\22:{\22docs\22:\22world inline interface docs\22,\22funcs\22:{\22f\22:\22inline interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22inline interface typedef docs\22}}}},\22types\22:{\22t\22:{\22docs\22:\22world typedef docs\22}},\22funcs\22:{\22imp\22:\22world func import docs\22,\22exp\22:\22world func export docs\22}}},\22interfaces\22:{\22coverage-iface\22:{\22docs\22:\22interface docs\22,\22funcs\22:{\22[constructor]res\22:\22constructor docs\22,\22[method]res.m\22:\22method docs\22,\22[static]res.s\22:\22static func docs\22,\22f\22:\22interface func docs\22},\22types\22:{\22t\22:{\22docs\22:\22basic typedef docs\22},\22r\22:{\22docs\22:\22record typedef docs\22,\22items\22:{\22f1\22:\22record field docs\22}},\22fl\22:{\22items\22:{\22f1\22:\22flag docs\22}},\22v\22:{\22items\22:{\22c1\22:\22variant case docs\22}},\22e\22:{\22items\22:{\22c1\22:\22enum case docs\22}}}},\22other-comment-forms\22:{\22docs\22:\22other comment forms\5cn multi-line block\22,\22funcs\22:{\22multiple-lines-split\22:\22one doc line\5cnnon-doc in the middle\5cnanother doc line\22,\22mixed-forms\22:\22mixed forms; line doc\5cnplus block doc\5cn multi-line\22}}}}")
(@producers
(processed-by "wit-component" "$CARGO_PKG_VERSION")
)
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces/resources.wat
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
)
)
(export (;11;) "implicit-own-handles3" (type 10))
(@custom "package-docs" "\00{\22worlds\22:{\22implicit-own-handles3\22:{\22types\22:{\22a\22:{\22docs\22:\22there should only be one `list` type despite there looking like two\5cnlist types here\22}}}},\22interfaces\22:{\22implicit-own-handles2\22:{\22types\22:{\22a\22:{\22docs\22:\22the `own` return and list param should be the same `own`\22},\22b\22:{\22docs\22:\22same as above, even when the `list<b>` implicitly-defined `own` comes\5cnbefore an explicitly defined `own`\22},\22c\22:{\22docs\22:\22same as the above, the `own` argument should have the same type as the\5cnreturn value\22}},\22nested\22:{}}}}")
(@custom "package-docs" "\00{\22worlds\22:{\22implicit-own-handles3\22:{\22types\22:{\22a\22:{\22docs\22:\22there should only be one `list` type despite there looking like two\5cnlist types here\22}}}},\22interfaces\22:{\22implicit-own-handles2\22:{\22types\22:{\22a\22:{\22docs\22:\22the `own` return and list param should be the same `own`\22},\22b\22:{\22docs\22:\22same as above, even when the `list<b>` implicitly-defined `own` comes\5cnbefore an explicitly defined `own`\22},\22c\22:{\22docs\22:\22same as the above, the `own` argument should have the same type as the\5cnreturn value\22}}}}}")
(@producers
(processed-by "wit-component" "$CARGO_PKG_VERSION")
)
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/tests/interfaces/wasi-http.wat

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions crates/wit-parser/src/ast/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,23 +480,22 @@ impl<'a> Resolver<'a> {
let mut iface_id_order = Vec::new();
let mut world_id_order = Vec::new();
for name in order {
match names.get(name) {
Some(ast::AstItem::Interface(_)) => {
match names.get(name).unwrap() {
ast::AstItem::Interface(_) => {
let id = self.alloc_interface(package_items[name]);
self.interfaces[id].name = Some(name.to_string());
let prev = ids.insert(name, AstItem::Interface(id));
assert!(prev.is_none());
iface_id_order.push(id);
}
Some(ast::AstItem::World(_)) => {
ast::AstItem::World(_) => {
let id = self.alloc_world(package_items[name]);
self.worlds[id].name = name.to_string();
let prev = ids.insert(name, AstItem::World(id));
assert!(prev.is_none());
world_id_order.push(id);
}
Some(ast::AstItem::Use(_) | ast::AstItem::Package(_)) => unreachable!(),
None => {}
ast::AstItem::Use(_) | ast::AstItem::Package(_) => unreachable!(),
};
}
for decl_list in decl_lists {
Expand Down Expand Up @@ -923,7 +922,7 @@ impl<'a> Resolver<'a> {
TypeItem::Use(u) => {
self.resolve_use(owner, u)?;
}
_ => {}
TypeItem::Def(_) => {}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/wit-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ pub struct Interface {
pub name: Option<String>,

/// The nested interfaces in this interface.
#[serde(skip_serializing_if = "IndexMap::is_empty")]
pub nested: IndexMap<String, Nest>,
/// Exported types from this interface.
///
Expand Down
4 changes: 4 additions & 0 deletions crates/wit-parser/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ struct InterfaceMetadata {
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
types: StringMap<TypeMetadata>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "StringMap::is_empty")
)]
nested: StringMap<NestMetadata>,
}

Expand Down
1 change: 0 additions & 1 deletion crates/wit-parser/tests/ui/comments.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"interfaces": [
{
"name": "foo",
"nested": {},
"types": {
"x": 0,
"bar": 1
Expand Down
6 changes: 0 additions & 6 deletions crates/wit-parser/tests/ui/complex-include.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,42 +126,36 @@
"interfaces": [
{
"name": "a",
"nested": {},
"types": {},
"functions": {},
"package": 0
},
{
"name": "b",
"nested": {},
"types": {},
"functions": {},
"package": 0
},
{
"name": "a",
"nested": {},
"types": {},
"functions": {},
"package": 1
},
{
"name": "b",
"nested": {},
"types": {},
"functions": {},
"package": 1
},
{
"name": "ai",
"nested": {},
"types": {},
"functions": {},
"package": 2
},
{
"name": "bi",
"nested": {},
"types": {},
"functions": {},
"package": 2
Expand Down
2 changes: 0 additions & 2 deletions crates/wit-parser/tests/ui/cross-package-resource.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"interfaces": [
{
"name": "foo",
"nested": {},
"types": {
"r": 0
},
Expand All @@ -12,7 +11,6 @@
},
{
"name": "foo",
"nested": {},
"types": {
"r": 1,
"t": 2
Expand Down
2 changes: 0 additions & 2 deletions crates/wit-parser/tests/ui/diamond1.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
"interfaces": [
{
"name": "types",
"nested": {},
"types": {},
"functions": {},
"package": 0
},
{
"name": "types",
"nested": {},
"types": {},
"functions": {},
"package": 1
Expand Down
4 changes: 0 additions & 4 deletions crates/wit-parser/tests/ui/disambiguate-diamond.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"interfaces": [
{
"name": "shared1",
"nested": {},
"types": {
"the-type": 0
},
Expand All @@ -40,7 +39,6 @@
},
{
"name": "shared2",
"nested": {},
"types": {
"the-type": 1
},
Expand All @@ -49,7 +47,6 @@
},
{
"name": null,
"nested": {},
"types": {
"the-type": 2
},
Expand All @@ -58,7 +55,6 @@
},
{
"name": null,
"nested": {},
"types": {
"the-type": 3
},
Expand Down
6 changes: 0 additions & 6 deletions crates/wit-parser/tests/ui/feature-gates.wit.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"interfaces": [
{
"name": "ungated",
"nested": {},
"types": {},
"functions": {
"ungated": {
Expand All @@ -89,7 +88,6 @@
},
{
"name": "ungated2",
"nested": {},
"types": {
"ungated": 0,
"ungated2": 1
Expand All @@ -104,7 +102,6 @@
},
{
"name": "ungated-use-target",
"nested": {},
"types": {
"t": 2
},
Expand All @@ -118,7 +115,6 @@
},
{
"name": "ungated-use",
"nested": {},
"types": {
"t": 3
},
Expand All @@ -132,7 +128,6 @@
},
{
"name": "ungated-for-world",
"nested": {},
"types": {},
"functions": {},
"stability": {
Expand All @@ -144,7 +139,6 @@
},
{
"name": "with-resources",
"nested": {},
"types": {
"ungated": 4
},
Expand Down
Loading

0 comments on commit 226fa06

Please sign in to comment.