Skip to content

Commit ce3d04a

Browse files
authored
CSharpSources: Dereference pointer variables (#1753)
Dereference pointers when generating getters for pointer variables. Otherwise, the managed instance would point to the pointer itself rather than the object at the native instance's address.
1 parent 3978fb3 commit ce3d04a

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,17 @@ private bool GenerateVariableGetter(Variable var)
12851285
};
12861286
ctx.PushMarshalKind(MarshalKind.ReturnVariableArray);
12871287

1288+
if (var.Type.Desugar().IsPointer())
1289+
{
1290+
var pointerType = var.Type.Desugar() as PointerType;
1291+
while (pointerType != null && !pointerType.Pointee.Desugar().IsPrimitiveType(PrimitiveType.Char))
1292+
{
1293+
ptr = $"*{ptr}";
1294+
pointerType = pointerType.Pointee.Desugar() as PointerType;
1295+
}
1296+
ptr = $"(IntPtr*)({ptr})";
1297+
}
1298+
12881299
var arrayType = var.Type.Desugar() as ArrayType;
12891300
var isRefTypeArray = arrayType != null && var.Namespace is Class context && context.IsRefType;
12901301
var elementType = arrayType?.Type.Desugar();

tests/dotnet/CSharp/CSharp.Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,4 +1988,11 @@ public void TestCallByValueCppToCSharpPointer()
19881988
Assert.That(RuleOfThreeTester.CopyConstructorCalls, Is.EqualTo(0));
19891989
Assert.That(RuleOfThreeTester.CopyAssignmentCalls, Is.EqualTo(0));
19901990
}
1991+
1992+
[Test]
1993+
public void TestPointerToClass()
1994+
{
1995+
Assert.IsTrue(CSharp.CSharp.PointerToClass.IsDefaultInstance);
1996+
Assert.IsTrue(CSharp.CSharp.PointerToClass.IsValid);
1997+
}
19911998
}

tests/dotnet/CSharp/CSharp.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,3 +1772,22 @@ void CallCallByValueInterfacePointer(CallByValueInterface* interface)
17721772
RuleOfThreeTester value;
17731773
interface->CallByPointer(&value);
17741774
}
1775+
1776+
static PointerTester internalPointerTesterInstance;
1777+
1778+
PointerTester::PointerTester()
1779+
{
1780+
a = 0;
1781+
}
1782+
1783+
bool PointerTester::IsDefaultInstance()
1784+
{
1785+
return this == &internalPointerTesterInstance;
1786+
}
1787+
1788+
bool PointerTester::IsValid()
1789+
{
1790+
return a == 0;
1791+
}
1792+
1793+
PointerTester* PointerToClass = &internalPointerTesterInstance;

tests/dotnet/CSharp/CSharp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,3 +1592,14 @@ struct DLL_API CallByValueInterface {
15921592
void DLL_API CallCallByValueInterfaceValue(CallByValueInterface*);
15931593
void DLL_API CallCallByValueInterfaceReference(CallByValueInterface*);
15941594
void DLL_API CallCallByValueInterfacePointer(CallByValueInterface*);
1595+
1596+
class DLL_API PointerTester
1597+
{
1598+
int a;
1599+
public:
1600+
PointerTester();
1601+
bool IsDefaultInstance();
1602+
bool IsValid();
1603+
};
1604+
1605+
DLL_API extern PointerTester* PointerToClass;

0 commit comments

Comments
 (0)