Browse Source

Fix wrong marshalling for static member arrays (#1428)

* Fix wrong marshalling for static member arrays
pull/1431/head
josetr 5 years ago committed by GitHub
parent
commit
397811dc3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 14
      tests/CSharp/CSharp.Tests.cs
  3. 10
      tests/CSharp/CSharp.cpp
  4. 13
      tests/CSharp/CSharp.h

10
src/Generator/Generators/CSharp/CSharpSources.cs

@ -880,12 +880,12 @@ namespace CppSharp.Generators.CSharp @@ -880,12 +880,12 @@ namespace CppSharp.Generators.CSharp
var arrayType = var.Type as ArrayType;
string ptr = Generator.GeneratedIdentifier("ptr");
if (arrayType != null && var.Namespace is Class @class && @class.IsRefType)
if (arrayType != null)
{
WriteLine($@"var {ptr} = {
(arrayType.Type.IsPrimitiveType(PrimitiveType.Char) &&
arrayType.QualifiedType.Qualifiers.IsConst ?
string.Empty : "(byte*)")}{location};");
if (arrayType.Type.IsPrimitiveType(PrimitiveType.Char) && arrayType.SizeType != ArrayType.ArraySize.Constant)
WriteLine($@"var {ptr} = {location};");
else
WriteLine($@"var {ptr} = ({arrayType.Type.Visit(TypePrinter)}*){location};");
}
else
{

14
tests/CSharp/CSharp.Tests.cs

@ -734,6 +734,20 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -734,6 +734,20 @@ public unsafe class CSharpTests : GeneratorTestFixture
Assert.That(CSharp.CSharp.VariableWithVariablePrimitiveArray[1], Is.EqualTo(20));
}
[Test]
public void TestStaticVariables()
{
Assert.That(StaticVariables.Boolean, Is.EqualTo(true));
Assert.That(StaticVariables.Chr, Is.EqualTo('G'));
Assert.That(StaticVariables.UChr, Is.EqualTo('G'));
Assert.That(StaticVariables.Int, Is.EqualTo(1020304050));
Assert.That(StaticVariables.Float, Is.EqualTo(0.5020f));
Assert.That(StaticVariables.String, Is.EqualTo("Str"));
Assert.That(StaticVariables.ChrArray, Is.EqualTo(new[] { 'A', 'B'}));
Assert.That(StaticVariables.IntArray, Is.EqualTo(new[] { 1020304050 , 1526374850 }));
Assert.That(StaticVariables.FloatArray, Is.EqualTo(new[] { 0.5020f, 0.6020f }));
}
[Test]
public void TestPointerPassedAsItsSecondaryBase()
{

10
tests/CSharp/CSharp.cpp

@ -1400,6 +1400,16 @@ bool HasVirtualTakesReturnsProblematicTypes::callsVirtualToReturnBool(bool b) @@ -1400,6 +1400,16 @@ bool HasVirtualTakesReturnsProblematicTypes::callsVirtualToReturnBool(bool b)
extern const unsigned char variableWithFixedPrimitiveArray[2] = { 5, 10 };
extern const unsigned int variableWithVariablePrimitiveArray[] = { 15, 20 };
const bool StaticVariables::Boolean = true;
const char StaticVariables::Chr = 'G';
const unsigned char StaticVariables::UChr = (unsigned char)'G';
const int StaticVariables::Int = 1020304050;
const float StaticVariables::Float = 0.5020f;
const std::string StaticVariables::String = "Str";
const char StaticVariables::ChrArray[2] { 'A', 'B' };
const int StaticVariables::IntArray[2] { 1020304050, 1526374850 };
const float StaticVariables::FloatArray[2] { 0.5020f, 0.6020f };
TestString::TestString() : unicodeConst(L"ქართული ენა"), unicode(0)
{
}

13
tests/CSharp/CSharp.h

@ -1101,6 +1101,19 @@ public: @@ -1101,6 +1101,19 @@ public:
DLL_API extern const unsigned char variableWithFixedPrimitiveArray[2];
DLL_API extern const unsigned int variableWithVariablePrimitiveArray[];
class DLL_API StaticVariables {
public:
static const bool Boolean;
static const char Chr;
static const unsigned char UChr;
static const int Int;
static const float Float;
static const std::string String;
static const char ChrArray[2];
static const int IntArray[2];
static const float FloatArray[2];
};
typedef void (*ALLCAPS_UNDERSCORES)(int i);
class DLL_API TestString

Loading…
Cancel
Save