Avoid non-blittable types in native callback methods #1027
Labels
enhancement
Proposed change to current functionality
generator
Issues binding a Java library (generator, class-parse, etc.)
Milestone
While working on implementation of marshal methods (a way to replace the current native JNI method registration with
generated code) which take advantage of the
[UnmanagedCallersOnly]
attribute, I came across a problem that some ofour registered methods either return a
bool
or take a parameter which is abool
. The problem here is thatbool
is a non-blittable type, while all
[UnmanagedCallersOnly]
methods must use only blittable types (as they arecalled directly by the native code, bypassing marshaling).
Marshal methods convert the code output by the generator from:
to
And at run time, a pointer to
n_SetChildrenDrawingOrderEnabled_Z
is obtained using the Mono embedding APImono_method_get_unmanaged_callers_only_ftnptr
.However, when a non-blittable type is encountered, the function will return an error:
The problem is quite common in MAUI apps, since in a simple Hello World app, out of 183 marshal method candidates, 133 have a
bool
in their parameter list or as a return value.I have implemented a workaround for this issue (which we will keep in the future, to deal with 3rd party libraries that weren't regenerated using the new generator) but the real fix is to make the generator instead output code equivalent to:
or, for a method which returns a
bool
:The reason to choose
byte
is that JNI'sjboolean
type is defined as an unsigned 8-bit value and the reason to explicitly return1
or0
instead ofjust casting the managed
bool
value is that the boolean type in dotnet always has value of0
forfalse
, but can have-1
,1
or!= 0
fortrue
,depending on the VM or context and so it's safer to "downcast" that set to the
0
/1
values common in other languages (including Java, C and C++ aboutwhich we care)
In
Mono.Android
generated MCW code we currently have4824
native callback methods either returningbool
or with at least onebool
parameter.The generator should take into account other
System
namespace non-blittable types, not justbool
(within reason - only those that can potentially happen in bindings). The list can be found in this table,for reference copied below:
The text was updated successfully, but these errors were encountered: