2
0

JintCallStack.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Esprima;
  6. using Esprima.Ast;
  7. using Jint.Collections;
  8. using Jint.Pooling;
  9. namespace Jint.Runtime.CallStack
  10. {
  11. internal sealed class JintCallStack
  12. {
  13. private readonly RefStack<CallStackElement> _stack = new();
  14. private readonly Dictionary<CallStackElement, int>? _statistics;
  15. // Internal for use by DebugHandler
  16. internal RefStack<CallStackElement> Stack => _stack;
  17. public JintCallStack(bool trackRecursionDepth)
  18. {
  19. if (trackRecursionDepth)
  20. {
  21. _statistics = new Dictionary<CallStackElement, int>(CallStackElementComparer.Instance);
  22. }
  23. }
  24. public int Push(in CallStackElement item)
  25. {
  26. _stack.Push(item);
  27. if (_statistics is not null)
  28. {
  29. if (_statistics.ContainsKey(item))
  30. {
  31. return ++_statistics[item];
  32. }
  33. else
  34. {
  35. _statistics.Add(item, 0);
  36. return 0;
  37. }
  38. }
  39. return -1;
  40. }
  41. public CallStackElement Pop()
  42. {
  43. ref readonly var item = ref _stack.Pop();
  44. if (_statistics is not null)
  45. {
  46. if (_statistics[item] == 0)
  47. {
  48. _statistics.Remove(item);
  49. }
  50. else
  51. {
  52. _statistics[item]--;
  53. }
  54. }
  55. return item;
  56. }
  57. public int Count => _stack._size;
  58. public void Clear()
  59. {
  60. _stack.Clear();
  61. _statistics?.Clear();
  62. }
  63. public override string ToString()
  64. {
  65. return string.Join("->", _stack.Select(cse => cse.ToString()).Reverse());
  66. }
  67. internal string BuildCallStackString(Location location)
  68. {
  69. static void AppendLocation(
  70. StringBuilder sb,
  71. string shortDescription,
  72. in Location loc,
  73. in CallStackElement? element)
  74. {
  75. sb
  76. .Append(" at");
  77. if (!string.IsNullOrWhiteSpace(shortDescription))
  78. {
  79. sb
  80. .Append(" ")
  81. .Append(shortDescription);
  82. }
  83. if (element?.Arguments is not null)
  84. {
  85. // it's a function
  86. sb.Append(" (");
  87. for (var index = 0; index < element.Value.Arguments.Value.Count; index++)
  88. {
  89. if (index != 0)
  90. {
  91. sb.Append(", ");
  92. }
  93. var arg = element.Value.Arguments.Value[index];
  94. sb.Append(GetPropertyKey(arg));
  95. }
  96. sb.Append(")");
  97. }
  98. sb
  99. .Append(" ")
  100. .Append(loc.Source)
  101. .Append(":")
  102. .Append(loc.End.Line)
  103. .Append(":")
  104. .Append(loc.Start.Column + 1) // report column number instead of index
  105. .AppendLine();
  106. }
  107. using var sb = StringBuilderPool.Rent();
  108. // stack is one frame behind function-wise when we start to process it from expression level
  109. var index = _stack._size - 1;
  110. var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
  111. var shortDescription = element?.ToString() ?? "";
  112. AppendLocation(sb.Builder, shortDescription, location, element);
  113. location = element?.Location ?? default;
  114. index--;
  115. while (index >= -1)
  116. {
  117. element = index >= 0 ? _stack[index] : null;
  118. shortDescription = element?.ToString() ?? "";
  119. AppendLocation(sb.Builder, shortDescription, location, element);
  120. location = element?.Location ?? default;
  121. index--;
  122. }
  123. return sb.ToString().TrimEnd();
  124. }
  125. /// <summary>
  126. /// A version of <see cref="EsprimaExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  127. /// </summary>
  128. private static string GetPropertyKey(Expression expression)
  129. {
  130. if (expression is Literal literal)
  131. {
  132. return EsprimaExtensions.LiteralKeyToString(literal);
  133. }
  134. if (expression is Identifier identifier)
  135. {
  136. return identifier.Name ?? "";
  137. }
  138. if (expression is StaticMemberExpression staticMemberExpression)
  139. {
  140. return GetPropertyKey(staticMemberExpression.Object) + "." +
  141. GetPropertyKey(staticMemberExpression.Property);
  142. }
  143. return "?";
  144. }
  145. }
  146. }