Skip to content

Commit

Permalink
Fix issue with world stubs
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Nov 16, 2023
1 parent 532c2df commit 9598faf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
60 changes: 39 additions & 21 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,37 +440,50 @@ impl WorldGenerator for CSharp {

files.push(&format!("{name}.cs"), indent(&src).as_bytes());

let generate_stub =
|name: String, files: &mut Files, fragments: &Vec<InterfaceFragment>| {
let stub_file_name = format!("{name}Impl");
let interface_name = CSharp::get_class_name_from_qualified_name(name.clone());
let stub_class_name = format!("{interface_name}Impl");
let generate_stub = |name: String, files: &mut Files, stubs: Stubs| {
let stub_file_name = format!("{name}Impl");
let interface_name = CSharp::get_class_name_from_qualified_name(name.clone());
let stub_class_name = format!("{interface_name}Impl");

let (fragments, fully_qaulified_namespace) = match stubs {
Stubs::World(fragments) => {
let fully_qaulified_namespace = format!("{namespace}");
(fragments, fully_qaulified_namespace)
}
Stubs::Interface(fragments) => {
let fully_qaulified_namespace = format!("{namespace}.{name}");
(fragments, fully_qaulified_namespace)
}
};

let body = fragments
.iter()
.map(|f| f.stub.deref())
.collect::<Vec<_>>()
.join("\n");
let body = fragments
.iter()
.map(|f| f.stub.deref())
.collect::<Vec<_>>()
.join("\n");

let body = format!(
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
let body = format!(
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
{CSHARP_IMPORTS}
namespace {namespace}.{name};
namespace {fully_qaulified_namespace};
public partial class {stub_class_name} : {interface_name} {{
{body}
}}
"
);
);

files.push(&format!("{stub_file_name}.cs"), indent(&body).as_bytes());
};
files.push(&format!("{stub_file_name}.cs"), indent(&body).as_bytes());
};

// TODO: is the world Impl class useful?
// if self.opts.generate_stub {
// generate_stub(format!("{name}"), files);
// }
if self.opts.generate_stub {
generate_stub(
format!("{name}World"),
files,
Stubs::World(&self.world_fragments),
);
}

files.push(
&format!("{snake}_component_type.o",),
Expand Down Expand Up @@ -529,7 +542,7 @@ impl WorldGenerator for CSharp {
files.push(&format!("{name}Interop.cs"), indent(&body).as_bytes());

if interface_type_and_fragments.is_export && self.opts.generate_stub {
generate_stub(format!("{name}"), files, fragments);
generate_stub(format!("{name}"), files, Stubs::Interface(fragments));
}
}
}
Expand Down Expand Up @@ -1259,6 +1272,11 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
}
}

enum Stubs<'a> {
World(&'a Vec<InterfaceFragment>),
Interface(&'a Vec<InterfaceFragment>),
}

struct Block {
_body: String,
_results: Vec<String>,
Expand Down
2 changes: 0 additions & 2 deletions crates/csharp/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ macro_rules! codegen_test {
"result-empty",
"ret-areas",
"return-resource-from-export",
"same-names2",
"same-names5",
"simple-functions",
"simple-http",
"simple-lists",
"small-anonymous",
"smoke-default",
"strings",
"unused-import",
"use-across-interfaces",
Expand Down

0 comments on commit 9598faf

Please sign in to comment.