JintCallStack.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Text;
  5. using Jint.Collections;
  6. using Jint.Native.Function;
  7. using Jint.Runtime.Environments;
  8. using Jint.Runtime.Interpreter.Expressions;
  9. using Environment = Jint.Runtime.Environments.Environment;
  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 Environment LexicalEnvironment;
  20. internal Environment GetThisEnvironment()
  21. {
  22. var lex = LexicalEnvironment;
  23. while (true)
  24. {
  25. if (lex is not 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(Function function, JintExpression? expression, in ExecutionContext executionContext)
  50. {
  51. var item = new CallStackElement(function, expression, new CallStackExecutionContext(executionContext));
  52. _stack.Push(item);
  53. if (_statistics is not null)
  54. {
  55. #pragma warning disable CA1854
  56. #pragma warning disable CA1864
  57. if (_statistics.ContainsKey(item))
  58. #pragma warning restore CA1854
  59. #pragma warning restore CA1864
  60. {
  61. return ++_statistics[item];
  62. }
  63. else
  64. {
  65. _statistics.Add(item, 0);
  66. return 0;
  67. }
  68. }
  69. return -1;
  70. }
  71. public CallStackElement Pop()
  72. {
  73. ref readonly var item = ref _stack.Pop();
  74. if (_statistics is not null)
  75. {
  76. if (_statistics[item] == 0)
  77. {
  78. _statistics.Remove(item);
  79. }
  80. else
  81. {
  82. _statistics[item]--;
  83. }
  84. }
  85. return item;
  86. }
  87. public bool TryPeek([NotNullWhen(true)] out CallStackElement item)
  88. {
  89. return _stack.TryPeek(out item);
  90. }
  91. public int Count => _stack._size;
  92. public void Clear()
  93. {
  94. _stack.Clear();
  95. _statistics?.Clear();
  96. }
  97. public override string ToString()
  98. {
  99. return string.Join("->", _stack.Select(static cse => cse.ToString()).Reverse());
  100. }
  101. internal string BuildCallStackString(SourceLocation location, int excludeTop = 0)
  102. {
  103. static void AppendLocation(
  104. ref ValueStringBuilder sb,
  105. string shortDescription,
  106. in SourceLocation loc,
  107. in CallStackElement? element)
  108. {
  109. sb.Append(" at");
  110. if (!string.IsNullOrWhiteSpace(shortDescription))
  111. {
  112. sb.Append(' ');
  113. sb.Append(shortDescription);
  114. }
  115. if (element?.Arguments is not null)
  116. {
  117. // it's a function
  118. sb.Append(" (");
  119. for (var index = 0; index < element.Value.Arguments.Value.Count; index++)
  120. {
  121. if (index != 0)
  122. {
  123. sb.Append(", ");
  124. }
  125. var arg = element.Value.Arguments.Value[index];
  126. sb.Append(GetPropertyKey(arg));
  127. }
  128. sb.Append(')');
  129. }
  130. sb.Append(' ');
  131. sb.Append(loc.Source);
  132. sb.Append(':');
  133. sb.Append(loc.End.Line.ToString(CultureInfo.InvariantCulture));
  134. sb.Append(':');
  135. sb.Append((loc.Start.Column + 1).ToString(CultureInfo.InvariantCulture)); // report column number instead of index
  136. sb.Append(System.Environment.NewLine);
  137. }
  138. var builder = new ValueStringBuilder();
  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(ref 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(ref builder, shortDescription, location, element);
  151. location = element?.Location ?? default;
  152. index--;
  153. }
  154. var result = builder.AsSpan().TrimEnd().ToString();
  155. builder.Dispose();
  156. return result;
  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. }