Skip to content

Commit 140c928

Browse files
[Rgen] Add method to get the pointer for the exception handler. (#22135)
1 parent 97d6913 commit 140c928

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/rgen/Microsoft.Macios.Generator/Emitters/BindingSyntaxFactory.ObjCRuntime.cs

+23
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,29 @@ internal static LocalDeclarationStatementSyntax GetAutoreleasePoolVariable ()
650650
IdentifierName ("EnsureUIThread").WithTrailingTrivia (Space))));
651651
}
652652

653+
/// <summary>
654+
/// Generate the declaration needed for the exception marhsaling.
655+
/// </summary>
656+
/// <returns>The local declaration needed for the exception marshaling.</returns>
657+
internal static LocalDeclarationStatementSyntax GetExceptionHandleAuxVariable ()
658+
{
659+
const string handleVariableName = "exception_gchandle";
660+
const string intPtr = "IntPtr";
661+
// object creation
662+
var zeroPtr = MemberAccessExpression (
663+
SyntaxKind.SimpleMemberAccessExpression,
664+
IdentifierName (intPtr),
665+
IdentifierName ("Zero"));
666+
667+
var declarator = VariableDeclarator (Identifier (handleVariableName))
668+
.WithInitializer (EqualsValueClause (zeroPtr));
669+
670+
return LocalDeclarationStatement (
671+
VariableDeclaration (IdentifierName (intPtr))
672+
.WithVariables (SingletonSeparatedList (declarator)))
673+
.NormalizeWhitespace (); // no special mono style
674+
}
675+
653676
static string? GetObjCMessageSendMethodName<T> (ExportData<T> exportData,
654677
TypeInfo returnType, ImmutableArray<Parameter> parameters, bool isSuper = false, bool isStret = false)
655678
where T : Enum

tests/rgen/Microsoft.Macios.Generator.Tests/Emitters/BindingSyntaxFactoryObjCRuntimeTests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using Microsoft.CodeAnalysis;
77
using Microsoft.Macios.Generator.DataModel;
8-
using Xamarin.Utils;
98
using Xunit;
109
using static Microsoft.Macios.Generator.Emitters.BindingSyntaxFactory;
1110
using static Microsoft.Macios.Generator.Tests.TestDataFactory;
@@ -655,4 +654,12 @@ void EnsureUiThreadTests (PlatformName platform, string? expectedDeclaration)
655654
Assert.Equal (expectedDeclaration, declaration.ToString ());
656655
}
657656
}
657+
658+
[Fact]
659+
void GetExceptionHandleAuxVariableTests ()
660+
{
661+
var expected = "IntPtr exception_gchandle = IntPtr.Zero;";
662+
var declaration = GetExceptionHandleAuxVariable ();
663+
Assert.Equal (expected, declaration.ToString ());
664+
}
658665
}

0 commit comments

Comments
 (0)