EnvironmentRecord.cs 4.8 KB

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