Browse Source

Finalizing post review changes

auz34 11 years ago
parent
commit
d5af1c10a1

+ 3 - 1
Jint/Engine.cs

@@ -25,6 +25,8 @@ using Jint.Runtime.References;
 
 
 namespace Jint
 namespace Jint
 {
 {
+    using Jint.Runtime.CallStack;
+
     public class Engine
     public class Engine
     {
     {
         private readonly ExpressionInterpreter _expressions;
         private readonly ExpressionInterpreter _expressions;
@@ -40,7 +42,7 @@ namespace Jint
         // 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<Tuple<CallExpression, JsValue, string>> CallStack = new Stack<Tuple<CallExpression, JsValue, string>>();
+        internal JintCallStack CallStack = new JintCallStack();
 
 
         public Engine() : this(null)
         public Engine() : this(null)
         {
         {

+ 3 - 1
Jint/Runtime/CallStack/CallStackElementComparer.cs

@@ -11,7 +11,9 @@
 
 
         public int GetHashCode(CallStackElement obj)
         public int GetHashCode(CallStackElement obj)
         {
         {
-            return obj.CallExpression.GetHashCode() + obj.Function.GetHashCode();
+            // TO DO have not found good hash function yet
+            // not sure it can be done for OR logic
+            return 0;
         }
         }
     }
     }
 }
 }

+ 15 - 1
Jint/Runtime/CallStack/JintCallStack.cs

@@ -1,6 +1,7 @@
 namespace Jint.Runtime.CallStack
 namespace Jint.Runtime.CallStack
 {
 {
     using System.Collections.Generic;
     using System.Collections.Generic;
+    using System.Linq;
 
 
     public class JintCallStack
     public class JintCallStack
     {
     {
@@ -23,7 +24,7 @@
             }
             }
         }
         }
 
 
-        public void Pop()
+        public CallStackElement Pop()
         {
         {
             var item = _stack.Pop();
             var item = _stack.Pop();
             if (_statistics[item] == 0)
             if (_statistics[item] == 0)
@@ -34,6 +35,19 @@
             {
             {
                 _statistics[item]--;
                 _statistics[item]--;
             }
             }
+
+            return item;
+        }
+
+        public void Clear()
+        {
+            _stack.Clear();
+            _statistics.Clear();
+        }
+
+        public override string ToString()
+        {
+            return string.Join("->", _stack.Select(cse => cse.ToString()).Reverse());
         }
         }
 
 
         // TODO printing Call Stack might become useful for debugging purposes
         // TODO printing Call Stack might become useful for debugging purposes

+ 8 - 13
Jint/Runtime/ExpressionIntepreter.cs

@@ -801,25 +801,20 @@ namespace Jint.Runtime
             var isRecursionHandled = _engine.Options.GetMaxRecursionDepth() >= 0;
             var isRecursionHandled = _engine.Options.GetMaxRecursionDepth() >= 0;
             if (isRecursionHandled)
             if (isRecursionHandled)
             {
             {
-                var stackItem = new Tuple<CallExpression, JsValue, string>(callExpression, func, r != null ? r.GetReferencedName() : "anonymous function");
+                var stackItem = new CallStackElement(callExpression, func, r != null ? r.GetReferencedName() : "anonymous function");
 
 
-                var recursionDepth = _engine.CallStack.Count(ce => ce.Item1 == callExpression || ce.Item2 == func);
+                var recursionDepth = _engine.CallStack.Push(stackItem);
 
 
-                if (recursionDepth > 0)
+                if (recursionDepth > _engine.Options.GetMaxRecursionDepth())
                 {
                 {
-                    if (_engine.Options.GetMaxRecursionDepth() == 0)
+                    _engine.CallStack.Pop();
+                    if (recursionDepth == 1)
                     {
                     {
-                        throw new RecursionDiscardedException(_engine.CallStack, stackItem);
-                    }
-
-                    if (_engine.Options.GetMaxRecursionDepth() != 0
-                        && recursionDepth > _engine.Options.GetMaxRecursionDepth())
-                    {
-                        throw new RecursionDepthOverflowException(_engine.CallStack, stackItem);
+                        throw new RecursionDiscardedException(_engine.CallStack, stackItem.ToString());
                     }
                     }
+                    
+                    throw new RecursionDepthOverflowException(_engine.CallStack, stackItem.ToString());
                 }
                 }
-
-                _engine.CallStack.Push(stackItem);
             }
             }
 
 
             if (func == Undefined.Instance)
             if (func == Undefined.Instance)

+ 5 - 3
Jint/Runtime/RecursionDepthOverflowException.cs

@@ -7,18 +7,20 @@ using Jint.Parser.Ast;
 
 
 namespace Jint.Runtime
 namespace Jint.Runtime
 {
 {
+    using Jint.Runtime.CallStack;
+
     public class RecursionDepthOverflowException : Exception
     public class RecursionDepthOverflowException : Exception
     {
     {
         public string CallChain { get; private set; }
         public string CallChain { get; private set; }
 
 
         public string CallExpressionReference { get; private set; }
         public string CallExpressionReference { get; private set; }
 
 
-        public RecursionDepthOverflowException(Stack<Tuple<CallExpression, JsValue, string>> currentStack, Tuple<CallExpression, JsValue, string> currentExpression)
+        public RecursionDepthOverflowException(JintCallStack currentStack, string currentExpressionReference)
             : base("The recursion is forbidden by script host.")
             : base("The recursion is forbidden by script host.")
         {
         {
-            CallExpressionReference = currentExpression.Item3;
+            CallExpressionReference = currentExpressionReference;
 
 
-            CallChain = string.Join("->", currentStack.Select(t => t.Item3).ToArray().Reverse());
+            CallChain = currentStack.ToString();
         }
         }
     }
     }
     
     

+ 5 - 3
Jint/Runtime/RecursionDiscardedException.cs

@@ -6,18 +6,20 @@ using Jint.Parser.Ast;
 
 
 namespace Jint.Runtime
 namespace Jint.Runtime
 {
 {
+    using Jint.Runtime.CallStack;
+
     public class RecursionDiscardedException : Exception 
     public class RecursionDiscardedException : Exception 
     {
     {
         public string CallChain { get; private set; }
         public string CallChain { get; private set; }
 
 
         public string CallExpressionReference { get; private set; }
         public string CallExpressionReference { get; private set; }
 
 
-        public RecursionDiscardedException(Stack<Tuple<CallExpression, JsValue, string>> currentStack, Tuple<CallExpression, JsValue, string> currentExpression)
+        public RecursionDiscardedException(JintCallStack currentStack, string currentExpressionReference)
             : base("The recursion is forbidden by script host.")
             : base("The recursion is forbidden by script host.")
         {
         {
-            CallExpressionReference = currentExpression.Item3;
+            CallExpressionReference = currentExpressionReference;
 
 
-            CallChain = string.Join("->", currentStack.Select(t => t.Item3).ToArray().Reverse());
+            CallChain = currentStack.ToString();
         }
         }
     }
     }
 }
 }