Skip to content

Commit

Permalink
Updated code formatting to match rules in .editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Aug 28, 2024
1 parent b84df12 commit 275df7a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 114 deletions.
128 changes: 64 additions & 64 deletions src/Hyperbee.AsyncExpressions/AsyncExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

namespace Hyperbee.AsyncExpressions;

[DebuggerDisplay( "{_body}" )]
[DebuggerTypeProxy( typeof(AsyncExpressionProxy) )]
[DebuggerDisplay("{_body}")]
[DebuggerTypeProxy(typeof(AsyncExpressionProxy))]
public class AsyncExpression : Expression
{
private readonly Expression _body;
private Expression _visitedBody;
private bool _isVisited;

internal AsyncExpression( Expression body )
internal AsyncExpression(Expression body)
{
ArgumentNullException.ThrowIfNull( body, nameof( body ) );
ArgumentNullException.ThrowIfNull(body, nameof(body));

_body = body;
}
Expand All @@ -24,10 +24,10 @@ internal AsyncExpression( Expression body )

public override Type Type => _body.Type;

internal static Task<T> ExecuteAsync<T>( Expression body, ParameterExpression[] parameterExpressions, params object[] parameters )
internal static Task<T> ExecuteAsync<T>(Expression body, ParameterExpression[] parameterExpressions, params object[] parameters)
{
var builder = AsyncTaskMethodBuilder<T>.Create();
var stateMachine = new StateMachine<T>( ref builder, body, parameterExpressions, parameters );
var stateMachine = new StateMachine<T>(ref builder, body, parameterExpressions, parameters);
stateMachine.MoveNext();
return stateMachine.Task;
}
Expand All @@ -36,102 +36,102 @@ internal static Task<T> ExecuteAsync<T>( Expression body, ParameterExpression[]

public override Expression Reduce()
{
if ( !_isVisited )
if (!_isVisited)
ApplyVisitor();

return _visitedBody;
}

private void ApplyVisitor()
{
if ( _isVisited )
if (_isVisited)
return;

_isVisited = true;

var visitor = new AsyncVisitor( _body );
_visitedBody = visitor.Visit( _body );
var visitor = new AsyncVisitor(_body);
_visitedBody = visitor.Visit(_body);
}

internal class AsyncVisitor : ExpressionVisitor
{
private readonly Expression _body;
public List<ParameterExpression> VisitedParameters { get; } = [];

public AsyncVisitor( Expression body )
public AsyncVisitor(Expression body)
{
_body = body;
}

protected override Expression VisitParameter( ParameterExpression node )
protected override Expression VisitParameter(ParameterExpression node)
{
// Add parameter if it's not already included
if ( !VisitedParameters.Contains( node ) )
if (!VisitedParameters.Contains(node))
{
VisitedParameters.Add( node );
VisitedParameters.Add(node);
}

return base.VisitParameter( node );
return base.VisitParameter(node);
}

protected override Expression VisitMethodCall( MethodCallExpression node )
protected override Expression VisitMethodCall(MethodCallExpression node)
{
// Visit each argument to process any nested parameters
foreach ( var argument in node.Arguments )
foreach (var argument in node.Arguments)
{
Visit( argument );
Visit(argument);
}

if ( node.Object?.Type == typeof(AsyncExpression) )
if (node.Object?.Type == typeof(AsyncExpression))
{
return node;
}

// If the method call is async, transform it
if ( typeof( Task ).IsAssignableFrom( node.Type ) )
if (typeof(Task).IsAssignableFrom(node.Type))
{
var executeMethod = typeof( AsyncExpression )
.GetMethod( nameof( ExecuteAsync ), BindingFlags.Static | BindingFlags.NonPublic, [typeof(Expression), typeof(ParameterExpression[]), typeof( object[] )] )!
.MakeGenericMethod( node.Type.GetGenericArguments()[0] );
var executeMethod = typeof(AsyncExpression)
.GetMethod(nameof(ExecuteAsync), BindingFlags.Static | BindingFlags.NonPublic, [typeof(Expression), typeof(ParameterExpression[]), typeof(object[])])!
.MakeGenericMethod(node.Type.GetGenericArguments()[0]);

var arguments = VisitedParameters.Select( p => Convert( p, typeof(object) ) ).Cast<Expression>().ToArray();
var argArray = NewArrayInit( typeof( object ), arguments );
var arguments = VisitedParameters.Select(p => Convert(p, typeof(object))).Cast<Expression>().ToArray();
var argArray = NewArrayInit(typeof(object), arguments);

//return ExecuteAsyncExpression(node.Type.GetGenericArguments()[0], _body, VisitedParameters.ToArray(), arguments);
return Call(executeMethod!, [Constant(_body), Constant(VisitedParameters.ToArray()), argArray] );
return Call(executeMethod!, [Constant(_body), Constant(VisitedParameters.ToArray()), argArray]);
}

return base.VisitMethodCall( node );
return base.VisitMethodCall(node);
}

protected override Expression VisitInvocation( InvocationExpression node )
protected override Expression VisitInvocation(InvocationExpression node)
{
// Visit each argument to process any nested parameters
foreach ( var argument in node.Arguments )
foreach (var argument in node.Arguments)
{
Visit( argument );
Visit(argument);
}

if ( node.Type == typeof(AsyncExpression) )
if (node.Type == typeof(AsyncExpression))
{
return node;
}

// If the invocation is async, transform it
if ( typeof(Task).IsAssignableFrom( node.Type ) )
if (typeof(Task).IsAssignableFrom(node.Type))
{
var executeMethod = typeof(AsyncExpression)
.GetMethod(nameof(ExecuteAsync), BindingFlags.Static | BindingFlags.NonPublic, [typeof(Expression), typeof(ParameterExpression[]), typeof(object[])])!
.MakeGenericMethod(node.Type.GetGenericArguments()[0]);

var arguments = node.Arguments.Select( a => Convert( a, typeof(object) ) ).ToArray();
var argArray = NewArrayInit( typeof(object), arguments );
var arguments = node.Arguments.Select(a => Convert(a, typeof(object))).ToArray();
var argArray = NewArrayInit(typeof(object), arguments);

//return ExecuteAsyncExpression(node.Type.GetGenericArguments()[0], _body, VisitedParameters.ToArray(), arguments);
return Call(executeMethod!, [Constant(_body), Constant(VisitedParameters.ToArray()), argArray]);
}

return base.VisitInvocation( node );
return base.VisitInvocation(node);
}
}
/*
Expand Down Expand Up @@ -182,7 +182,7 @@ public static Expression ExecuteAsyncExpression(Type resultType, Expression body
returnTask
);
}*/


private struct StateMachine<T> : IAsyncStateMachine
{
Expand All @@ -193,15 +193,15 @@ private struct StateMachine<T> : IAsyncStateMachine
private readonly object[] _parameters;
private readonly ParameterExpression[] _parameterExpressions;

public StateMachine( ref AsyncTaskMethodBuilder<T> builder, Expression body, ParameterExpression[] parameterExpressions, object[] parameters )
public StateMachine(ref AsyncTaskMethodBuilder<T> builder, Expression body, ParameterExpression[] parameterExpressions, object[] parameters)
{
_builder = builder; // critical: this makes a copy of builder
_body = body;
_parameterExpressions = parameterExpressions;
_parameters = parameters;
_state = -1;

SetStateMachine( this );
SetStateMachine(this);
}

public Task<T> Task => _builder.Task;
Expand All @@ -212,24 +212,24 @@ public void MoveNext()
{
ConfiguredTaskAwaitable<T>.ConfiguredTaskAwaiter awaiter;

if ( _state != 0 )
if (_state != 0)
{
// Initial state: compile the expression and execute it
var delegateType = GetDelegateType( _parameterExpressions.Select( p => p.Type ).Concat( [typeof(Task<T>)] ).ToArray() );
var lambda = Lambda( delegateType, _body, _parameterExpressions );

var delegateType = GetDelegateType(_parameterExpressions.Select(p => p.Type).Concat([typeof(Task<T>)]).ToArray());
var lambda = Lambda(delegateType, _body, _parameterExpressions);
var compiledLambda = lambda.Compile();
var task = compiledLambda.DynamicInvoke( _parameters );
var task = compiledLambda.DynamicInvoke(_parameters);

awaiter = ((Task<T>) task!).ConfigureAwait( false ).GetAwaiter();
awaiter = ((Task<T>)task!).ConfigureAwait(false).GetAwaiter();

if ( !awaiter.IsCompleted )
if (!awaiter.IsCompleted)
{
_state = 0;
_awaiter = awaiter;

// Schedule a continuation
_builder.AwaitUnsafeOnCompleted( ref awaiter, ref this );
_builder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
return;
}
}
Expand All @@ -244,35 +244,35 @@ public void MoveNext()
// Final state: success
var result = awaiter.GetResult();
_state = -2;
_builder.SetResult( result );
_builder.SetResult(result);
}
catch ( Exception ex )
catch (Exception ex)
{
// Final state: error
_state = -2;
_builder.SetException( ex );
_builder.SetException(ex);
}
}

public void SetStateMachine( IAsyncStateMachine stateMachine )
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
_builder.SetStateMachine( stateMachine );
_builder.SetStateMachine(stateMachine);
}
}

public class AsyncExpressionProxy( AsyncExpression node )
public class AsyncExpressionProxy(AsyncExpression node)
{
public Expression Body => node._body;
}

public static AsyncExpression CallAsync( MethodInfo methodInfo, params Expression[] arguments )
public static AsyncExpression CallAsync(MethodInfo methodInfo, params Expression[] arguments)
{
if ( !IsAsyncMethod( methodInfo ) )
throw new ArgumentException( "The specified method is not an async.", nameof( methodInfo ) );
if (!IsAsyncMethod(methodInfo))
throw new ArgumentException("The specified method is not an async.", nameof(methodInfo));

return new AsyncExpression( Call( methodInfo, arguments ) );
return new AsyncExpression(Call(methodInfo, arguments));

static bool IsAsyncMethod( MethodInfo methodInfo )
static bool IsAsyncMethod(MethodInfo methodInfo)
{
var returnType = methodInfo.ReturnType;

Expand All @@ -283,14 +283,14 @@ static bool IsAsyncMethod( MethodInfo methodInfo )
}
}

public static AsyncExpression LambdaAsync( LambdaExpression lambdaExpression, params Expression[] arguments )
public static AsyncExpression LambdaAsync(LambdaExpression lambdaExpression, params Expression[] arguments)
{
if ( !IsAsyncLambda( lambdaExpression ) )
throw new ArgumentException( "The specified lambda is not an async.", nameof(lambdaExpression) );
if (!IsAsyncLambda(lambdaExpression))
throw new ArgumentException("The specified lambda is not an async.", nameof(lambdaExpression));

return new AsyncExpression( Invoke( lambdaExpression, arguments ) );
return new AsyncExpression(Invoke(lambdaExpression, arguments));

static bool IsAsyncLambda( LambdaExpression lambdaExpression )
static bool IsAsyncLambda(LambdaExpression lambdaExpression)
{
var returnType = lambdaExpression.ReturnType;

Expand All @@ -300,8 +300,8 @@ static bool IsAsyncLambda( LambdaExpression lambdaExpression )
}
}

public static AwaitExpression Await( AsyncExpression asyncExpression, bool configureAwait )
public static AwaitExpression Await(AsyncExpression asyncExpression, bool configureAwait)
{
return new AwaitExpression( asyncExpression, configureAwait );
return new AwaitExpression(asyncExpression, configureAwait);
}
}
32 changes: 16 additions & 16 deletions src/Hyperbee.AsyncExpressions/AwaitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class AwaitExpression : Expression
private readonly Expression _asyncExpression;
private readonly bool _configureAwait;

internal AwaitExpression( Expression asyncExpression, bool configureAwait )
internal AwaitExpression(Expression asyncExpression, bool configureAwait)
{
_asyncExpression = asyncExpression ?? throw new ArgumentNullException( nameof( asyncExpression ) );
_asyncExpression = asyncExpression ?? throw new ArgumentNullException(nameof(asyncExpression));
_configureAwait = configureAwait;
}

Expand All @@ -22,43 +22,43 @@ public override Type Type
{
var taskType = _asyncExpression.Type;

if ( taskType.IsGenericType && taskType.GetGenericTypeDefinition() == typeof( Task<> ) )
if (taskType.IsGenericType && taskType.GetGenericTypeDefinition() == typeof(Task<>))
{
return taskType.GetGenericArguments()[0];
}

return taskType == typeof( Task )
? typeof( void )
: throw new InvalidOperationException( "Unsupported type in AwaitExpression." );
return taskType == typeof(Task)
? typeof(void)
: throw new InvalidOperationException("Unsupported type in AwaitExpression.");
}
}

public override bool CanReduce => true;

public override Expression Reduce()
{
if ( Type == typeof(void) )
if (Type == typeof(void))
{
var awaitMethod = typeof(AwaitExpression).GetMethod( nameof(Await), BindingFlags.NonPublic | BindingFlags.Static );
return Call( awaitMethod!, _asyncExpression, Constant( _configureAwait ) );
var awaitMethod = typeof(AwaitExpression).GetMethod(nameof(Await), BindingFlags.NonPublic | BindingFlags.Static);
return Call(awaitMethod!, _asyncExpression, Constant(_configureAwait));
}
else
{
var awaitMethod = typeof(AwaitExpression).GetMethods( BindingFlags.NonPublic | BindingFlags.Static ).First( m => m.Name == nameof(Await) && m.IsGenericMethodDefinition );
var genericAwaitMethod = awaitMethod.MakeGenericMethod( Type );
var awaitMethod = typeof(AwaitExpression).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).First(m => m.Name == nameof(Await) && m.IsGenericMethodDefinition);
var genericAwaitMethod = awaitMethod.MakeGenericMethod(Type);

return Call( genericAwaitMethod, _asyncExpression, Constant( _configureAwait ) );
return Call(genericAwaitMethod, _asyncExpression, Constant(_configureAwait));
}
}

private static void Await( Task task, bool configureAwait )
private static void Await(Task task, bool configureAwait)
{
task.ConfigureAwait( configureAwait ).GetAwaiter().GetResult();
task.ConfigureAwait(configureAwait).GetAwaiter().GetResult();
}

private static T Await<T>( Task<T> task, bool configureAwait )
private static T Await<T>(Task<T> task, bool configureAwait)
{
var awaiter = task.ConfigureAwait( configureAwait ).GetAwaiter();
var awaiter = task.ConfigureAwait(configureAwait).GetAwaiter();
var result = awaiter.GetResult();
return result;
}
Expand Down
Loading

0 comments on commit 275df7a

Please sign in to comment.