JintCallStack.cs 4.9 KB

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