Skip to content

Commit 1af1b36

Browse files
[RGen] Provide code that generates the sync call for an async implementation. (#23275)
1 parent 6b6cd08 commit 1af1b36

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ static partial class BindingSyntaxFactory {
7070
foreach (var parameter in delegateType.Delegate.Parameters [..^1]) {
7171
noNSErrorArgs.Add (Argument (IdentifierName (GetTaskCallbackParameterName (parameter.Name))));
7272
}
73+
var resultsArgs = noNSErrorArgs.ToImmutable ();
74+
var resultArgsSyntax = resultsArgs.Length == 1
75+
? resultsArgs [0]
76+
: Argument (New (resultsArgs));
77+
7378
// update the if to include the result setting, we are doing by hand to fix the indentation to match monos
7479
ifErrorNotNull = IfStatement (
7580
attributeLists: default,
@@ -87,7 +92,7 @@ static partial class BindingSyntaxFactory {
8792
ExpressionStatement (
8893
TcsSetResult (
8994
tcsVariableName: completionSourceName,
90-
arguments: [Argument (New (noNSErrorArgs.ToImmutable ()))]
95+
arguments: [resultArgsSyntax]
9196
)
9297
).WithLeadingTrivia (LineFeed, Tab, Tab)
9398
).WithLeadingTrivia (LineFeed, Tab)
@@ -101,11 +106,17 @@ static partial class BindingSyntaxFactory {
101106
foreach (var parameter in delegateType.Delegate.Parameters) {
102107
arguments.Add (Argument (IdentifierName (GetTaskCallbackParameterName (parameter.Name))));
103108
}
109+
110+
var argList = arguments.ToImmutable ();
104111
// return a single expression that sets the result of the tcs
112+
var argSyntax = argList.Length == 1
113+
? argList [0]
114+
: Argument (New (argList));
115+
105116
return ExpressionStatement (
106117
TcsSetResult (
107118
tcsVariableName: completionSourceName,
108-
arguments: [Argument (New (arguments.ToImmutable ()))]
119+
arguments: [argSyntax]
109120
)
110121
).WithLeadingTrivia (Tab);
111122
}
@@ -140,6 +151,28 @@ internal static ExpressionSyntax GetCallbackDeclaration (in TypeInfo delegateTyp
140151
.WithBlock (block);
141152
}
142153

154+
/// <summary>
155+
/// Generates an invocation expression for the synchronous part of an async method wrapper.
156+
/// </summary>
157+
/// <param name="method">The method to be called.</param>
158+
/// <returns>An invocation expression syntax.</returns>
159+
internal static ExpressionSyntax ExecuteSyncCall (in Method method)
160+
{
161+
// retrieve the last parameter from the method, since that should be the completion handler
162+
var completionHandler = GetCallbackDeclaration (method.Parameters [^1].Type);
163+
var arguments = ImmutableArray.CreateBuilder<ArgumentSyntax> (method.Parameters.Length);
164+
// build the arguments for the method call, those are the same arguments as the method parameters but the last one
165+
foreach (var parameter in method.Parameters [..^1]) {
166+
arguments.Add (Argument (IdentifierName (parameter.Name)));
167+
}
168+
// add the completion handler as the last argument
169+
arguments.Add (Argument (completionHandler));
170+
171+
var argumentList = ArgumentList (
172+
SeparatedList<ArgumentSyntax> (arguments.ToSyntaxNodeOrTokenArray ()));
173+
return InvocationExpression (IdentifierName (method.Name).WithTrailingTrivia (Space)).WithArgumentList (argumentList);
174+
}
175+
143176
/// <summary>
144177
/// Generates an invocation expression for sending an Objective-C message.
145178
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public void MyMethod (Callback cb) {}
378378
yield return [
379379
singleArgNoError,
380380
@"(_cbvalue) => {
381-
_tcs.SetResult (new (_cbvalue));
381+
_tcs.SetResult (_cbvalue);
382382
}",
383383
];
384384

@@ -402,7 +402,7 @@ public void MyMethod (Callback cb) {}
402402
if (_cberror is not null)
403403
_tcs.SetException (new global::Foundation.NSErrorException (_cberror));
404404
else
405-
_tcs.SetResult (new (_cbvalue));
405+
_tcs.SetResult (_cbvalue);
406406
}",
407407
];
408408

0 commit comments

Comments
 (0)