Skip to content

[generator] Eliminate usage of JNINativeWrapper.CreateDelegate in bindings. #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Java.Interop/Java.Interop/JniEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -50,6 +51,50 @@ public static bool WithinNewObjectScope {
internal set {CurrentInfo.WithinNewObjectScope = value;}
}

[global::System.Diagnostics.CodeAnalysis.SuppressMessage (
"Design",
"CA1031:Do not catch general exception types",
Justification = "Exceptions cannot cross a JNI boundary.")]
public static bool BeginMarshalMethod (IntPtr jnienv, out JniTransition transition, [NotNullWhen (true)] out JniRuntime? runtime)
{
runtime = null;
Exception? ex = null;
try {
runtime = Info.Value?.Runtime;
}
catch (Exception e) {
ex = e;
}
if (runtime == null || ex != null) {
transition = default;
runtime = null;
Console.Error.WriteLine ("JNI Environment Information is not available on this thread.");
if (ex != null) {
Console.Error.WriteLine (ex);
}
return false;
}

try {
runtime.OnEnterMarshalMethod ();
transition = new JniTransition (jnienv);
}
catch (Exception e) {
runtime = null;
transition = default;

Console.Error.WriteLine ($"OnEnterMarshalMethod failed: {e}");
return false;
}

return true;
}

public static void EndMarshalMethod (ref JniTransition transition)
{
transition.Dispose ();
}

internal static void SetEnvironmentPointer (IntPtr environmentPointer)
{
CurrentInfo.EnvironmentPointer = environmentPointer;
Expand Down
13 changes: 13 additions & 0 deletions src/Java.Interop/Java.Interop/JniRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,19 @@ public virtual bool ExceptionShouldTransitionToJni (Exception e)

partial class JniRuntime {

public virtual void OnEnterMarshalMethod ()
{
ValueManager.WaitForGCBridgeProcessing ();
}

public virtual void OnUserUnhandledException (ref JniTransition transition, Exception e)
{
transition.SetPendingException (e);

// TODO: Enable when we move to 'net9.0'
//Debugger.BreakForUserUnhandledException (e);
}

public virtual void RaisePendingException (Exception pendingException)
{
JniEnvironment.Exceptions.Throw (pendingException);
Expand Down
4 changes: 4 additions & 0 deletions src/Java.Interop/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#nullable enable
static Java.Interop.JniEnvironment.BeginMarshalMethod(nint jnienv, out Java.Interop.JniTransition transition, out Java.Interop.JniRuntime? runtime) -> bool
static Java.Interop.JniEnvironment.EndMarshalMethod(ref Java.Interop.JniTransition transition) -> void
virtual Java.Interop.JniRuntime.OnEnterMarshalMethod() -> void
virtual Java.Interop.JniRuntime.OnUserUnhandledException(ref Java.Interop.JniTransition transition, System.Exception! e) -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if !NET9_0_OR_GREATER

using System;

namespace System.Diagnostics
{
// This attribute was added in .NET 9, and we may not be targeting .NET 9 yet.
public class DebuggerDisableUserUnhandledExceptionsAttribute : Attribute
{
}
}

#endif // !NET9_0_OR_GREATER
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ internal partial class AnimatorListenerInvoker : global::Java.Lang.Object, Anima
#pragma warning disable 0169
static Delegate GetOnAnimationEnd_IHandler ()
{
if (cb_OnAnimationEnd_OnAnimationEnd_I_Z == null)
cb_OnAnimationEnd_OnAnimationEnd_I_Z = JNINativeWrapper.CreateDelegate (new _JniMarshal_PPI_Z (n_OnAnimationEnd_I));
return cb_OnAnimationEnd_OnAnimationEnd_I_Z;
return cb_OnAnimationEnd_OnAnimationEnd_I_Z ??= new _JniMarshal_PPI_Z (n_OnAnimationEnd_I);
}

[global::System.Diagnostics.DebuggerDisableUserUnhandledExceptions]
static bool n_OnAnimationEnd_I (IntPtr jnienv, IntPtr native__this, int param1)
{
var __this = global::Java.Lang.Object.GetObject<java.code.AnimatorListener> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return __this.OnAnimationEnd (param1);
if (!global::Java.Interop.JniEnvironment.BeginMarshalMethod (jnienv, out var __envp, out var __r))
return default;

try {
var __this = global::Java.Lang.Object.GetObject<java.code.AnimatorListener> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return __this.OnAnimationEnd (param1);
} catch (global::System.Exception __e) {
__r.OnUserUnhandledException (ref __envp, __e);
return default;
} finally {
global::Java.Interop.JniEnvironment.EndMarshalMethod (ref __envp);
}
}
#pragma warning restore 0169

Expand All @@ -96,15 +105,24 @@ internal partial class AnimatorListenerInvoker : global::Java.Lang.Object, Anima
#pragma warning disable 0169
static Delegate GetOnAnimationEnd_IIHandler ()
{
if (cb_OnAnimationEnd_OnAnimationEnd_II_Z == null)
cb_OnAnimationEnd_OnAnimationEnd_II_Z = JNINativeWrapper.CreateDelegate (new _JniMarshal_PPII_Z (n_OnAnimationEnd_II));
return cb_OnAnimationEnd_OnAnimationEnd_II_Z;
return cb_OnAnimationEnd_OnAnimationEnd_II_Z ??= new _JniMarshal_PPII_Z (n_OnAnimationEnd_II);
}

[global::System.Diagnostics.DebuggerDisableUserUnhandledExceptions]
static bool n_OnAnimationEnd_II (IntPtr jnienv, IntPtr native__this, int param1, int param2)
{
var __this = global::Java.Lang.Object.GetObject<java.code.AnimatorListener> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return __this.OnAnimationEnd (param1, param2);
if (!global::Java.Interop.JniEnvironment.BeginMarshalMethod (jnienv, out var __envp, out var __r))
return default;

try {
var __this = global::Java.Lang.Object.GetObject<java.code.AnimatorListener> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
return __this.OnAnimationEnd (param1, param2);
} catch (global::System.Exception __e) {
__r.OnUserUnhandledException (ref __envp, __e);
return default;
} finally {
global::Java.Interop.JniEnvironment.EndMarshalMethod (ref __envp);
}
}
#pragma warning restore 0169

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,23 @@ internal partial class IMyInterface2Invoker : global::Java.Lang.Object, IMyInter
#pragma warning disable 0169
static Delegate GetDoSomethingHandler ()
{
if (cb_DoSomething_DoSomething_V == null)
cb_DoSomething_DoSomething_V = JNINativeWrapper.CreateDelegate (new _JniMarshal_PP_V (n_DoSomething));
return cb_DoSomething_DoSomething_V;
return cb_DoSomething_DoSomething_V ??= new _JniMarshal_PP_V (n_DoSomething);
}

[global::System.Diagnostics.DebuggerDisableUserUnhandledExceptions]
static void n_DoSomething (IntPtr jnienv, IntPtr native__this)
{
var __this = global::Java.Lang.Object.GetObject<java.code.IMyInterface2> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
__this.DoSomething ();
if (!global::Java.Interop.JniEnvironment.BeginMarshalMethod (jnienv, out var __envp, out var __r))
return;

try {
var __this = global::Java.Lang.Object.GetObject<java.code.IMyInterface2> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
__this.DoSomething ();
} catch (global::System.Exception __e) {
__r.OnUserUnhandledException (ref __envp, __e);
} finally {
global::Java.Interop.JniEnvironment.EndMarshalMethod (ref __envp);
}
}
#pragma warning restore 0169

Expand Down
Loading