CallFrame.cs 2.2 KB

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