JintCallStack.cs 5.7 KB

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