From 58863bba59b8314bf5f4a669c4133ac2135a2b2e Mon Sep 17 00:00:00 2001 From: Gareth Francis Date: Fri, 21 Jun 2019 16:55:30 +0100 Subject: [PATCH 1/4] Change C# bool[] typemaps to marshall as 1-byte Default marshalling for bool[] now uses 1-byte entries in the array, to ensure array contents is as expected in C++. When running under mono csharp_lib_arrays_bool testcase will fail due to an apparent bug in mono. Works correctly under Microsoft's runtime. See https://github.com/mono/mono/issues/15592 --- Examples/test-suite/csharp/Makefile.in | 5 ++ .../csharp/csharp_lib_arrays_bool_runme.cs | 78 +++++++++++++++++++ Examples/test-suite/csharp_lib_arrays_bool.i | 78 +++++++++++++++++++ Lib/csharp/arrays_csharp.i | 42 +++++++++- 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs create mode 100644 Examples/test-suite/csharp_lib_arrays_bool.i diff --git a/Examples/test-suite/csharp/Makefile.in b/Examples/test-suite/csharp/Makefile.in index c9e48f804..49608e809 100644 --- a/Examples/test-suite/csharp/Makefile.in +++ b/Examples/test-suite/csharp/Makefile.in @@ -19,6 +19,7 @@ CPP_TEST_CASES = \ csharp_exceptions \ csharp_features \ csharp_lib_arrays \ + csharp_lib_arrays_bool \ csharp_namespace_system_collision \ csharp_prepost \ csharp_typemaps \ @@ -36,6 +37,9 @@ CPP11_TEST_CASES = \ cpp11_shared_ptr_upcast \ cpp11_strongly_typed_enumerations_simple \ +# bool[] typemaps don't work correctly when running under mono +FAILING_CPP_TESTS = csharp_lib_arrays_bool + include $(srcdir)/../common.mk # Overridden variables here @@ -48,6 +52,7 @@ CSHARPFLAGSSPECIAL = intermediary_classname.cpptest: SWIGOPT += -dllimport intermediary_classname complextest.cpptest: CSHARPFLAGSSPECIAL = -r:System.Numerics.dll csharp_lib_arrays.cpptest: CSHARPFLAGSSPECIAL = -unsafe +csharp_lib_arrays_bool.cpptest: CSHARPFLAGSSPECIAL = -unsafe csharp_swig2_compatibility.cpptest: SWIGOPT += -DSWIG2_CSHARP # Rules for the different types of tests diff --git a/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs b/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs new file mode 100644 index 000000000..25b7fe699 --- /dev/null +++ b/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs @@ -0,0 +1,78 @@ +using System; +using csharp_lib_arrays_boolNamespace; + +public class runme +{ + static void Main() + { + { + bool[] source = { true, false, false, true, false, true, true, false }; + bool[] target = new bool[ source.Length ]; + + csharp_lib_arrays_bool.myArrayCopyUsingFixedArraysBool( source, target, target.Length ); + CompareArrays(source, target, "bool[] INPUT/OUTPUT Fixed"); + } + + { + bool[] source = { true, false, false, true, false, true, true, false }; + bool[] target = { false, true, true, false, true, false, false, true }; + + csharp_lib_arrays_bool.myArraySwapUsingFixedArraysBool( source, target, target.Length ); + + for (int i=0; i( T[] a, T[] b, string testName ) + { + if (a.Length != b.Length) + throw new Exception("size mismatch"); + + for(int i=0; i( T[] a ) + { + foreach ( T i in a ) + Console.Error.Write( "{0} ", i ); + Console.Error.WriteLine(); + } +} + diff --git a/Examples/test-suite/csharp_lib_arrays_bool.i b/Examples/test-suite/csharp_lib_arrays_bool.i new file mode 100644 index 000000000..58cee9d80 --- /dev/null +++ b/Examples/test-suite/csharp_lib_arrays_bool.i @@ -0,0 +1,78 @@ +%module csharp_lib_arrays_bool + +%include "arrays_csharp.i" + +%apply bool INPUT[] { bool* sourceArray } +%apply bool OUTPUT[] { bool* targetArray } + +%apply bool INOUT[] { bool* array1 } +%apply bool INOUT[] { bool* array2 } + +%inline %{ +#include + +/* copy the contents of the first array to the second */ +void myArrayCopyBool( bool* sourceArray, bool* targetArray, int nitems ) { + int i; + for ( i = 0; i < nitems; i++ ) { + targetArray[ i ] = sourceArray[ i ]; + } +} + +/* swap the contents of the two arrays */ +void myArraySwapBool( bool* array1, bool* array2, int nitems ) { + int i; + bool temp; + for ( i = 0; i < nitems; i++ ) { + temp = array1[ i ]; + array1[ i ] = array2[ i ]; + array2[ i ] = temp; + } +} + +bool checkBoolArrayCorrect( bool* sourceArray, int sourceArraySize ) { + if( sourceArraySize != 8 ) { + std::cout << "checkBoolArrayCorrect: Expected array with 8 elements" << std::endl; + return false; + } + return sourceArray[0] == true && + sourceArray[1] == false && + sourceArray[2] == false && + sourceArray[3] == true && + sourceArray[4] == false && + sourceArray[5] == true && + sourceArray[6] == true && + sourceArray[7] == false; +} +%} + +%clear bool* sourceArray; +%clear bool* targetArray; + +%clear bool* array1; +%clear bool* array2; + +// Below replicates the above array handling but this time using the pinned (fixed) array typemaps +%csmethodmodifiers myArrayCopyUsingFixedArraysBool "public unsafe"; +%csmethodmodifiers myArraySwapUsingFixedArraysBool "public unsafe"; + +%apply bool FIXED[] { bool* sourceArray } +%apply bool FIXED[] { bool* targetArray } + +%inline %{ +void myArrayCopyUsingFixedArraysBool( bool *sourceArray, bool* targetArray, int nitems ) { + myArrayCopyBool(sourceArray, targetArray, nitems); +} +%} + +%apply bool FIXED[] { bool* array1 } +%apply bool FIXED[] { bool* array2 } + +%inline %{ +void myArraySwapUsingFixedArraysBool( bool* array1, bool* array2, int nitems ) { + myArraySwapBool(array1, array2, nitems); +} +%} + + + diff --git a/Lib/csharp/arrays_csharp.i b/Lib/csharp/arrays_csharp.i index 237067a88..861da8386 100644 --- a/Lib/csharp/arrays_csharp.i +++ b/Lib/csharp/arrays_csharp.i @@ -103,7 +103,47 @@ CSHARP_ARRAYS(long long, long) CSHARP_ARRAYS(unsigned long long, ulong) CSHARP_ARRAYS(float, float) CSHARP_ARRAYS(double, double) -CSHARP_ARRAYS(bool, bool) + +// By default C# will marshal bools as 4 bytes +// UnmanagedType.I1 will change this to 1 byte +// FIXME - When running on mono ArraySubType appears to be ignored and bools will be marshalled as 4-byte +// https://github.com/mono/mono/issues/15592 + +// input only arrays +%typemap(ctype) bool INPUT[] "bool*" +%typemap(cstype) bool INPUT[] "bool[]" +%typemap(imtype, inattributes="[global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]") bool INPUT[] "bool[]" +%typemap(csin) bool INPUT[] "$csinput" + +%typemap(in) bool INPUT[] %{ +$1 = $input; +%} +%typemap(freearg) bool INPUT[] "" +%typemap(argout) bool INPUT[] "" + +// output only arrays +%typemap(ctype) bool OUTPUT[] "bool*" +%typemap(cstype) bool OUTPUT[] "bool[]" +%typemap(imtype, inattributes="[global::System.Runtime.InteropServices.Out, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]") bool OUTPUT[] "bool[]" +%typemap(csin) bool OUTPUT[] "$csinput" + +%typemap(in) bool OUTPUT[] %{ +$1 = $input; +%} +%typemap(freearg) bool OUTPUT[] "" +%typemap(argout) bool OUTPUT[] "" + +// inout arrays +%typemap(ctype) bool INOUT[] "bool*" +%typemap(cstype) bool INOUT[] "bool[]" +%typemap(imtype, inattributes="[global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.Out, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]") bool INOUT[] "bool[]" +%typemap(csin) bool INOUT[] "$csinput" + +%typemap(in) bool INOUT[] %{ +$1 = $input; +%} +%typemap(freearg) bool INOUT[] "" +%typemap(argout) bool INOUT[] "" %define CSHARP_ARRAYS_FIXED( CTYPE, CSTYPE ) From 15ad67c37f8ddbb33e69a14ebaf97251dcfd4ee7 Mon Sep 17 00:00:00 2001 From: Gareth Francis Date: Mon, 15 Jul 2019 16:08:31 +0100 Subject: [PATCH 2/4] Skip failing bool[] test cases when running under mono --- Examples/test-suite/csharp/Makefile.in | 3 --- .../test-suite/csharp/csharp_lib_arrays_bool_runme.cs | 11 +++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Examples/test-suite/csharp/Makefile.in b/Examples/test-suite/csharp/Makefile.in index 49608e809..8272864d5 100644 --- a/Examples/test-suite/csharp/Makefile.in +++ b/Examples/test-suite/csharp/Makefile.in @@ -37,9 +37,6 @@ CPP11_TEST_CASES = \ cpp11_shared_ptr_upcast \ cpp11_strongly_typed_enumerations_simple \ -# bool[] typemaps don't work correctly when running under mono -FAILING_CPP_TESTS = csharp_lib_arrays_bool - include $(srcdir)/../common.mk # Overridden variables here diff --git a/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs b/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs index 25b7fe699..24ae8a73e 100644 --- a/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs +++ b/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs @@ -25,6 +25,12 @@ public class runme CompareArrays(source, target, "bool[] INOUT"); } + if( runtimeIsMono() ) + { + Console.Error.WriteLine("Tests are running on mono, failing bool[] tests skipped"); + return; + } + { bool[] source = { true, false, false, true, false, true, true, false }; bool[] target = new bool[ source.Length ]; @@ -74,5 +80,10 @@ public class runme Console.Error.Write( "{0} ", i ); Console.Error.WriteLine(); } + + static bool runtimeIsMono() + { + return Type.GetType ("Mono.Runtime") != null; + } } From fc9c37192b1c5793e6fb37378284db071bd77d9f Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 16 Jul 2019 19:46:32 +0100 Subject: [PATCH 3/4] Quieten failing bool[] testcase message --- Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs b/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs index 24ae8a73e..3b19b576f 100644 --- a/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs +++ b/Examples/test-suite/csharp/csharp_lib_arrays_bool_runme.cs @@ -27,7 +27,8 @@ public class runme if( runtimeIsMono() ) { - Console.Error.WriteLine("Tests are running on mono, failing bool[] tests skipped"); +// Console.Error.WriteLine("Tests are running on mono, failing bool[] tests skipped"); +// See Mono bug report https://github.com/mono/mono/issues/15592 return; } From a7fa720c1f48662dd8fad3f9c068e1e9eed04393 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 16 Jul 2019 19:50:35 +0100 Subject: [PATCH 4/4] Add changes entry to fix C# bool[] --- CHANGES.current | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.current b/CHANGES.current index 99b31d5c9..27351c834 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.1 (in progress) =========================== +2019-07-16: geefr + [C#] #616 #1576 Fix C# bool INPUT[], bool OUTPUT[], bool INOUT[] typemaps to marshall + as 1-byte. + 2019-06-06: bkotzz [Java] #1552 Improve performance in Java std::vector constructor wrapper that takes a native Java array as input.