JintCallStack.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using System.Text;
  4. using Esprima;
  5. using Esprima.Ast;
  6. using Jint.Collections;
  7. using Jint.Native.Function;
  8. using Jint.Pooling;
  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. if (_statistics.ContainsKey(item))
  57. {
  58. return ++_statistics[item];
  59. }
  60. else
  61. {
  62. _statistics.Add(item, 0);
  63. return 0;
  64. }
  65. }
  66. return -1;
  67. }
  68. public CallStackElement Pop()
  69. {
  70. ref readonly var item = ref _stack.Pop();
  71. if (_statistics is not null)
  72. {
  73. if (_statistics[item] == 0)
  74. {
  75. _statistics.Remove(item);
  76. }
  77. else
  78. {
  79. _statistics[item]--;
  80. }
  81. }
  82. return item;
  83. }
  84. public bool TryPeek([NotNullWhen(true)] out CallStackElement item)
  85. {
  86. return _stack.TryPeek(out item);
  87. }
  88. public int Count => _stack._size;
  89. public void Clear()
  90. {
  91. _stack.Clear();
  92. _statistics?.Clear();
  93. }
  94. public override string ToString()
  95. {
  96. return string.Join("->", _stack.Select(static cse => cse.ToString()).Reverse());
  97. }
  98. internal string BuildCallStackString(Location location, int excludeTop = 0)
  99. {
  100. static void AppendLocation(
  101. StringBuilder sb,
  102. string shortDescription,
  103. in Location loc,
  104. in CallStackElement? element)
  105. {
  106. sb
  107. .Append(" at");
  108. if (!string.IsNullOrWhiteSpace(shortDescription))
  109. {
  110. sb
  111. .Append(" ")
  112. .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
  130. .Append(" ")
  131. .Append(loc.Source)
  132. .Append(":")
  133. .Append(loc.End.Line)
  134. .Append(":")
  135. .Append(loc.Start.Column + 1) // report column number instead of index
  136. .AppendLine();
  137. }
  138. using var sb = StringBuilderPool.Rent();
  139. // stack is one frame behind function-wise when we start to process it from expression level
  140. var index = _stack._size - 1 - excludeTop;
  141. var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
  142. var shortDescription = element?.ToString() ?? "";
  143. AppendLocation(sb.Builder, shortDescription, location, element);
  144. location = element?.Location ?? default;
  145. index--;
  146. while (index >= -1)
  147. {
  148. element = index >= 0 ? _stack[index] : null;
  149. shortDescription = element?.ToString() ?? "";
  150. AppendLocation(sb.Builder, shortDescription, location, element);
  151. location = element?.Location ?? default;
  152. index--;
  153. }
  154. return sb.ToString().TrimEnd();
  155. }
  156. /// <summary>
  157. /// A version of <see cref="EsprimaExtensions.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 EsprimaExtensions.LiteralKeyToString(literal);
  164. }
  165. if (expression is Identifier identifier)
  166. {
  167. return identifier.Name ?? "";
  168. }
  169. if (expression is StaticMemberExpression staticMemberExpression)
  170. {
  171. return GetPropertyKey(staticMemberExpression.Object) + "." +
  172. GetPropertyKey(staticMemberExpression.Property);
  173. }
  174. return "?";
  175. }
  176. }
  177. }