Environment.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Diagnostics;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Jint.Native;
  4. namespace Jint.Runtime.Environments;
  5. /// <summary>
  6. /// Base implementation of an Environment Record
  7. /// https://tc39.es/ecma262/#sec-environment-records
  8. /// </summary>
  9. [DebuggerTypeProxy(typeof(EnvironmentDebugView))]
  10. internal abstract class Environment : JsValue
  11. {
  12. protected internal readonly Engine _engine;
  13. protected internal Environment? _outerEnv;
  14. protected Environment(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. internal abstract bool HasBinding(Key name);
  24. internal abstract bool HasBinding(BindingName name);
  25. internal abstract bool TryGetBinding(BindingName name, bool strict, [NotNullWhen(true)] out JsValue? value);
  26. /// <summary>
  27. /// Creates a new mutable binding in an environment record.
  28. /// </summary>
  29. /// <param name="name">The identifier of the binding.</param>
  30. /// <param name="canBeDeleted"><c>true</c> if the binding may be subsequently deleted.</param>
  31. internal abstract void CreateMutableBinding(Key name, bool canBeDeleted = false);
  32. /// <summary>
  33. /// Creates a new but uninitialized immutable binding in an environment record.
  34. /// </summary>
  35. /// <param name="name">The identifier of the binding.</param>
  36. /// <param name="strict"><c>false</c> if the binding may used before it's been initialized.</param>
  37. internal abstract void CreateImmutableBinding(Key name, bool strict = true);
  38. /// <summary>
  39. /// Set the value of an already existing but uninitialized binding in an Environment Record.
  40. /// </summary>
  41. /// <param name="name">The text of the bound name</param>
  42. /// <param name="value">The value for the binding.</param>
  43. /// <param name="hint">Disposal type hint</param>
  44. internal abstract void InitializeBinding(Key name, JsValue value, DisposeHint hint);
  45. /// <summary>
  46. /// Sets the value of an already existing mutable binding in an environment record.
  47. /// </summary>
  48. /// <param name="name">The identifier of the binding</param>
  49. /// <param name="value">The value of the binding.</param>
  50. /// <param name="strict">The identify strict mode references.</param>
  51. internal abstract void SetMutableBinding(Key name, JsValue value, bool strict);
  52. internal abstract void SetMutableBinding(BindingName name, JsValue value, bool strict);
  53. /// <summary>
  54. /// Returns the value of an already existing binding from an environment record.
  55. /// </summary>
  56. /// <param name="name">The identifier of the binding</param>
  57. /// <param name="strict">The identify strict mode references.</param>
  58. /// <return>The value of an already existing binding from an environment record.</return>
  59. internal abstract JsValue GetBindingValue(Key name, bool strict);
  60. /// <summary>
  61. /// 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.
  62. /// </summary>
  63. /// <param name="name">The identifier of the binding</param>
  64. /// <returns><true>true</true> if the deletion is successfull.</returns>
  65. internal abstract bool DeleteBinding(Key name);
  66. internal abstract bool HasThisBinding();
  67. internal abstract bool HasSuperBinding();
  68. internal abstract JsValue WithBaseObject();
  69. internal abstract bool HasBindings();
  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. Throw.NotSupportedException();
  78. return null;
  79. }
  80. public override bool Equals(JsValue? other)
  81. {
  82. Throw.NotSupportedException();
  83. return false;
  84. }
  85. internal abstract JsValue GetThisBinding();
  86. internal JsValue? NewTarget { get; set; }
  87. internal virtual Completion DisposeResources(Completion c) => c;
  88. /// <summary>
  89. /// Helper to cache JsString/Key when environments use different lookups.
  90. /// </summary>
  91. [DebuggerDisplay("\"{Key.Name}\"")]
  92. internal sealed class BindingName
  93. {
  94. public readonly Key Key;
  95. public readonly JsString Value;
  96. public readonly JsValue? CalculatedValue;
  97. public BindingName(string value)
  98. {
  99. var key = (Key) value;
  100. Key = key;
  101. Value = JsString.Create(value);
  102. if (key == KnownKeys.Undefined)
  103. {
  104. CalculatedValue = Undefined;
  105. }
  106. }
  107. public BindingName(JsString value)
  108. {
  109. var key = (Key) value.ToString();
  110. Key = key;
  111. Value = value;
  112. if (key == KnownKeys.Undefined)
  113. {
  114. CalculatedValue = Undefined;
  115. }
  116. }
  117. }
  118. private sealed class EnvironmentDebugView
  119. {
  120. private readonly Environment _record;
  121. public EnvironmentDebugView(Environment record)
  122. {
  123. _record = record;
  124. }
  125. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  126. public KeyValuePair<JsValue, JsValue>[] Entries
  127. {
  128. get
  129. {
  130. var bindingNames = _record.GetAllBindingNames();
  131. var bindings = new KeyValuePair<JsValue, JsValue>[bindingNames.Length];
  132. var i = 0;
  133. foreach (var key in bindingNames)
  134. {
  135. bindings[i++] = new KeyValuePair<JsValue, JsValue>(key, _record.GetBindingValue(key, false));
  136. }
  137. return bindings;
  138. }
  139. }
  140. }
  141. }