DebugHandler.cs 5.4 KB

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