EnvironmentRecord.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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(BindingName name);
  25. internal abstract bool TryGetBinding(
  26. 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(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. /// 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.
  65. /// </summary>
  66. /// <param name="name">The identifier of the binding</param>
  67. /// <returns><true>true</true> if the deletion is successfull.</returns>
  68. public abstract bool DeleteBinding(string name);
  69. public abstract bool HasThisBinding();
  70. public abstract bool HasSuperBinding();
  71. public abstract JsValue WithBaseObject();
  72. public abstract bool HasBindings();
  73. /// <summary>
  74. /// Returns an array of all the defined binding names
  75. /// </summary>
  76. /// <returns>The array of all defined bindings</returns>
  77. internal abstract string[] GetAllBindingNames();
  78. public override object ToObject()
  79. {
  80. ExceptionHelper.ThrowNotSupportedException();
  81. return null;
  82. }
  83. public override bool Equals(JsValue? other)
  84. {
  85. ExceptionHelper.ThrowNotSupportedException();
  86. return false;
  87. }
  88. public abstract JsValue GetThisBinding();
  89. public JsValue? NewTarget { get; protected set; }
  90. /// <summary>
  91. /// Helper to cache JsString/Key when environments use different lookups.
  92. /// </summary>
  93. [DebuggerDisplay("\"{Key.Name}\"")]
  94. internal sealed class BindingName
  95. {
  96. public readonly Key Key;
  97. public readonly JsString Value;
  98. public readonly bool HasEvalOrArguments;
  99. public readonly JsValue? CalculatedValue;
  100. public BindingName(string value)
  101. {
  102. var key = (Key) value;
  103. Key = key;
  104. Value = JsString.Create(value);
  105. HasEvalOrArguments = key == KnownKeys.Eval || key == KnownKeys.Arguments;
  106. if (key == KnownKeys.Undefined)
  107. {
  108. CalculatedValue = Undefined;
  109. }
  110. }
  111. }
  112. }
  113. }