EnvironmentRecord.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #nullable enable
  2. using System.Diagnostics;
  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. 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. /// 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.
  64. /// </summary>
  65. /// <param name="name">The identifier of the binding</param>
  66. /// <returns><true>true</true> if the deletion is successfull.</returns>
  67. public abstract bool DeleteBinding(string name);
  68. public abstract bool HasThisBinding();
  69. public abstract bool HasSuperBinding();
  70. public abstract JsValue WithBaseObject();
  71. /// <summary>
  72. /// Returns an array of all the defined binding names
  73. /// </summary>
  74. /// <returns>The array of all defined bindings</returns>
  75. internal abstract string[] GetAllBindingNames();
  76. public override object ToObject()
  77. {
  78. ExceptionHelper.ThrowNotSupportedException();
  79. return null;
  80. }
  81. public override bool Equals(JsValue other)
  82. {
  83. ExceptionHelper.ThrowNotSupportedException();
  84. return false;
  85. }
  86. public abstract JsValue GetThisBinding();
  87. public JsValue? NewTarget { get; protected set; }
  88. /// <summary>
  89. /// Helper to cache JsString/Key when environments use different lookups.
  90. /// </summary>
  91. [DebuggerDisplay("\"{Key.Name}\"")]
  92. internal readonly struct BindingName
  93. {
  94. public readonly Key Key;
  95. public readonly JsString StringValue;
  96. public BindingName(string value)
  97. {
  98. Key = (Key) value;
  99. StringValue = JsString.Create(value);
  100. }
  101. }
  102. }
  103. }