Skip to content
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

Support multiple versions of the same interface in C #695

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,25 +1276,39 @@ pub fn owner_namespace(
) -> Option<String> {
let ty = &resolve.types[id];
match ty.owner {
TypeOwner::Interface(owner) => {
Some(interface_identifier(&interface_names[&owner], resolve))
}
TypeOwner::Interface(owner) => Some(interface_identifier(
&interface_names[&owner],
resolve,
false,
)),
TypeOwner::World(owner) => Some(resolve.worlds[owner].name.to_snake_case()),
TypeOwner::None => None,
}
}

fn interface_identifier(interface_id: &WorldKey, resolve: &Resolve) -> String {
fn interface_identifier(interface_id: &WorldKey, resolve: &Resolve, in_export: bool) -> String {
match interface_id {
WorldKey::Name(name) => name.to_snake_case(),
WorldKey::Interface(id) => {
let mut ns = String::new();
if in_export {
ns.push_str("exports_");
}
let iface = &resolve.interfaces[*id];
let pkg = &resolve.packages[iface.package.unwrap()];
ns.push_str(&pkg.name.namespace.to_snake_case());
ns.push_str("_");
ns.push_str(&pkg.name.name.to_snake_case());
ns.push_str("_");
if let Some(version) = &pkg.name.version {
let version = version
.to_string()
.replace('.', "_")
.replace('-', "_")
.replace('+', "_");
ns.push_str(&version);
ns.push_str("_");
}
ns.push_str(&iface.name.as_ref().unwrap().to_snake_case());
ns
}
Expand Down Expand Up @@ -1638,22 +1652,10 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
}

impl InterfaceGenerator<'_> {
fn c_func_name(&self, interface_name: Option<&WorldKey>, func: &Function) -> String {
fn c_func_name(&self, interface_id: Option<&WorldKey>, func: &Function) -> String {
let mut name = String::new();
match interface_name {
Some(WorldKey::Name(k)) => name.push_str(&k.to_snake_case()),
Some(WorldKey::Interface(id)) => {
if !self.in_import {
name.push_str("exports_");
}
let iface = &self.resolve.interfaces[*id];
let pkg = &self.resolve.packages[iface.package.unwrap()];
name.push_str(&pkg.name.namespace.to_snake_case());
name.push_str("_");
name.push_str(&pkg.name.name.to_snake_case());
name.push_str("_");
name.push_str(&iface.name.as_ref().unwrap().to_snake_case());
}
match interface_id {
Some(id) => name.push_str(&interface_identifier(id, &self.resolve, !self.in_import)),
None => name.push_str(&self.gen.world.to_snake_case()),
}
name.push_str("_");
Expand Down
2 changes: 0 additions & 2 deletions crates/c/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ macro_rules! codegen_test {
(resource_local_alias $name:tt $test:tt) => {};
(resources_with_lists $name:tt $test:tt) => {};
(resources_in_aggregates $name:tt $test:tt) => {};
(multiversion $name:tt $test:tt) => {};

($id:ident $name:tt $test:tt) => {
#[test]
fn $id() {
Expand Down
Loading