JintCallStack.cs 4.9 KB

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