2
0

DebugHandler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (statement is ExpressionStatement expressionStatement
  94. && 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
  130. {
  131. CurrentStatement = statement,
  132. CallStack = _debugCallStack,
  133. CurrentMemoryUsage = _engine.CurrentMemoryUsage
  134. };
  135. if (_engine.ExecutionContext.VariableEnvironment != null)
  136. {
  137. var lexicalEnvironment = _engine.ExecutionContext.VariableEnvironment;
  138. info.Locals = GetLocalVariables(lexicalEnvironment);
  139. info.Globals = GetGlobalVariables(lexicalEnvironment);
  140. }
  141. return info;
  142. }
  143. private static Dictionary<string, JsValue> GetLocalVariables(LexicalEnvironment lex)
  144. {
  145. Dictionary<string, JsValue> locals = new Dictionary<string, JsValue>();
  146. if (!ReferenceEquals(lex?._record, null))
  147. {
  148. AddRecordsFromEnvironment(lex, locals);
  149. }
  150. return locals;
  151. }
  152. private static Dictionary<string, JsValue> GetGlobalVariables(LexicalEnvironment lex)
  153. {
  154. Dictionary<string, JsValue> globals = new Dictionary<string, JsValue>();
  155. LexicalEnvironment tempLex = lex;
  156. while (!ReferenceEquals(tempLex?._record, null))
  157. {
  158. AddRecordsFromEnvironment(tempLex, globals);
  159. tempLex = tempLex._outer;
  160. }
  161. return globals;
  162. }
  163. private static void AddRecordsFromEnvironment(LexicalEnvironment lex, Dictionary<string, JsValue> locals)
  164. {
  165. var bindings = lex._record.GetAllBindingNames();
  166. foreach (var binding in bindings)
  167. {
  168. if (locals.ContainsKey(binding) == false)
  169. {
  170. var jsValue = lex._record.GetBindingValue(binding, false);
  171. if (jsValue.TryCast<ICallable>() == null)
  172. {
  173. locals.Add(binding, jsValue);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }