Skip to content
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
5 changes: 5 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
6 changes: 6 additions & 0 deletions external/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<!--
This is intentionally blank, so any changes locally don't get applied to
any external projects that reference this one.
-->
</Project>
15 changes: 13 additions & 2 deletions src/InteropGen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void Generate(NativeModel model, CodeGen gen)
}


bool TryMapCommonType(NativeType type, bool allowStrings, out string res)
bool TryMapCommonType(NativeType type, bool allowStrings, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out string res)
{
string? Map()
{
Expand Down Expand Up @@ -296,6 +296,11 @@ string MapDotnetType(NativeType type, bool allowHandles)

void GenerateHandleReturn(NativeHandle nativeHandleType)
{
// Ownership follows the "New" naming rule, NOT the method/factory
// classification: a New-suffixed function whose first parameter is a
// handle is modeled as an instance method (isFactory == false) but still
// returns a freshly-created, caller-owned handle. Using isFactory here
// would emit RetainFromNative for those and over-retain (leak).
if (f.Name.EndsWith("New"))
gen.Line($"return new {nativeHandleType.Name}(ret);");
else
Expand All @@ -308,7 +313,13 @@ void GenerateHandleReturn(NativeHandle nativeHandleType)
GenerateHandleReturn(nativeHandle);
else if(f.ReturnType is NativeNullableType { ElementType: NativeHandle nativeHandleType})
{
gen.Line("if(ret == null) return null;");
// Mirror GenerateHandleReturn's New-based branch: New-suffixed
// functions return a SafeHandle from P/Invoke (compare to null);
// other handle-returning functions return a raw IntPtr (compare to IntPtr.Zero).
if (f.Name.EndsWith("New"))
gen.Line("if(ret == null) return null;");
else
gen.Line("if(ret == global::System.IntPtr.Zero) return null;");
GenerateHandleReturn(nativeHandleType);
}
else
Expand Down
10 changes: 5 additions & 5 deletions src/InteropGen/NativeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class NativeEnum(string name) : NativeType

class NativeHandle : NativeType
{
public string Name { get; set; }
public required string Name { get; set; }
public List<NativeFunction> Methods { get; set; } = new();
public List<NativeFunction> Factories { get; set; } = new();
public override string ToString() => Name;
Expand All @@ -95,7 +95,7 @@ class ExternalNativeType(string name) : NativeType

class NativeFunctionPointer : NativeType
{
public NativeType ReturnType { get; set; }
public required NativeType ReturnType { get; set; }
public List<NativeVar> Parameters { get; set; } = new();
}

Expand All @@ -108,9 +108,9 @@ class NativeNamedFunctionPointer(string name) : NativeFunctionPointer

class NativeFunction
{
public string Name { get; set; }
public NativeType ReturnType { get; set; }
public List<NativeVar> Parameters { get; set; }
public required string Name { get; set; }
public required NativeType ReturnType { get; set; }
public required List<NativeVar> Parameters { get; set; }
}

class NativeModel
Expand Down
Loading