CallFrame.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Native;
  4. using Jint.Runtime.CallStack;
  5. using Environment = Jint.Runtime.Environments.Environment;
  6. namespace Jint.Runtime.Debugger
  7. {
  8. public sealed class CallFrame
  9. {
  10. private readonly CallStackExecutionContext _context;
  11. private readonly CallStackElement? _element;
  12. private readonly Lazy<DebugScopes> _scopeChain;
  13. internal CallFrame(
  14. CallStackElement? element,
  15. in CallStackExecutionContext context,
  16. Location location,
  17. JsValue? returnValue)
  18. {
  19. _element = element;
  20. _context = context;
  21. Location = location;
  22. ReturnValue = returnValue;
  23. _scopeChain = new Lazy<DebugScopes>(() => new DebugScopes(Environment));
  24. }
  25. private Environment Environment => _context.LexicalEnvironment;
  26. // TODO: CallFrameId
  27. /// <summary>
  28. /// Name of the function of this call frame. For global scope, this will be "(anonymous)".
  29. /// </summary>
  30. public string FunctionName => _element?.ToString() ?? "(anonymous)";
  31. /// <summary>
  32. /// Source location of function of this call frame.
  33. /// </summary>
  34. /// <remarks>For top level (global) call frames, as well as functions not defined in script, this will be null.</remarks>
  35. public Location? FunctionLocation => (_element?.Function._functionDefinition?.Function as Node)?.Location;
  36. /// <summary>
  37. /// Currently executing source location in this call frame.
  38. /// </summary>
  39. public Location Location { get; }
  40. /// <summary>
  41. /// The scope chain of this call frame.
  42. /// </summary>
  43. public DebugScopes ScopeChain => _scopeChain.Value;
  44. /// <summary>
  45. /// The value of <c>this</c> in this call frame.
  46. /// </summary>
  47. public JsValue This
  48. {
  49. get
  50. {
  51. var environment = _context.GetThisEnvironment();
  52. return environment.GetThisBinding();
  53. }
  54. }
  55. /// <summary>
  56. /// The return value of this call frame. Will be null for call frames that aren't at the top of the stack,
  57. /// as well as if execution is not at a return point.
  58. /// </summary>
  59. public JsValue? ReturnValue { get; }
  60. }
  61. }