DebugHandler.cs 6.7 KB

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