Browse Source

Worked around an MS .NET bug which prevents marshalling bools in certain cases.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/550/head
Dimitar Dobrev 10 years ago
parent
commit
91b3a5aef1
  1. 10
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 19
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 5
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 9
      tests/Common/Common.Tests.cs
  5. 10
      tests/Common/Common.cpp
  6. 11
      tests/Common/Common.h

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

@ -208,11 +208,16 @@ namespace CppSharp.Generators.CSharp @@ -208,11 +208,16 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.Void:
return true;
case PrimitiveType.Char:
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
if (Context.Driver.Options.MarshalCharAsManagedChar)
Context.Return.Write("(char) ");
goto default;
case PrimitiveType.Char16:
return false;
case PrimitiveType.Bool:
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
Context.Return.Write("{0} != 0", Context.ReturnVarName);
return true;
default:
Context.Return.Write(Context.ReturnVarName);
return true;
@ -533,11 +538,16 @@ namespace CppSharp.Generators.CSharp @@ -533,11 +538,16 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.Void:
return true;
case PrimitiveType.Char:
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
if (Context.Driver.Options.MarshalCharAsManagedChar)
Context.Return.Write("(sbyte) ");
goto default;
case PrimitiveType.Char16:
return false;
case PrimitiveType.Bool:
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
Context.Return.Write("(byte) ({0} ? 1 : 0)", Context.Parameter.Name);
return true;
default:
Context.Return.Write(Context.Parameter.Name);
return true;

19
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -2475,13 +2475,20 @@ namespace CppSharp.Generators.CSharp @@ -2475,13 +2475,20 @@ namespace CppSharp.Generators.CSharp
if (param.Param == operatorParam && needsInstance)
continue;
var name = string.Empty;
var name = new StringBuilder();
if (param.Context != null
&& !string.IsNullOrWhiteSpace(param.Context.ArgumentPrefix))
name += param.Context.ArgumentPrefix;
name.Append(param.Context.ArgumentPrefix);
name += param.Name;
names.Add(name);
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
if ((param.Param.Type.GetFinalPointee() ?? param.Param.Type).IsPrimitiveType(PrimitiveType.Bool))
{
var typePrinter = new CSharpTypePrinter(Driver);
typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
name.AppendFormat("({0}) ", param.Param.Type.Visit(typePrinter));
}
name.Append(param.Name);
names.Add(name.ToString());
}
var needsFixedThis = needsInstance && isValueType;
@ -2657,7 +2664,9 @@ namespace CppSharp.Generators.CSharp @@ -2657,7 +2664,9 @@ namespace CppSharp.Generators.CSharp
Function function = null)
{
PrimitiveType primitive;
if (param.Type.IsPrimitiveType(out primitive) && primitive != PrimitiveType.Char)
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
if (param.Type.IsPrimitiveType(out primitive) &&
primitive != PrimitiveType.Char && primitive != PrimitiveType.Bool)
{
return new ParamMarshal { Name = param.Name, Param = param };
}

5
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -479,11 +479,14 @@ namespace CppSharp.Generators.CSharp @@ -479,11 +479,14 @@ namespace CppSharp.Generators.CSharp
{
switch (primitive)
{
case PrimitiveType.Bool: return "bool";
case PrimitiveType.Bool:
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
return ContextKind == CSharpTypePrinterContextKind.Native ? "byte" : "bool";
case PrimitiveType.Void: return "void";
case PrimitiveType.Char16:
case PrimitiveType.WideChar: return "char";
case PrimitiveType.Char:
// TODO: work around https://github.com/dotnet/coreclr/issues/1485
return driver.Options.MarshalCharAsManagedChar &&
ContextKind != CSharpTypePrinterContextKind.Native
? "char"

9
tests/Common/Common.Tests.cs

@ -538,8 +538,15 @@ public class CommonTests : GeneratorTestFixture @@ -538,8 +538,15 @@ public class CommonTests : GeneratorTestFixture
[Test]
public void TestVirtualReturningClassWithCharField()
{
using (new HasVirtualReturningHasCharField())
using (var hasVirtualReturningHasProblematicFields = new HasVirtualReturningHasProblematicFields())
{
var hasProblematicFields = hasVirtualReturningHasProblematicFields.returnsProblematicFields();
Assert.That(hasProblematicFields.b, Is.EqualTo(false));
hasProblematicFields.b = true;
Assert.That(hasProblematicFields.b, Is.EqualTo(true));
Assert.That(hasProblematicFields.c, Is.EqualTo(char.MinValue));
hasProblematicFields.c = 'a';
Assert.That(hasProblematicFields.c, Is.EqualTo('a'));
}
}
}

10
tests/Common/Common.cpp

@ -501,11 +501,15 @@ void funcTryValTypeOut(CS_OUT ValueTypeClassPassTry classTry) @@ -501,11 +501,15 @@ void funcTryValTypeOut(CS_OUT ValueTypeClassPassTry classTry)
{
}
HasVirtualReturningHasCharField::HasVirtualReturningHasCharField()
HasProblematicFields::HasProblematicFields() : b(false), c(0)
{
}
HasCharField HasVirtualReturningHasCharField::returnsCharField()
HasVirtualReturningHasProblematicFields::HasVirtualReturningHasProblematicFields()
{
return HasCharField();
}
HasProblematicFields HasVirtualReturningHasProblematicFields::returnsProblematicFields()
{
return HasProblematicFields();
}

11
tests/Common/Common.h

@ -807,14 +807,17 @@ class DLL_API CS_VALUE_TYPE ValueTypeClassPassTry { }; @@ -807,14 +807,17 @@ class DLL_API CS_VALUE_TYPE ValueTypeClassPassTry { };
void DLL_API funcTryValTypePtrOut(CS_OUT ValueTypeClassPassTry* classTry);
void DLL_API funcTryValTypeOut(CS_OUT ValueTypeClassPassTry classTry);
class DLL_API HasCharField
class DLL_API HasProblematicFields
{
public:
HasProblematicFields();
bool b;
char c;
};
class DLL_API HasVirtualReturningHasCharField
class DLL_API HasVirtualReturningHasProblematicFields
{
public:
HasVirtualReturningHasCharField();
virtual HasCharField returnsCharField();
HasVirtualReturningHasProblematicFields();
virtual HasProblematicFields returnsProblematicFields();
};

Loading…
Cancel
Save