Browse Source

Introduce structures to manage CallStack nicer and faster

auz34 11 years ago
parent
commit
6ab9a74965

+ 3 - 0
Jint/Jint.csproj

@@ -159,6 +159,9 @@
     <Compile Include="Parser\Token.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Runtime\Arguments.cs" />
+    <Compile Include="Runtime\CallStack\JintCallStack.cs" />
+    <Compile Include="Runtime\CallStack\CallStackElementComparer.cs" />
+    <Compile Include="Runtime\CallStack\CallStackElement.cs" />
     <Compile Include="Runtime\Completion.cs" />
     <Compile Include="Runtime\Descriptors\PropertyDescriptor.cs" />
     <Compile Include="Runtime\Descriptors\Specialized\FieldInfoDescriptor.cs" />

+ 26 - 0
Jint/Runtime/CallStack/CallStackElement.cs

@@ -0,0 +1,26 @@
+namespace Jint.Runtime
+{
+    using Jint.Native;
+    using Jint.Parser.Ast;
+
+    public class CallStackElement
+    {
+        private string _shortDescription;
+
+        public CallStackElement(CallExpression callExpression, JsValue function, string shortDescription)
+        {
+            _shortDescription = shortDescription;
+            CallExpression = callExpression;
+            Function = function;
+        }
+
+        public CallExpression CallExpression { get; private set; }
+
+        public JsValue Function { get; private set; }
+
+        public override string ToString()
+        {
+            return _shortDescription;
+        }
+    }
+}

+ 17 - 0
Jint/Runtime/CallStack/CallStackElementComparer.cs

@@ -0,0 +1,17 @@
+namespace Jint.Runtime.CallStack
+{
+    using System.Collections.Generic;
+
+    public class CallStackElementComparer: IEqualityComparer<CallStackElement>
+    {
+        public bool Equals(CallStackElement x, CallStackElement y)
+        {
+            return x.CallExpression == y.CallExpression || x.Function == y.Function;
+        }
+
+        public int GetHashCode(CallStackElement obj)
+        {
+            return obj.CallExpression.GetHashCode() + obj.Function.GetHashCode();
+        }
+    }
+}

+ 41 - 0
Jint/Runtime/CallStack/JintCallStack.cs

@@ -0,0 +1,41 @@
+namespace Jint.Runtime.CallStack
+{
+    using System.Collections.Generic;
+
+    public class JintCallStack
+    {
+        private Stack<CallStackElement> _stack = new Stack<CallStackElement>();
+
+        private Dictionary<CallStackElement, int> _statistics =
+            new Dictionary<CallStackElement, int>(new CallStackElementComparer());
+
+        public int Push(CallStackElement item)
+        {
+            _stack.Push(item);
+            if (_statistics.ContainsKey(item))
+            {
+                return ++_statistics[item];
+            }
+            else
+            {
+                _statistics.Add(item, 0);
+                return 0;
+            }
+        }
+
+        public void Pop()
+        {
+            var item = _stack.Pop();
+            if (_statistics[item] == 0)
+            {
+                _statistics.Remove(item);
+            }
+            else
+            {
+                _statistics[item]--;
+            }
+        }
+
+        // TODO printing Call Stack might become useful for debugging purposes
+    }
+}