From 540676f81d860786294c6e4a39926e46ae5c98a4 Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Fri, 31 May 2024 17:34:21 +1000 Subject: [PATCH 1/2] Add support for .NET 8 Math functions --- .../Linq/Functions/MathGenerator.cs | 44 +++++++++++++++++-- .../Linq/Functions/RoundGenerator.cs | 7 +++ .../Linq/Functions/TruncateGenerator.cs | 5 +++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/NHibernate/Linq/Functions/MathGenerator.cs b/src/NHibernate/Linq/Functions/MathGenerator.cs index 5f8351f1f3c..990ea27e0df 100644 --- a/src/NHibernate/Linq/Functions/MathGenerator.cs +++ b/src/NHibernate/Linq/Functions/MathGenerator.cs @@ -80,16 +80,52 @@ public MathGenerator() ReflectHelper.FastGetMethod(MathF.Ceiling, default(float)), ReflectHelper.FastGetMethod(MathF.Pow, default(float), default(float)), +#endif +#if NET8_0_OR_GREATER + ReflectHelper.FastGetMethod(float.Sin, default(float)), + ReflectHelper.FastGetMethod(float.Cos, default(float)), + ReflectHelper.FastGetMethod(float.Tan, default(float)), + ReflectHelper.FastGetMethod(float.Sinh, default(float)), + ReflectHelper.FastGetMethod(float.Cosh, default(float)), + ReflectHelper.FastGetMethod(float.Tanh, default(float)), + ReflectHelper.FastGetMethod(float.Asin, default(float)), + ReflectHelper.FastGetMethod(float.Acos, default(float)), + ReflectHelper.FastGetMethod(float.Atan, default(float)), + ReflectHelper.FastGetMethod(float.Atan2, default(float), default(float)), + ReflectHelper.FastGetMethod(float.Sqrt, default(float)), + ReflectHelper.FastGetMethod(float.Abs, default(float)), + ReflectHelper.FastGetMethod(float.Sign, default(float)), + ReflectHelper.FastGetMethod(float.Floor, default(float)), + ReflectHelper.FastGetMethod(float.Ceiling, default(float)), + ReflectHelper.FastGetMethod(float.Pow, default(float), default(float)), + + ReflectHelper.FastGetMethod(double.Sin, default(double)), + ReflectHelper.FastGetMethod(double.Cos, default(double)), + ReflectHelper.FastGetMethod(double.Tan, default(double)), + ReflectHelper.FastGetMethod(double.Sinh, default(double)), + ReflectHelper.FastGetMethod(double.Cosh, default(double)), + ReflectHelper.FastGetMethod(double.Tanh, default(double)), + ReflectHelper.FastGetMethod(double.Asin, default(double)), + ReflectHelper.FastGetMethod(double.Acos, default(double)), + ReflectHelper.FastGetMethod(double.Atan, default(double)), + ReflectHelper.FastGetMethod(double.Atan2, default(double), default(double)), + ReflectHelper.FastGetMethod(double.Sqrt, default(double)), + ReflectHelper.FastGetMethod(double.Abs, default(double)), + ReflectHelper.FastGetMethod(double.Sign, default(double)), + ReflectHelper.FastGetMethod(double.Floor, default(double)), + ReflectHelper.FastGetMethod(double.Ceiling, default(double)), + ReflectHelper.FastGetMethod(double.Pow, default(double), default(double)), #endif }; } public override HqlTreeNode BuildHql(MethodInfo method, Expression expression, ReadOnlyCollection arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) { - string function = method.Name.ToLowerInvariant(); - - if (function == "pow") - function = "power"; + var function = method.Name.ToLowerInvariant() switch + { + "pow" => "power", + var f => f, + }; var firstArgument = visitor.Visit(arguments[0]).AsExpression(); diff --git a/src/NHibernate/Linq/Functions/RoundGenerator.cs b/src/NHibernate/Linq/Functions/RoundGenerator.cs index ec349b157d8..b5d82f6cc87 100644 --- a/src/NHibernate/Linq/Functions/RoundGenerator.cs +++ b/src/NHibernate/Linq/Functions/RoundGenerator.cs @@ -25,6 +25,13 @@ public RoundGenerator() ReflectHelper.FastGetMethod(MathF.Round, default(float)), ReflectHelper.FastGetMethod(MathF.Round, default(float), default(int)), #endif + +#if NET8_0_OR_GREATER + ReflectHelper.FastGetMethod(float.Round, default(float)), + ReflectHelper.FastGetMethod(float.Round, default(float), default(int)), + ReflectHelper.FastGetMethod(double.Round, default(double)), + ReflectHelper.FastGetMethod(double.Round, default(double), default(int)), +#endif }; } diff --git a/src/NHibernate/Linq/Functions/TruncateGenerator.cs b/src/NHibernate/Linq/Functions/TruncateGenerator.cs index d2f646f6ebd..07b10997091 100644 --- a/src/NHibernate/Linq/Functions/TruncateGenerator.cs +++ b/src/NHibernate/Linq/Functions/TruncateGenerator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.ObjectModel; using System.Linq.Expressions; +using System.Numerics; using System.Reflection; using NHibernate.Hql.Ast; using NHibernate.Linq.Visitors; @@ -20,6 +21,10 @@ public TruncateGenerator() #if NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER ReflectHelper.FastGetMethod(MathF.Truncate, default(float)), +#endif +#if NET8_0_OR_GREATER + ReflectHelper.FastGetMethod(float.Truncate, default(float)), + ReflectHelper.FastGetMethod(double.Truncate, default(double)), #endif }; } From 8c974c0b3681bcf5ee795fbd1e723f882fbcda4d Mon Sep 17 00:00:00 2001 From: Alex Zaytsev Date: Fri, 31 May 2024 17:35:25 +1000 Subject: [PATCH 2/2] Cleanup --- src/NHibernate/Linq/Functions/TruncateGenerator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NHibernate/Linq/Functions/TruncateGenerator.cs b/src/NHibernate/Linq/Functions/TruncateGenerator.cs index 07b10997091..ba404cdb9b2 100644 --- a/src/NHibernate/Linq/Functions/TruncateGenerator.cs +++ b/src/NHibernate/Linq/Functions/TruncateGenerator.cs @@ -1,7 +1,6 @@ using System; using System.Collections.ObjectModel; using System.Linq.Expressions; -using System.Numerics; using System.Reflection; using NHibernate.Hql.Ast; using NHibernate.Linq.Visitors;