JintCallStack.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (!string.IsNullOrWhiteSpace(shortDescription))
  75. {
  76. sb
  77. .Append(" ")
  78. .Append(shortDescription);
  79. }
  80. if (arguments is not null)
  81. {
  82. // it's a function
  83. sb.Append(" (");
  84. for (var index = 0; index < arguments.Value.Count; index++)
  85. {
  86. if (index != 0)
  87. {
  88. sb.Append(", ");
  89. }
  90. var arg = arguments.Value[index];
  91. sb.Append(GetPropertyKey(arg));
  92. }
  93. sb.Append(")");
  94. }
  95. sb
  96. .Append(" ")
  97. .Append(loc.Source)
  98. .Append(":")
  99. .Append(loc.Start.Line)
  100. .Append(":")
  101. .Append(loc.Start.Column + 1) // report column number instead of index
  102. .AppendLine();
  103. }
  104. using var sb = StringBuilderPool.Rent();
  105. // stack is one frame behind function-wise when we start to process it from expression level
  106. var index = _stack._size - 1;
  107. var element = index >= 0 ? _stack[index] : (CallStackElement?) null;
  108. var shortDescription = element?.ToString() ?? "";
  109. AppendLocation(sb.Builder, shortDescription, location, element?.Arguments);
  110. location = element?.Location ?? default;
  111. index--;
  112. while (index >= -1)
  113. {
  114. element = index >= 0 ? _stack[index] : null;
  115. shortDescription = element?.ToString() ?? "";
  116. AppendLocation(sb.Builder, shortDescription, location, element?.Arguments);
  117. location = element?.Location ?? default;
  118. index--;
  119. }
  120. return sb.ToString().TrimEnd();
  121. }
  122. /// <summary>
  123. /// A version of <see cref="EsprimaExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  124. /// </summary>
  125. private static string GetPropertyKey(Expression expression)
  126. {
  127. if (expression is Literal literal)
  128. {
  129. return EsprimaExtensions.LiteralKeyToString(literal);
  130. }
  131. if (expression is Identifier identifier)
  132. {
  133. return identifier.Name ?? "";
  134. }
  135. if (expression is StaticMemberExpression staticMemberExpression)
  136. {
  137. return GetPropertyKey(staticMemberExpression.Object) + "." +
  138. GetPropertyKey(staticMemberExpression.Property);
  139. }
  140. return "?";
  141. }
  142. }
  143. }