JintCallStack.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #pragma warning disable CA1854
  57. if (_statistics.ContainsKey(item))
  58. #pragma warning restore CA1854
  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(Location location, int excludeTop = 0)
  101. {
  102. static void AppendLocation(
  103. StringBuilder sb,
  104. string shortDescription,
  105. in Location loc,
  106. in CallStackElement? element)
  107. {
  108. sb
  109. .Append(" at");
  110. if (!string.IsNullOrWhiteSpace(shortDescription))
  111. {
  112. sb
  113. .Append(' ')
  114. .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
  132. .Append(' ')
  133. .Append(loc.Source)
  134. .Append(':')
  135. .Append(loc.End.Line)
  136. .Append(':')
  137. .Append(loc.Start.Column + 1) // report column number instead of index
  138. .AppendLine();
  139. }
  140. using var sb = StringBuilderPool.Rent();
  141. // stack is one frame behind function-wise when we start to process it from expression level
  142. var index = _stack._size - 1 - excludeTop;
  143. var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
  144. var shortDescription = element?.ToString() ?? "";
  145. AppendLocation(sb.Builder, shortDescription, location, element);
  146. location = element?.Location ?? default;
  147. index--;
  148. while (index >= -1)
  149. {
  150. element = index >= 0 ? _stack[index] : null;
  151. shortDescription = element?.ToString() ?? "";
  152. AppendLocation(sb.Builder, shortDescription, location, element);
  153. location = element?.Location ?? default;
  154. index--;
  155. }
  156. return sb.ToString().TrimEnd();
  157. }
  158. /// <summary>
  159. /// A version of <see cref="EsprimaExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  160. /// </summary>
  161. private static string GetPropertyKey(Node expression)
  162. {
  163. if (expression is Literal literal)
  164. {
  165. return EsprimaExtensions.LiteralKeyToString(literal);
  166. }
  167. if (expression is Identifier identifier)
  168. {
  169. return identifier.Name ?? "";
  170. }
  171. if (expression is StaticMemberExpression staticMemberExpression)
  172. {
  173. return GetPropertyKey(staticMemberExpression.Object) + "." +
  174. GetPropertyKey(staticMemberExpression.Property);
  175. }
  176. return "?";
  177. }
  178. }
  179. }