DebugHandler.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Jint.Native;
  5. using Jint.Parser.Ast;
  6. using Jint.Runtime.Environments;
  7. using Jint.Runtime.References;
  8. namespace Jint.Runtime.Debugger
  9. {
  10. internal class DebugHandler
  11. {
  12. private readonly Stack<string> _debugCallStack;
  13. private StepMode _stepMode;
  14. private int _callBackStepOverDepth;
  15. private readonly Engine _engine;
  16. public DebugHandler(Engine engine)
  17. {
  18. _engine = engine;
  19. _debugCallStack = new Stack<string>();
  20. _stepMode = StepMode.Into;
  21. }
  22. internal void PopDebugCallStack()
  23. {
  24. if (_debugCallStack.Count > 0)
  25. {
  26. _debugCallStack.Pop();
  27. }
  28. if (_stepMode == StepMode.Over && _debugCallStack.Count <= _callBackStepOverDepth)
  29. {
  30. _callBackStepOverDepth = _debugCallStack.Count;
  31. _stepMode = StepMode.Into;
  32. }
  33. }
  34. internal void AddToDebugCallStack(CallExpression callExpression)
  35. {
  36. var identifier = callExpression.Callee as Identifier;
  37. if (identifier != null)
  38. {
  39. var stack = identifier.Name + "(";
  40. var paramStrings = new List<string>();
  41. foreach (var argument in callExpression.Arguments)
  42. {
  43. if (argument != null)
  44. {
  45. var argIdentifier = argument as Identifier;
  46. paramStrings.Add(argIdentifier != null ? argIdentifier.Name : "null");
  47. }
  48. else
  49. {
  50. paramStrings.Add("null");
  51. }
  52. }
  53. stack += string.Join(", ", paramStrings);
  54. stack += ")";
  55. _debugCallStack.Push(stack);
  56. }
  57. }
  58. internal void OnStep(Statement statement)
  59. {
  60. var old = _stepMode;
  61. if (statement == null)
  62. {
  63. return;
  64. }
  65. BreakPoint breakpoint = _engine.BreakPoints.FirstOrDefault(breakPoint => BpTest(statement, breakPoint));
  66. bool breakpointFound = false;
  67. if (breakpoint != null)
  68. {
  69. DebugInformation info = CreateDebugInformation(statement);
  70. var result = _engine.InvokeBreakEvent(info);
  71. if (result.HasValue)
  72. {
  73. _stepMode = result.Value;
  74. breakpointFound = true;
  75. }
  76. }
  77. if (breakpointFound == false && _stepMode == StepMode.Into)
  78. {
  79. DebugInformation info = CreateDebugInformation(statement);
  80. var result = _engine.InvokeStepEvent(info);
  81. if (result.HasValue)
  82. {
  83. _stepMode = result.Value;
  84. }
  85. }
  86. if (old == StepMode.Into && _stepMode == StepMode.Over)
  87. {
  88. _callBackStepOverDepth = _debugCallStack.Count;
  89. }
  90. }
  91. private bool BpTest(Statement statement, BreakPoint breakpoint)
  92. {
  93. bool afterStart, beforeEnd;
  94. afterStart = (breakpoint.Line == statement.Location.Start.Line &&
  95. breakpoint.Char >= statement.Location.Start.Column);
  96. if (!afterStart)
  97. {
  98. return false;
  99. }
  100. beforeEnd = (breakpoint.Line == statement.Location.End.Line &&
  101. breakpoint.Char <= statement.Location.End.Column);
  102. if (!beforeEnd)
  103. {
  104. return false;
  105. }
  106. if (!string.IsNullOrEmpty(breakpoint.Condition))
  107. {
  108. return _engine.Execute(breakpoint.Condition).GetCompletionValue().AsBoolean();
  109. }
  110. return true;
  111. }
  112. private DebugInformation CreateDebugInformation(Statement statement)
  113. {
  114. var info = new DebugInformation { CurrentStatement = statement, CallStack = _debugCallStack };
  115. if (_engine.ExecutionContext != null && _engine.ExecutionContext.LexicalEnvironment != null)
  116. {
  117. info.Locals = GetIdentifierReference(_engine.ExecutionContext.LexicalEnvironment);
  118. }
  119. return info;
  120. }
  121. public static Dictionary<string, JsValue> GetIdentifierReference(LexicalEnvironment lex)
  122. {
  123. Dictionary<string, JsValue> locals = new Dictionary<string, JsValue>();
  124. LexicalEnvironment tempLex = lex;
  125. while (tempLex != null && tempLex.Record != null)
  126. {
  127. var bindings = tempLex.Record.GetAllBindingNames();
  128. foreach (var binding in bindings)
  129. {
  130. if (locals.ContainsKey(binding) == false)
  131. {
  132. var jsValue = tempLex.Record.GetBindingValue(binding, false);
  133. if (jsValue.TryCast<ICallable>() == null)
  134. {
  135. locals.Add(binding, jsValue);
  136. }
  137. }
  138. }
  139. tempLex = tempLex.Outer;
  140. }
  141. return locals;
  142. }
  143. }
  144. }