JintCallStack.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #nullable enable
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Text;
  4. using Esprima;
  5. using Esprima.Ast;
  6. using Jint.Collections;
  7. using Jint.Pooling;
  8. namespace Jint.Runtime.CallStack
  9. {
  10. internal sealed class JintCallStack
  11. {
  12. private readonly RefStack<CallStackElement> _stack = new();
  13. private readonly Dictionary<CallStackElement, int>? _statistics;
  14. // Internal for use by DebugHandler
  15. internal RefStack<CallStackElement> Stack => _stack;
  16. public JintCallStack(bool trackRecursionDepth)
  17. {
  18. if (trackRecursionDepth)
  19. {
  20. _statistics = new Dictionary<CallStackElement, int>(CallStackElementComparer.Instance);
  21. }
  22. }
  23. public int Push(in CallStackElement item)
  24. {
  25. _stack.Push(item);
  26. if (_statistics is not null)
  27. {
  28. if (_statistics.ContainsKey(item))
  29. {
  30. return ++_statistics[item];
  31. }
  32. else
  33. {
  34. _statistics.Add(item, 0);
  35. return 0;
  36. }
  37. }
  38. return -1;
  39. }
  40. public CallStackElement Pop()
  41. {
  42. ref readonly var item = ref _stack.Pop();
  43. if (_statistics is not null)
  44. {
  45. if (_statistics[item] == 0)
  46. {
  47. _statistics.Remove(item);
  48. }
  49. else
  50. {
  51. _statistics[item]--;
  52. }
  53. }
  54. return item;
  55. }
  56. public bool TryPeek([NotNullWhen(true)] out CallStackElement item)
  57. {
  58. return _stack.TryPeek(out item);
  59. }
  60. public int Count => _stack._size;
  61. public void Clear()
  62. {
  63. _stack.Clear();
  64. _statistics?.Clear();
  65. }
  66. public override string ToString()
  67. {
  68. return string.Join("->", _stack.Select(cse => cse.ToString()).Reverse());
  69. }
  70. internal string BuildCallStackString(Location location, int excludeTop = 0)
  71. {
  72. static void AppendLocation(
  73. StringBuilder sb,
  74. string shortDescription,
  75. in Location loc,
  76. in CallStackElement? element)
  77. {
  78. sb
  79. .Append(" at");
  80. if (!string.IsNullOrWhiteSpace(shortDescription))
  81. {
  82. sb
  83. .Append(" ")
  84. .Append(shortDescription);
  85. }
  86. if (element?.Arguments is not null)
  87. {
  88. // it's a function
  89. sb.Append(" (");
  90. for (var index = 0; index < element.Value.Arguments.Value.Count; index++)
  91. {
  92. if (index != 0)
  93. {
  94. sb.Append(", ");
  95. }
  96. var arg = element.Value.Arguments.Value[index];
  97. sb.Append(GetPropertyKey(arg));
  98. }
  99. sb.Append(")");
  100. }
  101. sb
  102. .Append(" ")
  103. .Append(loc.Source)
  104. .Append(":")
  105. .Append(loc.End.Line)
  106. .Append(":")
  107. .Append(loc.Start.Column + 1) // report column number instead of index
  108. .AppendLine();
  109. }
  110. using var sb = StringBuilderPool.Rent();
  111. // stack is one frame behind function-wise when we start to process it from expression level
  112. var index = _stack._size - 1 - excludeTop;
  113. var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
  114. var shortDescription = element?.ToString() ?? "";
  115. AppendLocation(sb.Builder, shortDescription, location, element);
  116. location = element?.Location ?? default;
  117. index--;
  118. while (index >= -1)
  119. {
  120. element = index >= 0 ? _stack[index] : null;
  121. shortDescription = element?.ToString() ?? "";
  122. AppendLocation(sb.Builder, shortDescription, location, element);
  123. location = element?.Location ?? default;
  124. index--;
  125. }
  126. return sb.ToString().TrimEnd();
  127. }
  128. /// <summary>
  129. /// A version of <see cref="EsprimaExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  130. /// </summary>
  131. private static string GetPropertyKey(Expression expression)
  132. {
  133. if (expression is Literal literal)
  134. {
  135. return EsprimaExtensions.LiteralKeyToString(literal);
  136. }
  137. if (expression is Identifier identifier)
  138. {
  139. return identifier.Name ?? "";
  140. }
  141. if (expression is StaticMemberExpression staticMemberExpression)
  142. {
  143. return GetPropertyKey(staticMemberExpression.Object) + "." +
  144. GetPropertyKey(staticMemberExpression.Property);
  145. }
  146. return "?";
  147. }
  148. }
  149. }