Skip to content

Commit 4e88d1b

Browse files
mandel-macaqueGitHub Actions Autoformatter
andauthored
[Rgen] Generalize NSArray.FromNSObject to later use it for other code generation. (#22397)
The NSArray.FromNSObject is used in several parts of the code generation, for example trampolines will use it too. We generalize the call so that we can easily do function composition to generate the code. --------- Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
1 parent 756eb06 commit 4e88d1b

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,17 +562,10 @@ internal static BinaryExpressionSyntax ByteToBool (InvocationExpressionSyntax in
562562
.WithExpressionBody (constructor.WithLeadingTrivia (Space));
563563

564564
// generate: NSArray.FromNSObjects (o => new NSNumber (o), shape);
565-
var factoryInvocation = InvocationExpression (MemberAccessExpression (
566-
SyntaxKind.SimpleMemberAccessExpression,
567-
IdentifierName ("NSArray"),
568-
IdentifierName ("FromNSObjects").WithTrailingTrivia (Space))).WithArgumentList (
569-
ArgumentList (
570-
SeparatedList<ArgumentSyntax> (
571-
new SyntaxNodeOrToken [] {
572-
Argument (lambdaExpression),
573-
Token (SyntaxKind.CommaToken),
574-
Argument (IdentifierName (parameter.Name).WithLeadingTrivia (Space))
575-
})));
565+
var factoryInvocation = NSArrayFromNSObjects ([
566+
Argument (lambdaExpression),
567+
Argument (IdentifierName (parameter.Name))
568+
]);
576569

577570
var declarator =
578571
VariableDeclarator (Identifier (variableName).WithLeadingTrivia (Space).WithTrailingTrivia (Space))

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ internal static InvocationExpressionSyntax NSArrayFromHandleFunc (string returnT
347347
.WithArgumentList (argumentList);
348348
}
349349

350+
/// <summary>
351+
/// Factory method that returns the expression for the NSArray.FromNSObjects invocation.
352+
/// </summary>
353+
/// <param name="arguments">The arguments to be used with the invocation.</param>
354+
/// <returns>The NSArray.FromNSObjects invocation.</returns>
355+
internal static InvocationExpressionSyntax NSArrayFromNSObjects (ImmutableArray<ArgumentSyntax> arguments)
356+
{
357+
var argumentList = ArgumentList (
358+
SeparatedList<ArgumentSyntax> (arguments.ToSyntaxNodeOrTokenArray ()));
359+
360+
return InvocationExpression (MemberAccessExpression (
361+
SyntaxKind.SimpleMemberAccessExpression,
362+
IdentifierName ("NSArray"),
363+
IdentifierName ("FromNSObjects").WithTrailingTrivia (Space)))
364+
.WithArgumentList (argumentList);
365+
}
366+
350367
/// <summary>
351368
/// Returns the enum extension method needed to get the value of the enum from a NativeHandle.
352369
/// </summary>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,33 @@ void SmartEnumGetValueTests (TypeInfo enumType, ImmutableArray<ArgumentSyntax> a
408408
Assert.Equal (expectedDeclaration, declaration.ToFullString ());
409409
}
410410

411+
class TestDataNSArrayFromNSObjects : IEnumerable<object []> {
412+
public IEnumerator<object []> GetEnumerator ()
413+
{
414+
yield return [
415+
ImmutableArray.Create (
416+
Argument (IdentifierName ("arg1"))),
417+
"NSArray.FromNSObjects (arg1)"
418+
];
419+
420+
yield return [
421+
ImmutableArray.Create (
422+
Argument (IdentifierName ("arg1")),
423+
Argument (IdentifierName ("arg2")),
424+
Argument (IdentifierName ("arg3"))),
425+
"NSArray.FromNSObjects (arg1, arg2, arg3)"
426+
];
427+
}
428+
429+
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
430+
}
431+
432+
[Theory]
433+
[ClassData (typeof (TestDataNSArrayFromNSObjects))]
434+
void NSArrayFromNSObjectsTests (ImmutableArray<ArgumentSyntax> arguments, string expectedDeclaration)
435+
{
436+
var declaration = NSArrayFromNSObjects (arguments);
437+
Assert.Equal (expectedDeclaration, declaration.ToFullString ());
438+
}
439+
411440
}

0 commit comments

Comments
 (0)