Skip to content

Commit ebe6b8a

Browse files
Saalvagetritao
authored andcommitted
Fix value type out parameters
1 parent 1710202 commit ebe6b8a

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,17 +634,20 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
634634
{
635635
if (Context.Parameter.Usage == ParameterUsage.Out)
636636
{
637-
var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter);
638-
Context.Before.WriteLine("var {0} = new {1}.{2}();",
639-
arg, qualifiedIdentifier, Helpers.InternalStruct);
637+
Context.Before.WriteLine("fixed ({0}.{1}* {2} = &{3}.{4})",
638+
Context.Parameter.QualifiedType, Helpers.InternalStruct,
639+
arg, Context.Parameter.Name, Helpers.InstanceIdentifier);
640+
Context.Before.WriteOpenBraceAndIndent();
641+
Context.Return.Write($"new {typePrinter.IntPtrType}({arg})");
642+
Context.Cleanup.UnindentAndWriteCloseBrace();
640643
}
641644
else
642645
{
643646
Context.Before.WriteLine("var {0} = {1}.{2};",
644647
arg, Context.Parameter.Name, Helpers.InstanceIdentifier);
648+
Context.Return.Write($"new {typePrinter.IntPtrType}(&{arg})");
645649
}
646650

647-
Context.Return.Write($"new {typePrinter.IntPtrType}(&{arg})");
648651
return true;
649652
}
650653

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public override bool VisitClassDecl(Class @class)
413413
if (@class.IsValueType)
414414
{
415415
WriteLine($"private {@class.Name}.{Helpers.InternalStruct} {Helpers.InstanceField};");
416-
WriteLine($"internal {@class.Name}.{Helpers.InternalStruct} {Helpers.InstanceIdentifier} => {Helpers.InstanceField};");
416+
WriteLine($"internal ref {@class.Name}.{Helpers.InternalStruct} {Helpers.InstanceIdentifier} => ref {Helpers.InstanceField};");
417417
}
418418
else
419419
{

tests/dotnet/CSharp/CSharp.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,4 +1995,11 @@ public void TestPointerToClass()
19951995
Assert.IsTrue(CSharp.CSharp.PointerToClass.IsDefaultInstance);
19961996
Assert.IsTrue(CSharp.CSharp.PointerToClass.IsValid);
19971997
}
1998+
1999+
[Test]
2000+
public void TestValueTypeOutParameter()
2001+
{
2002+
CSharp.CSharp.ValueTypeOutParameter(out var unionTest);
2003+
Assert.AreEqual(2, unionTest.A);
2004+
}
19982005
}

tests/dotnet/CSharp/CSharp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,3 +1791,8 @@ bool PointerTester::IsValid()
17911791
}
17921792

17931793
PointerTester* PointerToClass = &internalPointerTesterInstance;
1794+
1795+
void ValueTypeOutParameter(UnionTester* tester)
1796+
{
1797+
tester->a = 2;
1798+
}

tests/dotnet/CSharp/CSharp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,10 @@ class DLL_API PointerTester
16031603
};
16041604

16051605
DLL_API extern PointerTester* PointerToClass;
1606+
1607+
union DLL_API UnionTester {
1608+
float a;
1609+
int b;
1610+
};
1611+
1612+
void DLL_API ValueTypeOutParameter(CS_OUT UnionTester* tester);

0 commit comments

Comments
 (0)