BreakPointCollection.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. namespace Jint.Runtime.Debugger;
  3. /// <summary>
  4. /// Collection of breakpoints.
  5. /// </summary>
  6. /// <remarks>
  7. /// Only allows a single breakpoint at the same location (source, column and line).
  8. /// Adding a new breakpoint at the same location <i>replaces</i> the old one - this allows replacing e.g. a
  9. /// conditional breakpoint with a new condition (or remove the condition).
  10. /// </remarks>
  11. public sealed class BreakPointCollection : IEnumerable<BreakPoint>
  12. {
  13. private readonly Dictionary<BreakLocation, BreakPoint> _breakPoints = new(new OptionalSourceBreakLocationEqualityComparer());
  14. public BreakPointCollection()
  15. {
  16. }
  17. /// <summary>
  18. /// Gets or sets whether breakpoints are activated. When false, all breakpoints will fail to match (and be skipped by the debugger).
  19. /// </summary>
  20. public bool Active { get; set; } = true;
  21. public int Count => _breakPoints.Count;
  22. /// <summary>
  23. /// Sets a new breakpoint. Note that this will replace any breakpoint at the same location (source/column/line).
  24. /// </summary>
  25. public void Set(BreakPoint breakPoint)
  26. {
  27. _breakPoints[breakPoint.Location] = breakPoint;
  28. }
  29. /// <summary>
  30. /// Removes breakpoint with the given location (source/column/line).
  31. /// Note that a null source matches <i>any</i> source.
  32. /// </summary>
  33. public bool RemoveAt(BreakLocation location)
  34. {
  35. return _breakPoints.Remove(location);
  36. }
  37. /// <summary>
  38. /// Checks whether collection contains a breakpoint at the given location (source/column/line).
  39. /// Note that a null source matches <i>any</i> source.
  40. /// </summary>
  41. public bool Contains(BreakLocation location)
  42. {
  43. return _breakPoints.ContainsKey(location);
  44. }
  45. /// <summary>
  46. /// Removes all breakpoints.
  47. /// </summary>
  48. public void Clear()
  49. {
  50. _breakPoints.Clear();
  51. }
  52. internal BreakPoint? FindMatch(DebugHandler debugger, BreakLocation location)
  53. {
  54. if (!Active)
  55. {
  56. return null;
  57. }
  58. if (!_breakPoints.TryGetValue(location, out var breakPoint))
  59. {
  60. return null;
  61. }
  62. if (!string.IsNullOrEmpty(breakPoint.Condition))
  63. {
  64. try
  65. {
  66. var completionValue = debugger.Evaluate(breakPoint.Condition!);
  67. // Truthiness check:
  68. if (!TypeConverter.ToBoolean(completionValue))
  69. {
  70. return null;
  71. }
  72. }
  73. catch (Exception ex) when (ex is JavaScriptException || ex is DebugEvaluationException)
  74. {
  75. // Error in the condition means it doesn't match - shouldn't actually throw.
  76. return null;
  77. }
  78. }
  79. return breakPoint;
  80. }
  81. public IEnumerator<BreakPoint> GetEnumerator()
  82. {
  83. return _breakPoints.Values.GetEnumerator();
  84. }
  85. IEnumerator IEnumerable.GetEnumerator()
  86. {
  87. return _breakPoints.GetEnumerator();
  88. }
  89. }