diff --git a/Data_Eval/Data_Eval/Data_Eval.csproj b/Data_Eval/Data_Eval/Data_Eval.csproj index 4ae4925..0efc75c 100644 --- a/Data_Eval/Data_Eval/Data_Eval.csproj +++ b/Data_Eval/Data_Eval/Data_Eval.csproj @@ -7,7 +7,7 @@ true false Data_Eval.snk - 2.5.0 + 2.6.0 diff --git a/Data_Eval/Data_Eval/Data_Eval.nuspec b/Data_Eval/Data_Eval/Data_Eval.nuspec index 356dbfc..43b2d8e 100644 --- a/Data_Eval/Data_Eval/Data_Eval.nuspec +++ b/Data_Eval/Data_Eval/Data_Eval.nuspec @@ -2,7 +2,7 @@ Data.Eval - 2.5.0 + 2.6.0 Data.Eval Bruce Dunwiddie shriop @@ -10,8 +10,8 @@ https://github.com/bruce-dunwiddie/data-eval false .Net Library for Evaluating Expressions at Runtime - Added basic support for anonymous classes. - Copyright © 2020 + Cache compilation exceptions to speed up looped exception creation. + Copyright © 2023 eval expression evaluator compile diff --git a/Data_Eval/Data_Eval/Evaluator.cs b/Data_Eval/Data_Eval/Evaluator.cs index eb66ab4..3e5734b 100644 --- a/Data_Eval/Data_Eval/Evaluator.cs +++ b/Data_Eval/Data_Eval/Evaluator.cs @@ -281,6 +281,11 @@ private void Init( if (alreadyCompiled) { execution = compiledTypes[classText]; + + if (execution.Exception != null) + { + throw execution.Exception; + } } else { @@ -297,11 +302,24 @@ private void Init( Compiler compiler = new Compiler(); - Type newType = compiler.Compile( - classText, - references, - "EvalAssembly", - "CustomEvaluator"); + Type newType; + + try + { + newType = compiler.Compile( + classText, + references, + "EvalAssembly", + "CustomEvaluator"); + } + catch (CompilationException ex) + { + execution.Exception = ex; + + compiledTypes[classText] = execution; + + throw; + } execution.Constructor = new DefaultClassConstructorExpression().GetFunc( newType); @@ -549,6 +567,8 @@ private sealed class Execution public Func Constructor = null; public Dictionary Variables = new Dictionary(); + + public CompilationException Exception = null; } private sealed class ExecutionVariable diff --git a/Data_Eval/Data_Eval/Pack.bat b/Data_Eval/Data_Eval/Pack.bat index 6c06d00..8792a1a 100644 --- a/Data_Eval/Data_Eval/Pack.bat +++ b/Data_Eval/Data_Eval/Pack.bat @@ -1,3 +1,3 @@ nuget pack Data_Eval.nuspec -Symbols -SymbolPackageFormat snupkg -nuget push Data.Eval.2.5.0.nupkg -Source https://api.nuget.org/v3/index.json -apikey %NUGET_KEY% +nuget push Data.Eval.2.6.0.nupkg -Source https://api.nuget.org/v3/index.json -apikey %NUGET_KEY% pause \ No newline at end of file diff --git a/Data_Eval/Tests/EvaluatorTests.cs b/Data_Eval/Tests/EvaluatorTests.cs index 653e00a..b11e1e2 100644 --- a/Data_Eval/Tests/EvaluatorTests.cs +++ b/Data_Eval/Tests/EvaluatorTests.cs @@ -5,6 +5,8 @@ using NUnit.Framework; using Data.Eval; +using Data.Eval.Compilation; +using System.Diagnostics; namespace Tests { @@ -214,5 +216,25 @@ public void Evaluator_LinqReference() Assert.AreEqual(1, evaluator.Eval()); } + + [Ignore("Don't want to have an intensive long running test unless needed.")] + [Test] + public void Evaluator_LoopedExceptionTime() + { + Stopwatch stopwatch = Stopwatch.StartNew(); + + for (int loopCount = 0; loopCount < 200; loopCount++) + { + CompilationException ex = Assert.Throws( + delegate + { + var blah = Evaluator.Eval("return blah"); + }); + } + + stopwatch.Stop(); + + Assert.Less(stopwatch.ElapsedMilliseconds, 3000); + } } } diff --git a/Data_Eval/Tests/ReferenceTests.cs b/Data_Eval/Tests/ReferenceTests.cs index 4e6bd37..3e6dcba 100644 --- a/Data_Eval/Tests/ReferenceTests.cs +++ b/Data_Eval/Tests/ReferenceTests.cs @@ -54,12 +54,12 @@ public void Evaluator_ExecAddCallingAssemblyReference() [Test] public void Evaluator_ExecAddUsing() { - var eval = new Evaluator("message = ExampleClass.HelloWorld"); + var eval = new Evaluator("testMessage = ExampleClass.HelloWorld"); eval.AddReference(typeof(TestExternalReference.ExampleClass).Assembly.Location); eval.AddUsing("TestExternalReference"); - eval["message"] = ""; + eval["testMessage"] = ""; eval.Exec(); - Assert.AreEqual("Hello World", eval["message"]); + Assert.AreEqual("Hello World", eval["testMessage"]); } [Test]