Skip to content

Commit

Permalink
fixed generator used for cargo component new to include bindings fo…
Browse files Browse the repository at this point in the history
…r resources that have no methods and incorrectly did borrow type when inlined in world (#304)
  • Loading branch information
calvinrp authored May 24, 2024
1 parent 5c923af commit d6f1496
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ impl<'a> UnimplementedFunction<'a> {

fn is_exported_resource(&self, id: TypeId) -> bool {
let type_info = &self.resolve.types[id];
if let TypeOwner::World(_) = type_info.owner {
return false;
}
match type_info.kind {
TypeDefKind::Type(Type::Id(kind_id)) => {
let kind_type = &self.resolve.types[kind_id];
Expand Down Expand Up @@ -573,6 +576,23 @@ impl<'a> InterfaceGenerator<'a> {
}
}

// Add resources that did not have any methods
for (_, id) in interface.types.iter() {
let ty = &resolve.types[*id];
if ty.kind == TypeDefKind::Resource && !resources.contains_key(id) {
let name = ty.name.as_deref().expect("unnamed resource type");
let impl_name = names.reserve(name);
resources.insert(
*id,
Resource {
ty,
impl_name,
functions: vec![],
},
);
}
}

Self {
resolve,
key,
Expand Down
13 changes: 13 additions & 0 deletions tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,20 @@ interface baz {
z: func(b: borrow<b>);
}
interface another {
resource yet-another {
hello: func() -> string;
}
resource empty {}
}
world not-used {
export not-exported;
}
world foo {
use another.{yet-another, empty};
resource file {
open: static func(path: string) -> file;
path: func() -> string;
Expand All @@ -223,6 +232,10 @@ world foo {
export baz;
export is-exported;
export is-exported-and-aliased;
export another;
export another-func: func(yet: borrow<yet-another>) -> result;
export another-func-empty: func(empty: borrow<empty>) -> result;
}"#,
true,
)
Expand Down

0 comments on commit d6f1496

Please sign in to comment.