Skip to content

Commit

Permalink
Fix value type out parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Saalvage authored and tritao committed Oct 18, 2023
1 parent 1710202 commit ebe6b8a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,17 +634,20 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
{
if (Context.Parameter.Usage == ParameterUsage.Out)
{
var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter);
Context.Before.WriteLine("var {0} = new {1}.{2}();",
arg, qualifiedIdentifier, Helpers.InternalStruct);
Context.Before.WriteLine("fixed ({0}.{1}* {2} = &{3}.{4})",
Context.Parameter.QualifiedType, Helpers.InternalStruct,
arg, Context.Parameter.Name, Helpers.InstanceIdentifier);
Context.Before.WriteOpenBraceAndIndent();
Context.Return.Write($"new {typePrinter.IntPtrType}({arg})");
Context.Cleanup.UnindentAndWriteCloseBrace();
}
else
{
Context.Before.WriteLine("var {0} = {1}.{2};",
arg, Context.Parameter.Name, Helpers.InstanceIdentifier);
Context.Return.Write($"new {typePrinter.IntPtrType}(&{arg})");
}

Context.Return.Write($"new {typePrinter.IntPtrType}(&{arg})");
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public override bool VisitClassDecl(Class @class)
if (@class.IsValueType)
{
WriteLine($"private {@class.Name}.{Helpers.InternalStruct} {Helpers.InstanceField};");
WriteLine($"internal {@class.Name}.{Helpers.InternalStruct} {Helpers.InstanceIdentifier} => {Helpers.InstanceField};");
WriteLine($"internal ref {@class.Name}.{Helpers.InternalStruct} {Helpers.InstanceIdentifier} => ref {Helpers.InstanceField};");
}
else
{
Expand Down
7 changes: 7 additions & 0 deletions tests/dotnet/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1995,4 +1995,11 @@ public void TestPointerToClass()
Assert.IsTrue(CSharp.CSharp.PointerToClass.IsDefaultInstance);
Assert.IsTrue(CSharp.CSharp.PointerToClass.IsValid);
}

[Test]
public void TestValueTypeOutParameter()
{
CSharp.CSharp.ValueTypeOutParameter(out var unionTest);
Assert.AreEqual(2, unionTest.A);
}
}
5 changes: 5 additions & 0 deletions tests/dotnet/CSharp/CSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1791,3 +1791,8 @@ bool PointerTester::IsValid()
}

PointerTester* PointerToClass = &internalPointerTesterInstance;

void ValueTypeOutParameter(UnionTester* tester)
{
tester->a = 2;
}
7 changes: 7 additions & 0 deletions tests/dotnet/CSharp/CSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,10 @@ class DLL_API PointerTester
};

DLL_API extern PointerTester* PointerToClass;

union DLL_API UnionTester {
float a;
int b;
};

void DLL_API ValueTypeOutParameter(CS_OUT UnionTester* tester);

0 comments on commit ebe6b8a

Please sign in to comment.