Skip to content

Commit

Permalink
[generator] Eliminate usage of JNINativeWrapper.CreateDelegate in b…
Browse files Browse the repository at this point in the history
…indings.
  • Loading branch information
jpobst committed Nov 5, 2024
1 parent 2bdf2bc commit 5746fff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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]");
}
}
}
31 changes: 27 additions & 4 deletions tools/generator/SourceWriters/MethodCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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))
Expand All @@ -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 (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)
Expand Down Expand Up @@ -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});");
}
}
}

0 comments on commit 5746fff

Please sign in to comment.