Skip to content

Commit

Permalink
tidy usings, update to net10
Browse files Browse the repository at this point in the history
  • Loading branch information
yowl committed Nov 12, 2024
1 parent ffd21d0 commit 162371b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/csharp/src/csproj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl CSProjectLLVMBuilder {
csproj.push_str(
r#"
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="9.0.0-rc.1.24412.1" />
<PackageReference Include="runtime.win-x64.Microsoft.DotNet.ILCompiler.LLVM" Version="9.0.0-rc.1.24412.1" />
<PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-*" />
<PackageReference Include="runtime.win-x64.Microsoft.DotNet.ILCompiler.LLVM" Version="10.0.0-*" />
</ItemGroup>
"#,
);
Expand Down
74 changes: 61 additions & 13 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ struct InterfaceFragment {
csharp_src: String,
csharp_interop_src: String,
stub: String,
usings: HashSet<String>,
interop_usings: HashSet<String>,
}

pub struct InterfaceTypeAndFragments {
Expand Down Expand Up @@ -121,8 +123,8 @@ pub enum FunctionLevel {
pub struct CSharp {
opts: Opts,
name: String,
//TODO remove unused
usings: HashSet<String>,
interop_usings: HashSet<String>,
return_area_size: usize,
return_area_align: usize,
tuple_counts: HashSet<usize>,
Expand Down Expand Up @@ -170,6 +172,8 @@ impl CSharp {
resolve,
name,
direction,
usings: HashSet::<String>::new(),
interop_usings: HashSet::<String>::new(),
}
}

Expand All @@ -193,6 +197,13 @@ impl CSharp {
self.usings.insert(using_ns_string);
}
}

fn require_interop_using(&mut self, using_ns: &str) {
if !self.interop_usings.contains(using_ns) {
let using_ns_string = using_ns.to_string();
self.interop_usings.insert(using_ns_string);
}
}
}

impl WorldGenerator for CSharp {
Expand Down Expand Up @@ -407,7 +418,6 @@ impl WorldGenerator for CSharp {
uwrite!(
src,
"
namespace {world_namespace} {{
{access} interface I{name}World {{
Expand All @@ -423,6 +433,9 @@ impl WorldGenerator for CSharp {
.join("\n"),
);

let usings: Vec<_> = self.world_fragments .iter() .flat_map(|f| &f.usings) .cloned() .collect();
usings.iter().for_each(|u| { self.require_using(u); });

let mut producers = wasm_metadata::Producers::empty();
producers.add(
"processed-by",
Expand Down Expand Up @@ -527,6 +540,7 @@ impl WorldGenerator for CSharp {

if self.needs_interop_string {
self.require_using("System.Text");
self.require_using("System.Runtime.InteropServices");
uwrite!(
src,
r#"
Expand Down Expand Up @@ -611,6 +625,14 @@ impl WorldGenerator for CSharp {
src.push_str("\n");

src.push_str("namespace exports {\n");

src.push_str(&self.world_fragments
.iter()
.flat_map(|f| &f.interop_usings)
.map(|s| "using ".to_owned() + s + ";")
.collect::<Vec<String>>()
.join("\n"));

src.push_str(&format!("{access} static class {name}World\n"));
src.push_str("{");

Expand Down Expand Up @@ -809,9 +831,8 @@ impl WorldGenerator for CSharp {
{body}
}}
",
&self
.usings
.iter()
fragments.iter()
.flat_map(|f| &f.usings)
.map(|s| "using ".to_owned() + s + ";")
.collect::<Vec<String>>()
.join("\n"),
Expand Down Expand Up @@ -839,12 +860,12 @@ impl WorldGenerator for CSharp {
}}
}}
",
&self
.usings
fragments
.iter()
.flat_map(|f| &f.interop_usings)
.map(|s| "using ".to_owned() + s + ";\n")
.collect::<Vec<String>>()
.join("\n"),
.join(""),
);

files.push(
Expand All @@ -869,6 +890,8 @@ struct InterfaceGenerator<'a> {
resolve: &'a Resolve,
name: &'a str,
direction: Direction,
usings: HashSet<String>,
interop_usings: HashSet<String>,
}

impl InterfaceGenerator<'_> {
Expand Down Expand Up @@ -980,6 +1003,8 @@ impl InterfaceGenerator<'_> {
csharp_src: self.src,
csharp_interop_src: self.csharp_interop_src,
stub: self.stub,
usings: self.usings,
interop_usings: self.interop_usings,
});
}

Expand All @@ -988,6 +1013,8 @@ impl InterfaceGenerator<'_> {
csharp_src: self.src,
csharp_interop_src: self.csharp_interop_src,
stub: self.stub,
usings: self.usings,
interop_usings: self.interop_usings,
});
}

Expand Down Expand Up @@ -1106,11 +1133,11 @@ impl InterfaceGenerator<'_> {

let import_name = &func.name;

self.gen.require_using("System.Runtime.InteropServices");

let target = if let FunctionKind::Freestanding = &func.kind {
self.require_interop_using("System.Runtime.InteropServices");
&mut self.csharp_interop_src
} else {
self.require_using("System.Runtime.InteropServices");
&mut self.src
};

Expand Down Expand Up @@ -1255,7 +1282,7 @@ impl InterfaceGenerator<'_> {
let export_name = func.legacy_core_export_name(core_module_name.as_deref());
let access = self.gen.access_modifier();

self.gen.require_using("System.Runtime.InteropServices");
self.require_interop_using("System.Runtime.InteropServices");
uwrite!(
self.csharp_interop_src,
r#"
Expand Down Expand Up @@ -1456,6 +1483,20 @@ impl InterfaceGenerator<'_> {
}
}

fn require_using(&mut self, using_ns: &str) {
if !self.usings.contains(using_ns) {
let using_ns_string = using_ns.to_string();
self.usings.insert(using_ns_string);
}
}

fn require_interop_using(&mut self, using_ns: &str) {
if !self.interop_usings.contains(using_ns) {
let using_ns_string = using_ns.to_string();
self.interop_usings.insert(using_ns_string);
}
}

fn start_resource(&mut self, id: TypeId, key: Option<&WorldKey>) {
let access = self.gen.access_modifier();
let qualified = self.type_name_with_qualifier(&Type::Id(id), true);
Expand All @@ -1471,6 +1512,7 @@ impl InterfaceGenerator<'_> {
.map(|key| self.resolve.name_world_key(key))
.unwrap_or_else(|| "$root".into());

self.require_using("System.Runtime.InteropServices");
// As of this writing, we cannot safely drop a handle to an imported resource from a .NET finalizer
// because it may still have one or more open child resources. Once WIT has explicit syntax for
// indicating parent/child relationships, we should be able to use that information to keep track
Expand Down Expand Up @@ -1509,7 +1551,7 @@ impl InterfaceGenerator<'_> {
.map(|s| format!("{}#", self.resolve.name_world_key(s)))
.unwrap_or_else(String::new);

self.gen.require_using("System.Runtime.InteropServices");
self.require_interop_using("System.Runtime.InteropServices");
uwrite!(
self.csharp_interop_src,
r#"
Expand All @@ -1528,6 +1570,7 @@ impl InterfaceGenerator<'_> {
.map(|key| format!("[export]{}", self.resolve.name_world_key(key)))
.unwrap_or_else(|| "[export]$root".into());

self.require_using("System.Runtime.InteropServices");
// The ergonomics of exported resources are not ideal, currently. Implementing such a resource
// requires both extending a class and implementing an interface. The reason for the class is to
// allow implementers to inherit code which tracks and disposes of the resource handle; the reason
Expand Down Expand Up @@ -2613,7 +2656,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
}

Instruction::StringLift { .. } => {
self.gen.gen.require_using("System.Text");
if FunctionKind::Freestanding == *self.kind || self.gen.direction == Direction::Export {
self.gen.require_interop_using("System.Text");
} else {
self.gen.require_using("System.Text");
};

results.push(format!(
"Encoding.UTF8.GetString((byte*){}, {})",
operands[0], operands[1]
Expand Down

0 comments on commit 162371b

Please sign in to comment.