-
Notifications
You must be signed in to change notification settings - Fork 130
Allow to track loop iterations using a user defined code call #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,8 @@ public CompilerOptions() | |
| /// </summary> | ||
| public bool EnableILAnalysis { get; set; } | ||
|
|
||
| public Action EmitOnLoopIteration { get; set; } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing doc comment. |
||
|
|
||
| /// <summary> | ||
| /// Performs a shallow clone of this instance. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -881,6 +881,7 @@ private CompilerOptions CreateOptions() | |
| EnableDebugging = this.EnableDebugging, | ||
| CompatibilityMode = this.CompatibilityMode, | ||
| EnableILAnalysis = this.EnableILAnalysis, | ||
| EmitOnLoopIteration = this.OnLoopIterationCall | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -1368,5 +1369,19 @@ internal Dictionary<Type, ClrStaticTypeWrapper> StaticTypeWrapperCache | |
| return this.staticTypeWrapperCache; | ||
| } | ||
| } | ||
|
|
||
| // Emit on loop iteration (before loop's branch call) | ||
| //_________________________________________________________________________________________ | ||
| private Action _onLoopIterationCall; | ||
| public Action OnLoopIterationCall | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing doc comment. |
||
| { | ||
| get { return _onLoopIterationCall; } | ||
| set | ||
| { | ||
| _onLoopIterationCall = value; | ||
| OnLoopIterationCallTarget = OnLoopIterationCall?.Target; | ||
| } | ||
| } | ||
| public object OnLoopIterationCallTarget; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be a property that returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea was to reduce further the amount of work required. In particular, avoid a call in favor of a field load
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good point, keep it a field then.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing doc comment. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| using System; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this file under the |
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using Jurassic; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| namespace UnitTests.Library | ||
| { | ||
|
|
||
| public class ClassWithCallCounters | ||
| { | ||
| private readonly int _iterationsLimit; | ||
| public int InstanceCounter = 0; | ||
| public static int StaticCounter = 0; | ||
|
|
||
| public ClassWithCallCounters(int iterationsLimit=100) | ||
| { | ||
| _iterationsLimit = iterationsLimit; | ||
| } | ||
| public void InstanceMethod() | ||
| { | ||
| InstanceCounter++; | ||
|
|
||
| if (InstanceCounter > _iterationsLimit) | ||
| throw new InvalidOperationException($"Reached a maximum of "+_iterationsLimit +" iterations"); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| public static void StaticMetod() | ||
| { | ||
| StaticCounter++; | ||
| } | ||
| } | ||
|
|
||
| [TestClass] | ||
| public class EmitUserCodeOnLoopIterationTests | ||
| { | ||
| [TestMethod] | ||
| public void OnLoopCallWithInstanceMethod() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| var classWithCallCounters = new ClassWithCallCounters(); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
|
|
||
| for (var i=0; i< 20; i++){ | ||
|
|
||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
| engine.CallGlobalFunction("Test"); | ||
| Assert.AreEqual(19, classWithCallCounters.InstanceCounter); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OnLoopCallWithInstanceMethodInDebug() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| engine.EnableDebugging = true; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use this property, it doesn't work in .NET core. |
||
| var classWithCallCounters = new ClassWithCallCounters(); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
|
|
||
| for (var i=0; i< 20; i++){ | ||
|
|
||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
| engine.CallGlobalFunction("Test"); | ||
| Assert.AreEqual(19, classWithCallCounters.InstanceCounter); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OnLoopCallWithStaticMethod() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
|
|
||
| engine.OnLoopIterationCall = ClassWithCallCounters.StaticMetod; | ||
| var staticCounterBefore = ClassWithCallCounters.StaticCounter; | ||
| var loopScript = @" | ||
| function Test(){ | ||
|
|
||
| for (var i=0; i< 20; i++){ | ||
|
|
||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
| engine.CallGlobalFunction("Test"); | ||
| Assert.AreEqual(19, ClassWithCallCounters.StaticCounter - staticCounterBefore); | ||
| } | ||
|
|
||
|
|
||
| [TestMethod] | ||
| public void OnLoopTerationAndTrivialContinueStatement() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| var classWithCallCounters = new ClassWithCallCounters(); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
| for ( var i=0; i< 10; i++){ | ||
| continue; | ||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
| engine.CallGlobalFunction("Test"); | ||
| Assert.AreEqual(19, classWithCallCounters.InstanceCounter); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OnLoopTerationAndLabeledContinueStatement() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| var classWithCallCounters = new ClassWithCallCounters(); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
| label1: | ||
| for ( var i=0; i< 10; i++){ | ||
| continue label1; | ||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
| engine.CallGlobalFunction("Test"); | ||
| Assert.AreEqual(19, classWithCallCounters.InstanceCounter); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OnNestedLoop() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| var classWithCallCounters = new ClassWithCallCounters(); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
|
|
||
| for ( var i=0; i< 10; i++){ | ||
| for (var j=0; j< 10; j++){ | ||
| } | ||
|
|
||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
|
|
||
| engine.CallGlobalFunction("Test"); | ||
|
|
||
| Assert.AreEqual(99, classWithCallCounters.InstanceCounter); | ||
|
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OnLoopTerationAndNestedLabeledContinueStatement() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| var classWithCallCounters = new ClassWithCallCounters(); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
| label1: | ||
| for ( var i=0; i< 10; i++){ | ||
| label2: | ||
| for (var j=0; j< 10; j++){ | ||
| continue label1; | ||
| } | ||
|
|
||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
|
|
||
| engine.CallGlobalFunction("Test"); | ||
|
|
||
| Assert.AreEqual(19, classWithCallCounters.InstanceCounter); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ThrowRightExceptionOnALotOfIterations() | ||
| { | ||
| var engine = new ScriptEngine(); | ||
| var classWithCallCounters = new ClassWithCallCounters(5); | ||
| engine.OnLoopIterationCall = classWithCallCounters.InstanceMethod; | ||
| var loopScript = @" | ||
| function Test(){ | ||
|
|
||
| for ( var i=0; i< 10; i++){ | ||
|
|
||
| } | ||
| }"; | ||
| engine.Evaluate(loopScript); | ||
|
|
||
| Exception e = null; | ||
| try | ||
| { | ||
| engine.CallGlobalFunction("Test"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| e = ex; | ||
| } | ||
|
|
||
| Assert.IsInstanceOfType(e, typeof(InvalidOperationException)); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
GetFieldcall can be inReflectionHelpers.