DebugHandler.cs 6.6 KB

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