EnvironmentRecord.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 HasBinding(in BindingName name);
  25. internal abstract bool TryGetBinding(
  26. in BindingName name,
  27. bool strict,
  28. out Binding binding,
  29. [NotNullWhen(true)] out JsValue? value);
  30. /// <summary>
  31. /// Creates a new mutable binding in an environment record.
  32. /// </summary>
  33. /// <param name="name">The identifier of the binding.</param>
  34. /// <param name="canBeDeleted"><c>true</c> if the binding may be subsequently deleted.</param>
  35. public abstract void CreateMutableBinding(string name, bool canBeDeleted = false);
  36. /// <summary>
  37. /// Creates a new but uninitialized immutable binding in an environment record.
  38. /// </summary>
  39. /// <param name="name">The identifier of the binding.</param>
  40. /// <param name="strict"><c>false</c> if the binding may used before it's been initialized.</param>
  41. public abstract void CreateImmutableBinding(string name, bool strict = true);
  42. /// <summary>
  43. /// Set the value of an already existing but uninitialized binding in an Environment Record.
  44. /// </summary>
  45. /// <param name="name">The text of the bound name</param>
  46. /// <param name="value">The value for the binding.</param>
  47. public abstract void InitializeBinding(string name, JsValue value);
  48. /// <summary>
  49. /// Sets the value of an already existing mutable binding in an environment record.
  50. /// </summary>
  51. /// <param name="name">The identifier of the binding</param>
  52. /// <param name="value">The value of the binding.</param>
  53. /// <param name="strict">The identify strict mode references.</param>
  54. public abstract void SetMutableBinding(string name, JsValue value, bool strict);
  55. internal abstract void SetMutableBinding(in BindingName name, JsValue value, bool strict);
  56. /// <summary>
  57. /// Returns the value of an already existing binding from an environment record.
  58. /// </summary>
  59. /// <param name="name">The identifier of the binding</param>
  60. /// <param name="strict">The identify strict mode references.</param>
  61. /// <return>The value of an already existing binding from an environment record.</return>
  62. public abstract JsValue GetBindingValue(string name, bool strict);
  63. /// <summary>
  64. /// Returns the value of an already existing binding from an environment record. Unlike <see cref="GetBindingValue(string, bool)"/>
  65. /// this does not throw an exception for uninitialized bindings, but instead returns false and sets <paramref name="value"/> to null.
  66. /// </summary>
  67. /// <param name="name">The identifier of the binding</param>
  68. /// <param name="strict">Strict mode</param>
  69. /// <param name="value">The value of an already existing binding from an environment record.</param>
  70. /// <returns>True if the value is initialized, otherwise false.</returns>
  71. /// <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>
  72. internal abstract bool TryGetBindingValue(string name, bool strict, [NotNullWhen(true)] out JsValue? value);
  73. /// <summary>
  74. /// 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.
  75. /// </summary>
  76. /// <param name="name">The identifier of the binding</param>
  77. /// <returns><true>true</true> if the deletion is successfull.</returns>
  78. public abstract bool DeleteBinding(string name);
  79. public abstract bool HasThisBinding();
  80. public abstract bool HasSuperBinding();
  81. public abstract JsValue WithBaseObject();
  82. /// <summary>
  83. /// Returns an array of all the defined binding names
  84. /// </summary>
  85. /// <returns>The array of all defined bindings</returns>
  86. internal abstract string[] GetAllBindingNames();
  87. public override object ToObject()
  88. {
  89. ExceptionHelper.ThrowNotSupportedException();
  90. return null;
  91. }
  92. public override bool Equals(JsValue? other)
  93. {
  94. ExceptionHelper.ThrowNotSupportedException();
  95. return false;
  96. }
  97. public abstract JsValue GetThisBinding();
  98. public JsValue? NewTarget { get; protected set; }
  99. /// <summary>
  100. /// Helper to cache JsString/Key when environments use different lookups.
  101. /// </summary>
  102. [DebuggerDisplay("\"{Key.Name}\"")]
  103. internal readonly struct BindingName
  104. {
  105. public readonly Key Key;
  106. public readonly JsString StringValue;
  107. public BindingName(string value)
  108. {
  109. Key = (Key) value;
  110. StringValue = JsString.Create(value);
  111. }
  112. }
  113. }
  114. }