From 98beeaa1b11365fa108076b2c1bb3e301b44b6bf Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Tue, 5 Nov 2024 12:53:26 -1000 Subject: [PATCH] [generator] Eliminate usage of `JNINativeWrapper.CreateDelegate` in bindings. --- ...bleUserUnhandledExceptionsAttributeAttr.cs | 17 ++++++++++ .../generator/SourceWriters/MethodCallback.cs | 31 ++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tools/generator/SourceWriters/Attributes/DebuggerDisableUserUnhandledExceptionsAttributeAttr.cs diff --git a/tools/generator/SourceWriters/Attributes/DebuggerDisableUserUnhandledExceptionsAttributeAttr.cs b/tools/generator/SourceWriters/Attributes/DebuggerDisableUserUnhandledExceptionsAttributeAttr.cs new file mode 100644 index 000000000..c11ea1a50 --- /dev/null +++ b/tools/generator/SourceWriters/Attributes/DebuggerDisableUserUnhandledExceptionsAttributeAttr.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xamarin.SourceWriter; + +namespace generator.SourceWriters +{ + public class DebuggerDisableUserUnhandledExceptionsAttributeAttr : AttributeWriter + { + public override void WriteAttribute (CodeWriter writer) + { + writer.WriteLine ("[global::System.Diagnostics.DebuggerDisableUserUnhandledExceptionsAttribute]"); + } + } +} diff --git a/tools/generator/SourceWriters/MethodCallback.cs b/tools/generator/SourceWriters/MethodCallback.cs index 161a5e53c..8f84bb798 100644 --- a/tools/generator/SourceWriters/MethodCallback.cs +++ b/tools/generator/SourceWriters/MethodCallback.cs @@ -47,6 +47,8 @@ public MethodCallback (GenBase type, Method method, CodeGenerationOptions option SourceWriterExtensions.AddSupportedOSPlatform (Attributes, method, opt); + Attributes.Add (new DebuggerDisableUserUnhandledExceptionsAttributeAttr ()); + Parameters.Add (new MethodParameterWriter ("jnienv", TypeReferenceWriter.IntPtr)); Parameters.Add (new MethodParameterWriter ("native__this", TypeReferenceWriter.IntPtr)); @@ -56,6 +58,11 @@ public MethodCallback (GenBase type, Method method, CodeGenerationOptions option protected override void WriteBody (CodeWriter writer) { + writer.WriteLine ("var __envp = new global::Java.Interop.JniTransition (jnienv);"); + writer.WriteLine (); + writer.WriteLine ("try {"); + + writer.Indent (); writer.WriteLine ($"var __this = global::Java.Lang.Object.GetObject<{opt.GetOutputName (type.FullName)}> (jnienv, native__this, JniHandleOwnership.DoNotTransfer){opt.NullForgivingOperator};"); foreach (var s in method.Parameters.GetCallbackPrep (opt)) @@ -79,6 +86,25 @@ protected override void WriteBody (CodeWriter writer) if (!method.IsVoid && method.Parameters.HasCleanup) writer.WriteLine ("return __ret;"); + + writer.Unindent (); + + writer.WriteLine ("} catch (global::System.Exception __e) {"); + writer.Indent (); + writer.WriteLine ("__envp.SetPendingException (__e);"); + writer.WriteLine ("global::System.Diagnostics.Debugger.BreakForUserUnhandledException (__e);"); + + if (!method.IsVoid) + writer.WriteLine ("return default;"); + + writer.Unindent (); + writer.WriteLine ("} finally {"); + writer.Indent (); + writer.WriteLine ("__envp.Dispose ();"); + writer.Unindent (); + writer.WriteLine ("}"); + + } public override void Write (CodeWriter writer) @@ -143,10 +169,7 @@ public GetDelegateHandlerMethod (Method method, CodeGenerationOptions opt) protected override void WriteBody (CodeWriter writer) { var callback_name = method.EscapedCallbackName; - - writer.WriteLine ($"if ({callback_name} == null)"); - writer.WriteLine ($"\t{callback_name} = JNINativeWrapper.CreateDelegate (new {method.GetDelegateType (opt)} (n_{method.Name + method.IDSignature}));"); - writer.WriteLine ($"return {callback_name};"); + writer.WriteLine ($"return {callback_name} ??= new {method.GetDelegateType (opt)} (n_{method.Name + method.IDSignature});"); } } }