Skip to content

Commit 98beeaa

Browse files
committed
[generator] Eliminate usage of JNINativeWrapper.CreateDelegate in bindings.
1 parent 2bdf2bc commit 98beeaa

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Xamarin.SourceWriter;
7+
8+
namespace generator.SourceWriters
9+
{
10+
public class DebuggerDisableUserUnhandledExceptionsAttributeAttr : AttributeWriter
11+
{
12+
public override void WriteAttribute (CodeWriter writer)
13+
{
14+
writer.WriteLine ("[global::System.Diagnostics.DebuggerDisableUserUnhandledExceptionsAttribute]");
15+
}
16+
}
17+
}

tools/generator/SourceWriters/MethodCallback.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public MethodCallback (GenBase type, Method method, CodeGenerationOptions option
4747

4848
SourceWriterExtensions.AddSupportedOSPlatform (Attributes, method, opt);
4949

50+
Attributes.Add (new DebuggerDisableUserUnhandledExceptionsAttributeAttr ());
51+
5052
Parameters.Add (new MethodParameterWriter ("jnienv", TypeReferenceWriter.IntPtr));
5153
Parameters.Add (new MethodParameterWriter ("native__this", TypeReferenceWriter.IntPtr));
5254

@@ -56,6 +58,11 @@ public MethodCallback (GenBase type, Method method, CodeGenerationOptions option
5658

5759
protected override void WriteBody (CodeWriter writer)
5860
{
61+
writer.WriteLine ("var __envp = new global::Java.Interop.JniTransition (jnienv);");
62+
writer.WriteLine ();
63+
writer.WriteLine ("try {");
64+
65+
writer.Indent ();
5966
writer.WriteLine ($"var __this = global::Java.Lang.Object.GetObject<{opt.GetOutputName (type.FullName)}> (jnienv, native__this, JniHandleOwnership.DoNotTransfer){opt.NullForgivingOperator};");
6067

6168
foreach (var s in method.Parameters.GetCallbackPrep (opt))
@@ -79,6 +86,25 @@ protected override void WriteBody (CodeWriter writer)
7986

8087
if (!method.IsVoid && method.Parameters.HasCleanup)
8188
writer.WriteLine ("return __ret;");
89+
90+
writer.Unindent ();
91+
92+
writer.WriteLine ("} catch (global::System.Exception __e) {");
93+
writer.Indent ();
94+
writer.WriteLine ("__envp.SetPendingException (__e);");
95+
writer.WriteLine ("global::System.Diagnostics.Debugger.BreakForUserUnhandledException (__e);");
96+
97+
if (!method.IsVoid)
98+
writer.WriteLine ("return default;");
99+
100+
writer.Unindent ();
101+
writer.WriteLine ("} finally {");
102+
writer.Indent ();
103+
writer.WriteLine ("__envp.Dispose ();");
104+
writer.Unindent ();
105+
writer.WriteLine ("}");
106+
107+
82108
}
83109

84110
public override void Write (CodeWriter writer)
@@ -143,10 +169,7 @@ public GetDelegateHandlerMethod (Method method, CodeGenerationOptions opt)
143169
protected override void WriteBody (CodeWriter writer)
144170
{
145171
var callback_name = method.EscapedCallbackName;
146-
147-
writer.WriteLine ($"if ({callback_name} == null)");
148-
writer.WriteLine ($"\t{callback_name} = JNINativeWrapper.CreateDelegate (new {method.GetDelegateType (opt)} (n_{method.Name + method.IDSignature}));");
149-
writer.WriteLine ($"return {callback_name};");
172+
writer.WriteLine ($"return {callback_name} ??= new {method.GetDelegateType (opt)} (n_{method.Name + method.IDSignature});");
150173
}
151174
}
152175
}

0 commit comments

Comments
 (0)