JintCallStack.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. namespace Jint.Runtime.CallStack
  3. {
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. public class JintCallStack : IEnumerable<CallStackElement>
  7. {
  8. private Stack<CallStackElement> _stack = new Stack<CallStackElement>();
  9. private Dictionary<CallStackElement, int> _statistics =
  10. new Dictionary<CallStackElement, int>(new CallStackElementComparer());
  11. public int Push(CallStackElement item)
  12. {
  13. _stack.Push(item);
  14. if (_statistics.ContainsKey(item))
  15. {
  16. return ++_statistics[item];
  17. }
  18. else
  19. {
  20. _statistics.Add(item, 0);
  21. return 0;
  22. }
  23. }
  24. public CallStackElement Pop()
  25. {
  26. var item = _stack.Pop();
  27. if (_statistics[item] == 0)
  28. {
  29. _statistics.Remove(item);
  30. }
  31. else
  32. {
  33. _statistics[item]--;
  34. }
  35. return item;
  36. }
  37. public void Clear()
  38. {
  39. _stack.Clear();
  40. _statistics.Clear();
  41. }
  42. public IEnumerator<CallStackElement> GetEnumerator()
  43. {
  44. return _stack.GetEnumerator();
  45. }
  46. public override string ToString()
  47. {
  48. return string.Join("->", _stack.Select(cse => cse.ToString()).Reverse());
  49. }
  50. IEnumerator IEnumerable.GetEnumerator()
  51. {
  52. return GetEnumerator();
  53. }
  54. }
  55. }