Browse Source

Should rfecognize recursion even if call is hidden in different variables

auz34 11 years ago
parent
commit
c03869d2f7
3 changed files with 26 additions and 7 deletions
  1. 20 1
      Jint.Tests/Runtime/EngineTests.cs
  2. 2 2
      Jint/Engine.cs
  3. 4 4
      Jint/Runtime/ExpressionIntepreter.cs

+ 20 - 1
Jint.Tests/Runtime/EngineTests.cs

@@ -606,7 +606,7 @@ namespace Jint.Tests.Runtime
         }
         }
 
 
         [Fact]
         [Fact]
-        public void ShouldThrowRecursionDiscarded()
+        public void ShouldDiscardRecursion()
         {
         {
             var script = @"var factorial = function(n) {
             var script = @"var factorial = function(n) {
                 if (n>1) {
                 if (n>1) {
@@ -622,6 +622,25 @@ namespace Jint.Tests.Runtime
             );
             );
         }
         }
 
 
+        [Fact]
+        public void ShouldDiscardHiddenRecursion()
+        {
+            var script = @"var renamedFunc;
+            var exec = function(callback) {
+                renamedFunc = callback;
+                callback();
+            };
+
+            var result = exec(function() {
+                renamedFunc();
+            });
+            ";
+
+            Assert.Throws<RecursionDiscardedException>(
+                () => new Engine(cfg => cfg.DiscardRecursion()).Execute(script)
+            );
+        }
+
         [Fact]
         [Fact]
         public void ShouldConvertDoubleToStringWithoutLosingPrecision()
         public void ShouldConvertDoubleToStringWithoutLosingPrecision()
         {
         {

+ 2 - 2
Jint/Engine.cs

@@ -35,9 +35,9 @@ namespace Jint
         private SyntaxNode _lastSyntaxNode = null;
         private SyntaxNode _lastSyntaxNode = null;
 
 
         // cache of types used when resolving CLR type names
         // cache of types used when resolving CLR type names
-        internal Dictionary<string, Type> TypeCache = new Dictionary<string, Type>(); 
+        internal Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();
 
 
-        internal Stack<string> CallStack = new Stack<string>(); 
+        internal Stack<CallExpression> CallStack = new Stack<CallExpression>(); 
 
 
         public Engine() : this(null)
         public Engine() : this(null)
         {
         {

+ 4 - 4
Jint/Runtime/ExpressionIntepreter.cs

@@ -798,8 +798,8 @@ namespace Jint.Runtime
             
             
             var r = callee as Reference;
             var r = callee as Reference;
 
 
-            var funcName = r.GetReferencedName();
-            var recursionDepth = _engine.CallStack.Count(s => s == funcName);
+            //var funcName = r.GetReferencedName();
+            var recursionDepth = _engine.CallStack.Count(ce => ce == callExpression);
 
 
             if (recursionDepth > 0)
             if (recursionDepth > 0)
             {
             {
@@ -808,8 +808,8 @@ namespace Jint.Runtime
                     throw new RecursionDiscardedException();
                     throw new RecursionDiscardedException();
                 }
                 }
             }
             }
-            
-            _engine.CallStack.Push(funcName);
+
+            _engine.CallStack.Push(callExpression);
 
 
             if (func == Undefined.Instance)
             if (func == Undefined.Instance)
             {
             {