Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,22 +1378,13 @@ public static bool TryCollectBoundConstants(ref CompilerContext context, Express

/// <summary>Collects the information about closure constants, nested lambdas, non-passed parameters, goto labels and variables in blocks.
/// Returns `OK` result if everything is fine and other result for error.</summary>
public static Result TryCollectInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable = false)
private static Result TryCollectTerminalInfo(ref CompilerContext context, Expression expr,
ExpressionType nodeType, NestedLambdaInfo nestedLambda)
{
var r = Result.OK;
var quotedData = (context.Status & ClosureStatus.QuotedData) != 0;
while (true)
switch (nodeType)
{
if (expr == null)
return Result.ExpressionIsNull;
#if LIGHT_EXPRESSION
if (!quotedData && expr.IsIntrinsic)
return expr.TryCollectInfo(ref context, nestedLambda, ref rootNestedLambdas, scopeIdx);
#endif
switch (expr.NodeType)
{
case ExpressionType.Constant:
if (quotedData) return Result.OK;
#if LIGHT_EXPRESSION
Expand Down Expand Up @@ -1428,6 +1419,47 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
}
return Result.OK;
}
default:
return r;
}
}

public static Result TryCollectInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable = false)
{
if (expr != null
#if LIGHT_EXPRESSION
&& (!expr.IsIntrinsic || (context.Status & ClosureStatus.QuotedData) != 0)
#endif
)
{
var nodeType = expr.NodeType;
if (nodeType == ExpressionType.Constant || nodeType == ExpressionType.Parameter || nodeType == ExpressionType.Default)
return TryCollectTerminalInfo(ref context, expr, nodeType, nestedLambda);
}
return TryCollectNonterminalInfo(ref context, expr, nestedLambda, ref rootNestedLambdas, scopeIdx, enterable);
}

private static Result TryCollectNonterminalInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable = false)
{
var r = Result.OK;
var quotedData = (context.Status & ClosureStatus.QuotedData) != 0;
while (true)
{
if (expr == null)
return Result.ExpressionIsNull;
#if LIGHT_EXPRESSION
if (!quotedData && expr.IsIntrinsic)
return expr.TryCollectInfo(ref context, nestedLambda, ref rootNestedLambdas, scopeIdx);
#endif
switch (expr.NodeType)
{
case ExpressionType.Constant:
case ExpressionType.Parameter:
return TryCollectTerminalInfo(ref context, expr, expr.NodeType, nestedLambda);
case ExpressionType.Call:
{
var callExpr = (MethodCallExpression)expr;
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.UnitTests.ComparisonReductionRegressionTests());
t.Run(new LightExpression.UnitTests.CompoundAssignmentExceptionRegressionTests());
t.Run(new LightExpression.UnitTests.NestedCompoundExceptionRegressionTests());
t.Run(new LightExpression.UnitTests.TerminalCollectionTests());
t.Run(new LightExpression.UnitTests.ArithmeticExceptionOperandsTests());
t.Run(new LightExpression.UnitTests.IndexerReadExceptionRegressionTests());
t.Run(new LightExpression.UnitTests.MemberBindingExceptionTests());
Expand Down Expand Up @@ -191,6 +192,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new ComparisonReductionRegressionTests());
fecTests.Run(new CompoundAssignmentExceptionRegressionTests());
fecTests.Run(new NestedCompoundExceptionRegressionTests());
fecTests.Run(new TerminalCollectionTests());
fecTests.Run(new ArithmeticExceptionOperandsTests());
fecTests.Run(new IndexerReadExceptionRegressionTests());
fecTests.Run(new MemberBindingExceptionTests());
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.UnitTests.ComparisonReductionRegressionTests());
lt.Run(new LightExpression.UnitTests.CompoundAssignmentExceptionRegressionTests());
lt.Run(new LightExpression.UnitTests.NestedCompoundExceptionRegressionTests());
lt.Run(new LightExpression.UnitTests.TerminalCollectionTests());
lt.Run(new LightExpression.UnitTests.ArithmeticExceptionOperandsTests());
lt.Run(new LightExpression.UnitTests.IndexerReadExceptionRegressionTests());
lt.Run(new LightExpression.UnitTests.MemberBindingExceptionTests());
Expand Down Expand Up @@ -217,6 +218,7 @@ public static void Main(string[] args)
st.Run(new ComparisonReductionRegressionTests());
st.Run(new CompoundAssignmentExceptionRegressionTests());
st.Run(new NestedCompoundExceptionRegressionTests());
st.Run(new TerminalCollectionTests());
st.Run(new ArithmeticExceptionOperandsTests());
st.Run(new IndexerReadExceptionRegressionTests());
st.Run(new MemberBindingExceptionTests());
Expand Down
27 changes: 27 additions & 0 deletions test/FastExpressionCompiler.UnitTests/TerminalCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct TerminalCollectionTests : ITestX
{
public void Run(TestRun t) { Bound_constants_keep_reference_identity(t); Undefined_parameters_are_still_rejected(t); }
public void Bound_constants_keep_reference_identity(TestContext t)
{
var value=new object();var boxed=(object)7;
foreach(var item in new[]{value,boxed,new string(new[]{'t','e','x','t'})})
{
var tree=S.Lambda<Func<object>>(S.Constant(item,typeof(object)));var fast=tree.FromSysExpression().CompileFast(true);if(t.IsNotNull(fast))t.IsTrue(ReferenceEquals(item,fast()));
}
var input=S.Parameter(typeof(int));var nested=S.Lambda<Func<int,Func<int>>>(S.Lambda<Func<int>>(input),input);var child=nested.FromSysExpression().CompileFast(true);if(t.IsNotNull(child))t.AreEqual(7,child(7)());
}
public void Undefined_parameters_are_still_rejected(TestContext t)
{
var tree=S.Lambda<Func<int>>(S.Parameter(typeof(int)));t.AreEqual(null,tree.FromSysExpression().CompileFast(true));
t.Throws<NotSupportedExpressionException>(()=>tree.FromSysExpression().CompileFast(true,CompilerFlags.ThrowOnNotSupportedExpression));
var typed=S.Lambda<Func<int>>(S.Default(typeof(int)));var fast=typed.FromSysExpression().CompileFast(true);if(t.IsNotNull(fast))t.AreEqual(0,fast());
}
}
Loading