Skip to content
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

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

Merged
merged 8 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 (global::System.Exception __e) {");
writer.Indent ();
writer.WriteLine ("__envp.SetPendingException (__e);");
Copy link
Member

@jonpryor jonpryor Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpobst: as per this comment, the generated body should instead be:

global::Java.Interop.JniEnvironment.Runtime.OnUnhandledException (ref __envp, __e);

The name OnUnhandledException() can (should) be debated, in part because the exception is not, in fact, "unhandled". Perhaps MarshalExceptionToJni()? (Suggestions welcome.)

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});");
}
}
}
Loading