DebugHandler.cs 6.7 KB

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