JintCallStack.cs 6.4 KB

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