From bcd6b226faf49c4fd36a6c3155b6cc58d35d4a7e Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sun, 11 Sep 2016 12:33:29 +0300 Subject: [PATCH] Fixed the marshalling of arrays. Fixes https://github.com/mono/CppSharp/issues/680. Signed-off-by: Dimitar Dobrev --- src/Generator/Generators/CSharp/CSharpSources.cs | 4 +++- src/Generator/Generators/CSharp/CSharpTypePrinter.cs | 9 +++++---- tests/CSharp/CSharp.Tests.cs | 7 +++++++ tests/CSharp/CSharp.cpp | 2 ++ tests/CSharp/CSharp.h | 2 ++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 66c6a4e9..3c140fb5 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -1169,7 +1169,9 @@ namespace CppSharp.Generators.CSharp var ctx = new CSharpMarshalContext(Context) { ArgName = decl.Name, - ReturnVarName = (isRefTypeArray ? string.Empty : "*") + Generator.GeneratedIdentifier("ptr"), + ReturnVarName = (isRefTypeArray || + (arrayType != null && arrayType.Type.Desugar().IsPrimitiveType()) ? string.Empty : "*") + + Generator.GeneratedIdentifier("ptr"), ReturnType = new QualifiedType(var.Type) }; diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 5cdd0981..002c395a 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -139,14 +139,15 @@ namespace CppSharp.Generators.CSharp Type arrayType = array.Type.Desugar(); PrimitiveType primitiveType; - if (arrayType.IsPointerToPrimitiveType(out primitiveType) && - !(arrayType is FunctionType)) + if ((arrayType.IsPointerToPrimitiveType(out primitiveType) && + !(arrayType is FunctionType)) || + (arrayType.IsPrimitiveType() && MarshalKind != CSharpMarshalKind.NativeField)) { if (primitiveType == PrimitiveType.Void) { - return "void**"; + return "void*"; } - return string.Format("{0}*", array.Type.Visit(this, quals)); + return string.Format("{0}", array.Type.Visit(this, quals)); } if (TypePrinterContext.Parameter != null) diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index a0f34e9d..da491b95 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -555,6 +555,13 @@ public unsafe class CSharpTests : GeneratorTestFixture } } + [Test] + public void TestConstantArray() + { + Assert.That(CSharp.CSharp.VariableWithFixedPrimitiveArray[0], Is.EqualTo(5)); + Assert.That(CSharp.CSharp.VariableWithFixedPrimitiveArray[1], Is.EqualTo(10)); + } + [Test] public void TestOverrideVirtualWithString() { diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index fc686a34..46dea891 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -1064,3 +1064,5 @@ bool HasVirtualTakesReturnsProblematicTypes::callsVirtualToReturnBool(bool b) { return virtualTakesAndReturnsBool(b); } + +extern const unsigned char variableWithFixedPrimitiveArray[2] = { 5, 10 }; diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 4f30b830..b5ae92bb 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -980,3 +980,5 @@ public: virtual bool virtualTakesAndReturnsBool(bool b); bool callsVirtualToReturnBool(bool b); }; + +extern const unsigned char DLL_API variableWithFixedPrimitiveArray[2];