DebugScope.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections.Generic;
  2. using Jint.Native;
  3. using Jint.Native.Object;
  4. using Jint.Runtime.Environments;
  5. namespace Jint.Runtime.Debugger
  6. {
  7. /// <summary>
  8. /// Scope information, bindings, and values for a single scope in the scope chain
  9. /// </summary>
  10. public sealed class DebugScope
  11. {
  12. private readonly EnvironmentRecord _record;
  13. private readonly List<string> _bindingNames;
  14. internal DebugScope(DebugScopeType type, EnvironmentRecord record, List<string> bindingNames, bool isTopLevel)
  15. {
  16. ScopeType = type;
  17. _record = record;
  18. _bindingNames = bindingNames;
  19. BindingObject = record is ObjectEnvironmentRecord objEnv ? objEnv._bindingObject : null;
  20. IsTopLevel = isTopLevel;
  21. }
  22. /// <summary>
  23. /// The type of scope. Scope types are the same as defined by Chrome devtools protocol.
  24. /// </summary>
  25. public DebugScopeType ScopeType { get; }
  26. /// <summary>
  27. /// For <see cref="DebugScopeType.Block">block</see> scopes, indicates whether this scope is at the top level of a containing function.
  28. /// </summary>
  29. /// <remarks>
  30. /// Block scopes at the top level of a function are combined with Local scope in Chromium.
  31. /// This property facilitates implementing the same "flattening" in e.g. a UI. Because empty scopes are excluded in the scope chain,
  32. /// top level cannot be determined from the scope chain order alone.
  33. /// </remarks>
  34. public bool IsTopLevel { get; }
  35. /// <summary>
  36. /// Names of all non-shadowed bindings in the scope.
  37. /// </summary>
  38. public IReadOnlyList<string> BindingNames => _bindingNames;
  39. /// <summary>
  40. /// Binding object for ObjectEnvironmentRecords - that is, Global scope and With scope. Null for other scopes.
  41. /// </summary>
  42. /// <remarks>
  43. /// This is mainly useful as an optimization for devtools, allowing the BindingObject to be serialized directly rather than
  44. /// building a new transient object in response to e.g. Runtime.getProperties.
  45. /// </remarks>
  46. public ObjectInstance BindingObject { get; }
  47. /// <summary>
  48. /// Retrieves the value of a specific binding. Note that some bindings (e.g. uninitialized let/const) may return null.
  49. /// </summary>
  50. /// <param name="name">Binding name</param>
  51. /// <returns>Value of the binding</returns>
  52. public JsValue GetBindingValue(string name)
  53. {
  54. _record.TryGetBindingValue(name, strict: true, out var result);
  55. return result;
  56. }
  57. /// <summary>
  58. /// Sets the value of an existing binding.
  59. /// </summary>
  60. /// <param name="name">Binding name</param>
  61. /// <param name="value">New value of the binding</param>
  62. public void SetBindingValue(string name, JsValue value)
  63. {
  64. _record.SetMutableBinding(name, value, strict: true);
  65. }
  66. }
  67. }