JintCallStack.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Text;
  3. using Esprima;
  4. using Esprima.Ast;
  5. using Jint.Collections;
  6. using Jint.Native.Function;
  7. using Jint.Pooling;
  8. using Jint.Runtime.Environments;
  9. using Jint.Runtime.Interpreter.Expressions;
  10. namespace Jint.Runtime.CallStack
  11. {
  12. // smaller version with only required info
  13. internal readonly record struct CallStackExecutionContext
  14. {
  15. public CallStackExecutionContext(in ExecutionContext context)
  16. {
  17. LexicalEnvironment = context.LexicalEnvironment;
  18. }
  19. internal readonly EnvironmentRecord LexicalEnvironment;
  20. internal EnvironmentRecord GetThisEnvironment()
  21. {
  22. var lex = LexicalEnvironment;
  23. while (true)
  24. {
  25. if (lex != null)
  26. {
  27. if (lex.HasThisBinding())
  28. {
  29. return lex;
  30. }
  31. lex = lex._outerEnv;
  32. }
  33. }
  34. }
  35. }
  36. internal sealed class JintCallStack
  37. {
  38. private readonly RefStack<CallStackElement> _stack = new();
  39. private readonly Dictionary<CallStackElement, int>? _statistics;
  40. // Internal for use by DebugHandler
  41. internal RefStack<CallStackElement> Stack => _stack;
  42. public JintCallStack(bool trackRecursionDepth)
  43. {
  44. if (trackRecursionDepth)
  45. {
  46. _statistics = new Dictionary<CallStackElement, int>(CallStackElementComparer.Instance);
  47. }
  48. }
  49. public int Push(FunctionInstance functionInstance, JintExpression? expression, in ExecutionContext executionContext)
  50. {
  51. var item = new CallStackElement(functionInstance, expression, new CallStackExecutionContext(executionContext));
  52. _stack.Push(item);
  53. if (_statistics is not null)
  54. {
  55. if (_statistics.ContainsKey(item))
  56. {
  57. return ++_statistics[item];
  58. }
  59. else
  60. {
  61. _statistics.Add(item, 0);
  62. return 0;
  63. }
  64. }
  65. return -1;
  66. }
  67. public CallStackElement Pop()
  68. {
  69. ref readonly var item = ref _stack.Pop();
  70. if (_statistics is not null)
  71. {
  72. if (_statistics[item] == 0)
  73. {
  74. _statistics.Remove(item);
  75. }
  76. else
  77. {
  78. _statistics[item]--;
  79. }
  80. }
  81. return item;
  82. }
  83. public bool TryPeek([NotNullWhen(true)] out CallStackElement item)
  84. {
  85. return _stack.TryPeek(out item);
  86. }
  87. public int Count => _stack._size;
  88. public void Clear()
  89. {
  90. _stack.Clear();
  91. _statistics?.Clear();
  92. }
  93. public override string ToString()
  94. {
  95. return string.Join("->", _stack.Select(cse => cse.ToString()).Reverse());
  96. }
  97. internal string BuildCallStackString(Location location, int excludeTop = 0)
  98. {
  99. static void AppendLocation(
  100. StringBuilder sb,
  101. string shortDescription,
  102. in Location loc,
  103. in CallStackElement? element)
  104. {
  105. sb
  106. .Append(" at");
  107. if (!string.IsNullOrWhiteSpace(shortDescription))
  108. {
  109. sb
  110. .Append(" ")
  111. .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
  129. .Append(" ")
  130. .Append(loc.Source)
  131. .Append(":")
  132. .Append(loc.End.Line)
  133. .Append(":")
  134. .Append(loc.Start.Column + 1) // report column number instead of index
  135. .AppendLine();
  136. }
  137. using var sb = StringBuilderPool.Rent();
  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(sb.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(sb.Builder, shortDescription, location, element);
  150. location = element?.Location ?? default;
  151. index--;
  152. }
  153. return sb.ToString().TrimEnd();
  154. }
  155. /// <summary>
  156. /// A version of <see cref="EsprimaExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  157. /// </summary>
  158. private static string GetPropertyKey(Node expression)
  159. {
  160. if (expression is Literal literal)
  161. {
  162. return EsprimaExtensions.LiteralKeyToString(literal);
  163. }
  164. if (expression is Identifier identifier)
  165. {
  166. return identifier.Name ?? "";
  167. }
  168. if (expression is StaticMemberExpression staticMemberExpression)
  169. {
  170. return GetPropertyKey(staticMemberExpression.Object) + "." +
  171. GetPropertyKey(staticMemberExpression.Property);
  172. }
  173. return "?";
  174. }
  175. }
  176. }