Skip to content

Commit 25b7c27

Browse files
[Mono.Android] add "built-in" delegate for _JniMarshal_PPII_L (#8245)
I noticed some methods appear when creating a new `.aotprofile` for .NET MAUI: void System.Reflection.Emit.RuntimeILGenerator:.ctor (System.Reflection.Module,System.Reflection.Emit.ITokenGenerator,int) void System.Reflection.Emit.RuntimeILGenerator:BeginCatchBlock (System.Type) void System.Reflection.Emit.RuntimeILGenerator:Emit (System.Reflection.Emit.OpCode,int) void System.Reflection.Emit.RuntimeILGenerator:Emit (System.Reflection.Emit.OpCode,System.Reflection.Emit.Label) void System.Reflection.Emit.RuntimeILGenerator:Emit (System.Reflection.Emit.OpCode,System.Reflection.Emit.LocalBuilder) void System.Reflection.Emit.RuntimeILGenerator:Emit (System.Reflection.Emit.OpCode,System.Reflection.MethodInfo) void System.Reflection.Emit.RuntimeILGenerator:Emit (System.Reflection.Emit.OpCode) We generally don't want S.R.E to run at application startup. This can happen when `Android.Runtime.JNINativeWrapper:CreateBuiltInDelegate` doesn't find a match. We fallback to S.R.E in that case. Running a `dotnet new maui` app w/ .NET 8 Preview 7 logs the message: 08-07 10:01:51.674 16590 16590 D monodroid-assembly: Falling back to System.Reflection.Emit for delegate type '_JniMarshal_PPII_L': IntPtr n_GetClipPath_II(IntPtr, IntPtr, Int32, Int32) This may also coincide with `AndroidEnableMarshalMethods` being turned off by default. We would not have seen this log message when it was on. After this change, we skip the S.R.E code path: 08-07 10:37:23.089 17661 17661 D Mono : AOT NOT FOUND: (wrapper native-to-managed) Android.Runtime.JNINativeWrapper:Wrap_JniMarshal_PPII_L (intptr,intptr,int,int). We should update the `.aotprofile` in xamarin-android and dotnet/maui after this change. To update `JNINativeWrapper.g.tt`: dotnet tool install -g dotnet-t4 t4 src\Mono.Android\Android.Runtime\JNINativeWrapper.g.tt -o src\Mono.Android\Android.Runtime\JNINativeWrapper.g.cs Other changes: * Fixed code formatting, so we don't have empty tabs in output.
1 parent 569b7c0 commit 25b7c27

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public static partial class JNINativeWrapper
99
static bool _unhandled_exception (Exception e)
1010
{
1111
if (Debugger.IsAttached || !JNIEnvInit.PropagateExceptions) {
12+
#if NETCOREAPP
1213
AndroidRuntimeInternal.mono_unhandled_exception?.Invoke (e);
14+
#else
15+
JNIEnvInit.mono_unhandled_exception?.Invoke (e);
16+
#endif
1317
return false;
1418
}
1519
return true;
@@ -158,6 +162,17 @@ internal static void Wrap_JniMarshal_PPII_V (this _JniMarshal_PPII_V callback, I
158162
}
159163
}
160164

165+
internal static IntPtr Wrap_JniMarshal_PPII_L (this _JniMarshal_PPII_L callback, IntPtr jnienv, IntPtr klazz, int p0, int p1)
166+
{
167+
AndroidRuntimeInternal.WaitForBridgeProcessing ();
168+
try {
169+
return callback (jnienv, klazz, p0, p1);
170+
} catch (Exception e) when (_unhandled_exception (e)) {
171+
AndroidEnvironment.UnhandledException (e);
172+
return default;
173+
}
174+
}
175+
161176
internal static void Wrap_JniMarshal_PPLI_V (this _JniMarshal_PPLI_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, int p1)
162177
{
163178
AndroidRuntimeInternal.WaitForBridgeProcessing ();
@@ -473,6 +488,8 @@ private static Delegate CreateBuiltInDelegate (Delegate dlg, Type delegateType)
473488
return new _JniMarshal_PPJ_Z (Unsafe.As<_JniMarshal_PPJ_Z> (dlg).Wrap_JniMarshal_PPJ_Z);
474489
case nameof (_JniMarshal_PPII_V):
475490
return new _JniMarshal_PPII_V (Unsafe.As<_JniMarshal_PPII_V> (dlg).Wrap_JniMarshal_PPII_V);
491+
case nameof (_JniMarshal_PPII_L):
492+
return new _JniMarshal_PPII_L (Unsafe.As<_JniMarshal_PPII_L> (dlg).Wrap_JniMarshal_PPII_L);
476493
case nameof (_JniMarshal_PPLI_V):
477494
return new _JniMarshal_PPLI_V (Unsafe.As<_JniMarshal_PPLI_V> (dlg).Wrap_JniMarshal_PPLI_V);
478495
case nameof (_JniMarshal_PPLZ_V):

src/Mono.Android/Android.Runtime/JNINativeWrapper.g.tt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ var delegateTypes = new [] {
8282
Return = "void",
8383
Invoke = "jnienv, klazz, p0, p1",
8484
},
85+
new {
86+
Type = "_JniMarshal_PPII_L",
87+
Signature = "IntPtr jnienv, IntPtr klazz, int p0, int p1",
88+
Return = "IntPtr",
89+
Invoke = "jnienv, klazz, p0, p1",
90+
},
8591
new {
8692
Type = "_JniMarshal_PPLI_V",
8793
Signature = "IntPtr jnienv, IntPtr klazz, IntPtr p0, int p1",
@@ -271,7 +277,7 @@ foreach (var info in delegateTypes) {
271277
<#= info.Return != "void" ? "return " : "" #>callback (<#= info.Invoke #>);
272278
} catch (Exception e) when (_unhandled_exception (e)) {
273279
AndroidEnvironment.UnhandledException (e);
274-
<#= info.Return != "void" ? "return default;" : "" #>
280+
<#= info.Return != "void" ? "\t\t\t\treturn default;" : "" #>
275281
}
276282
}
277283

0 commit comments

Comments
 (0)