Skip to content

Commit b3da31d

Browse files
authored
Fix issue with world stubs (#756)
Signed-off-by: James Sturtevant <[email protected]>
1 parent 532c2df commit b3da31d

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

crates/csharp/src/lib.rs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -440,37 +440,50 @@ impl WorldGenerator for CSharp {
440440

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

443-
let generate_stub =
444-
|name: String, files: &mut Files, fragments: &Vec<InterfaceFragment>| {
445-
let stub_file_name = format!("{name}Impl");
446-
let interface_name = CSharp::get_class_name_from_qualified_name(name.clone());
447-
let stub_class_name = format!("{interface_name}Impl");
443+
let generate_stub = |name: String, files: &mut Files, stubs: Stubs| {
444+
let stub_file_name = format!("{name}Impl");
445+
let interface_name = CSharp::get_class_name_from_qualified_name(name.clone());
446+
let stub_class_name = format!("{interface_name}Impl");
447+
448+
let (fragments, fully_qaulified_namespace) = match stubs {
449+
Stubs::World(fragments) => {
450+
let fully_qaulified_namespace = format!("{namespace}");
451+
(fragments, fully_qaulified_namespace)
452+
}
453+
Stubs::Interface(fragments) => {
454+
let fully_qaulified_namespace = format!("{namespace}.{name}");
455+
(fragments, fully_qaulified_namespace)
456+
}
457+
};
448458

449-
let body = fragments
450-
.iter()
451-
.map(|f| f.stub.deref())
452-
.collect::<Vec<_>>()
453-
.join("\n");
459+
let body = fragments
460+
.iter()
461+
.map(|f| f.stub.deref())
462+
.collect::<Vec<_>>()
463+
.join("\n");
454464

455-
let body = format!(
456-
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
465+
let body = format!(
466+
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
457467
{CSHARP_IMPORTS}
458468
459-
namespace {namespace}.{name};
469+
namespace {fully_qaulified_namespace};
460470
461471
public partial class {stub_class_name} : {interface_name} {{
462472
{body}
463473
}}
464474
"
465-
);
475+
);
466476

467-
files.push(&format!("{stub_file_name}.cs"), indent(&body).as_bytes());
468-
};
477+
files.push(&format!("{stub_file_name}.cs"), indent(&body).as_bytes());
478+
};
469479

470-
// TODO: is the world Impl class useful?
471-
// if self.opts.generate_stub {
472-
// generate_stub(format!("{name}"), files);
473-
// }
480+
if self.opts.generate_stub {
481+
generate_stub(
482+
format!("{name}World"),
483+
files,
484+
Stubs::World(&self.world_fragments),
485+
);
486+
}
474487

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

531544
if interface_type_and_fragments.is_export && self.opts.generate_stub {
532-
generate_stub(format!("{name}"), files, fragments);
545+
generate_stub(format!("{name}"), files, Stubs::Interface(fragments));
533546
}
534547
}
535548
}
@@ -1259,6 +1272,11 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
12591272
}
12601273
}
12611274

1275+
enum Stubs<'a> {
1276+
World(&'a Vec<InterfaceFragment>),
1277+
Interface(&'a Vec<InterfaceFragment>),
1278+
}
1279+
12621280
struct Block {
12631281
_body: String,
12641282
_results: Vec<String>,

crates/csharp/tests/codegen.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ macro_rules! codegen_test {
5252
"result-empty",
5353
"ret-areas",
5454
"return-resource-from-export",
55-
"same-names2",
5655
"same-names5",
5756
"simple-functions",
5857
"simple-http",
5958
"simple-lists",
6059
"small-anonymous",
61-
"smoke-default",
6260
"strings",
6361
"unused-import",
6462
"use-across-interfaces",

0 commit comments

Comments
 (0)