2
0

DebugHandler.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Identifier;
  40. if (identifier != null)
  41. {
  42. var stack = identifier.Name + "(";
  43. var paramStrings = new List<string>();
  44. foreach (var argument in callExpression.Arguments)
  45. {
  46. if (argument != null)
  47. {
  48. var argIdentifier = argument as 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. return _engine.Execute(breakpoint.Condition).GetCompletionValue().AsBoolean();
  125. }
  126. return true;
  127. }
  128. private DebugInformation CreateDebugInformation(Statement statement)
  129. {
  130. var info = new DebugInformation { CurrentStatement = statement, CallStack = _debugCallStack };
  131. if (_engine.ExecutionContext != null && _engine.ExecutionContext.LexicalEnvironment != null)
  132. {
  133. var lexicalEnvironment = _engine.ExecutionContext.LexicalEnvironment;
  134. info.Locals = GetLocalVariables(lexicalEnvironment);
  135. info.Globals = GetGlobalVariables(lexicalEnvironment);
  136. }
  137. return info;
  138. }
  139. private static Dictionary<string, JsValue> GetLocalVariables(LexicalEnvironment lex)
  140. {
  141. Dictionary<string, JsValue> locals = new Dictionary<string, JsValue>();
  142. if (lex != null && lex.Record != null)
  143. {
  144. AddRecordsFromEnvironment(lex, locals);
  145. }
  146. return locals;
  147. }
  148. private static Dictionary<string, JsValue> GetGlobalVariables(LexicalEnvironment lex)
  149. {
  150. Dictionary<string, JsValue> globals = new Dictionary<string, JsValue>();
  151. LexicalEnvironment tempLex = lex;
  152. while (tempLex != null && tempLex.Record != null)
  153. {
  154. AddRecordsFromEnvironment(tempLex, globals);
  155. tempLex = tempLex.Outer;
  156. }
  157. return globals;
  158. }
  159. private static void AddRecordsFromEnvironment(LexicalEnvironment lex, Dictionary<string, JsValue> locals)
  160. {
  161. var bindings = lex.Record.GetAllBindingNames();
  162. foreach (var binding in bindings)
  163. {
  164. if (locals.ContainsKey(binding) == false)
  165. {
  166. var jsValue = lex.Record.GetBindingValue(binding, false);
  167. if (jsValue.TryCast<ICallable>() == null)
  168. {
  169. locals.Add(binding, jsValue);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }