EnvironmentRecord.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Diagnostics;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Jint.Native;
  4. namespace Jint.Runtime.Environments
  5. {
  6. /// <summary>
  7. /// Base implementation of an Environment Record
  8. /// https://tc39.es/ecma262/#sec-environment-records
  9. /// </summary>
  10. public abstract class EnvironmentRecord : JsValue
  11. {
  12. protected internal readonly Engine _engine;
  13. protected internal EnvironmentRecord? _outerEnv;
  14. protected EnvironmentRecord(Engine engine) : base(InternalTypes.ObjectEnvironmentRecord)
  15. {
  16. _engine = engine;
  17. }
  18. /// <summary>
  19. /// Determines if an environment record has a binding for an identifier.
  20. /// </summary>
  21. /// <param name="name">The identifier of the binding</param>
  22. /// <returns><c>true</c> if it does and <c>false</c> if it does not.</returns>
  23. public abstract bool HasBinding(string name);
  24. internal abstract bool TryGetBinding(
  25. in BindingName name,
  26. bool strict,
  27. out Binding binding,
  28. [NotNullWhen(true)] out JsValue? value);
  29. /// <summary>
  30. /// Creates a new mutable binding in an environment record.
  31. /// </summary>
  32. /// <param name="name">The identifier of the binding.</param>
  33. /// <param name="canBeDeleted"><c>true</c> if the binding may be subsequently deleted.</param>
  34. public abstract void CreateMutableBinding(string name, bool canBeDeleted = false);
  35. /// <summary>
  36. /// Creates a new but uninitialized immutable binding in an environment record.
  37. /// </summary>
  38. /// <param name="name">The identifier of the binding.</param>
  39. /// <param name="strict"><c>false</c> if the binding may used before it's been initialized.</param>
  40. public abstract void CreateImmutableBinding(string name, bool strict = true);
  41. /// <summary>
  42. /// Set the value of an already existing but uninitialized binding in an Environment Record.
  43. /// </summary>
  44. /// <param name="name">The text of the bound name</param>
  45. /// <param name="value">The value for the binding.</param>
  46. public abstract void InitializeBinding(string name, JsValue value);
  47. /// <summary>
  48. /// Sets the value of an already existing mutable binding in an environment record.
  49. /// </summary>
  50. /// <param name="name">The identifier of the binding</param>
  51. /// <param name="value">The value of the binding.</param>
  52. /// <param name="strict">The identify strict mode references.</param>
  53. public abstract void SetMutableBinding(string name, JsValue value, bool strict);
  54. internal abstract void SetMutableBinding(in BindingName name, JsValue value, bool strict);
  55. /// <summary>
  56. /// Returns the value of an already existing binding from an environment record.
  57. /// </summary>
  58. /// <param name="name">The identifier of the binding</param>
  59. /// <param name="strict">The identify strict mode references.</param>
  60. /// <return>The value of an already existing binding from an environment record.</return>
  61. public abstract JsValue GetBindingValue(string name, bool strict);
  62. /// <summary>
  63. /// Returns the value of an already existing binding from an environment record. Unlike <see cref="GetBindingValue(string, bool)"/>
  64. /// this does not throw an exception for uninitialized bindings, but instead returns false and sets <paramref name="value"/> to null.
  65. /// </summary>
  66. /// <param name="name">The identifier of the binding</param>
  67. /// <param name="strict">Strict mode</param>
  68. /// <param name="value">The value of an already existing binding from an environment record.</param>
  69. /// <returns>True if the value is initialized, otherwise false.</returns>
  70. /// <remarks>This is used for debugger inspection. Note that this will currently still throw if the binding cannot be retrieved (e.g. because it doesn't exist).</remarks>
  71. internal abstract bool TryGetBindingValue(string name, bool strict, [NotNullWhen(true)] out JsValue? value);
  72. /// <summary>
  73. /// Delete a binding from an environment record. The String value N is the text of the bound name If a binding for N exists, remove the binding and return true. If the binding exists but cannot be removed return false. If the binding does not exist return true.
  74. /// </summary>
  75. /// <param name="name">The identifier of the binding</param>
  76. /// <returns><true>true</true> if the deletion is successfull.</returns>
  77. public abstract bool DeleteBinding(string name);
  78. public abstract bool HasThisBinding();
  79. public abstract bool HasSuperBinding();
  80. public abstract JsValue WithBaseObject();
  81. /// <summary>
  82. /// Returns an array of all the defined binding names
  83. /// </summary>
  84. /// <returns>The array of all defined bindings</returns>
  85. internal abstract string[] GetAllBindingNames();
  86. public override object ToObject()
  87. {
  88. ExceptionHelper.ThrowNotSupportedException();
  89. return null;
  90. }
  91. public override bool Equals(JsValue? other)
  92. {
  93. ExceptionHelper.ThrowNotSupportedException();
  94. return false;
  95. }
  96. public abstract JsValue GetThisBinding();
  97. public JsValue? NewTarget { get; protected set; }
  98. /// <summary>
  99. /// Helper to cache JsString/Key when environments use different lookups.
  100. /// </summary>
  101. [DebuggerDisplay("\"{Key.Name}\"")]
  102. internal readonly struct BindingName
  103. {
  104. public readonly Key Key;
  105. public readonly JsString StringValue;
  106. public BindingName(string value)
  107. {
  108. Key = (Key) value;
  109. StringValue = JsString.Create(value);
  110. }
  111. }
  112. }
  113. }