JintCallStack.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Text;
  5. using Jint.Collections;
  6. using Jint.Native.Function;
  7. using Jint.Runtime.Environments;
  8. using Jint.Runtime.Interpreter.Expressions;
  9. using Environment = Jint.Runtime.Environments.Environment;
  10. namespace Jint.Runtime.CallStack;
  11. // smaller version with only required info
  12. internal readonly record struct CallStackExecutionContext
  13. {
  14. public CallStackExecutionContext(in ExecutionContext context)
  15. {
  16. LexicalEnvironment = context.LexicalEnvironment;
  17. }
  18. internal readonly Environment LexicalEnvironment;
  19. internal Environment GetThisEnvironment()
  20. {
  21. var lex = LexicalEnvironment;
  22. while (true)
  23. {
  24. if (lex is not null)
  25. {
  26. if (lex.HasThisBinding())
  27. {
  28. return lex;
  29. }
  30. lex = lex._outerEnv;
  31. }
  32. }
  33. }
  34. }
  35. internal sealed class JintCallStack
  36. {
  37. private readonly RefStack<CallStackElement> _stack = new();
  38. private readonly Dictionary<CallStackElement, int>? _statistics;
  39. // Internal for use by DebugHandler
  40. internal RefStack<CallStackElement> Stack => _stack;
  41. public JintCallStack(bool trackRecursionDepth)
  42. {
  43. if (trackRecursionDepth)
  44. {
  45. _statistics = new Dictionary<CallStackElement, int>(CallStackElementComparer.Instance);
  46. }
  47. }
  48. public int Push(Function function, JintExpression? expression, in ExecutionContext executionContext)
  49. {
  50. var item = new CallStackElement(function, expression, new CallStackExecutionContext(executionContext));
  51. _stack.Push(item);
  52. if (_statistics is not null)
  53. {
  54. #pragma warning disable CA1854
  55. #pragma warning disable CA1864
  56. if (_statistics.ContainsKey(item))
  57. #pragma warning restore CA1854
  58. #pragma warning restore CA1864
  59. {
  60. return ++_statistics[item];
  61. }
  62. else
  63. {
  64. _statistics.Add(item, 0);
  65. return 0;
  66. }
  67. }
  68. return -1;
  69. }
  70. public CallStackElement Pop()
  71. {
  72. ref readonly var item = ref _stack.Pop();
  73. if (_statistics is not null)
  74. {
  75. if (_statistics[item] == 0)
  76. {
  77. _statistics.Remove(item);
  78. }
  79. else
  80. {
  81. _statistics[item]--;
  82. }
  83. }
  84. return item;
  85. }
  86. public bool TryPeek([NotNullWhen(true)] out CallStackElement item)
  87. {
  88. return _stack.TryPeek(out item);
  89. }
  90. public int Count => _stack._size;
  91. public void Clear()
  92. {
  93. _stack.Clear();
  94. _statistics?.Clear();
  95. }
  96. public override string ToString()
  97. {
  98. return string.Join("->", _stack.Select(static cse => cse.ToString()).Reverse());
  99. }
  100. internal string BuildCallStackString(SourceLocation location, int excludeTop = 0)
  101. {
  102. static void AppendLocation(
  103. ref ValueStringBuilder sb,
  104. string shortDescription,
  105. in SourceLocation loc,
  106. in CallStackElement? element)
  107. {
  108. sb.Append(" at");
  109. if (!string.IsNullOrWhiteSpace(shortDescription))
  110. {
  111. sb.Append(' ');
  112. sb.Append(shortDescription);
  113. }
  114. if (element?.Arguments is not null)
  115. {
  116. // it's a function
  117. sb.Append(" (");
  118. for (var index = 0; index < element.Value.Arguments.Value.Count; index++)
  119. {
  120. if (index != 0)
  121. {
  122. sb.Append(", ");
  123. }
  124. var arg = element.Value.Arguments.Value[index];
  125. sb.Append(GetPropertyKey(arg));
  126. }
  127. sb.Append(')');
  128. }
  129. sb.Append(' ');
  130. sb.Append(loc.SourceFile);
  131. sb.Append(':');
  132. sb.Append(loc.End.Line.ToString(CultureInfo.InvariantCulture));
  133. sb.Append(':');
  134. sb.Append((loc.Start.Column + 1).ToString(CultureInfo.InvariantCulture)); // report column number instead of index
  135. sb.Append(System.Environment.NewLine);
  136. }
  137. var builder = new ValueStringBuilder();
  138. // stack is one frame behind function-wise when we start to process it from expression level
  139. var index = _stack._size - 1 - excludeTop;
  140. var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
  141. var shortDescription = element?.ToString() ?? "";
  142. AppendLocation(ref builder, shortDescription, location, element);
  143. location = element?.Location ?? default;
  144. index--;
  145. while (index >= -1)
  146. {
  147. element = index >= 0 ? _stack[index] : null;
  148. shortDescription = element?.ToString() ?? "";
  149. AppendLocation(ref builder, shortDescription, location, element);
  150. location = element?.Location ?? default;
  151. index--;
  152. }
  153. var result = builder.AsSpan().TrimEnd().ToString();
  154. builder.Dispose();
  155. return result;
  156. }
  157. /// <summary>
  158. /// A version of <see cref="AstExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  159. /// </summary>
  160. private static string GetPropertyKey(Node expression)
  161. {
  162. if (expression is Literal literal)
  163. {
  164. return AstExtensions.LiteralKeyToString(literal);
  165. }
  166. if (expression is Identifier identifier)
  167. {
  168. return identifier.Name ?? "";
  169. }
  170. if (expression is MemberExpression { Computed: false } staticMemberExpression)
  171. {
  172. return GetPropertyKey(staticMemberExpression.Object) + "." +
  173. GetPropertyKey(staticMemberExpression.Property);
  174. }
  175. return "?";
  176. }
  177. }