Environment.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. [DebuggerTypeProxy(typeof(EnvironmentDebugView))]
  11. internal abstract class Environment : JsValue
  12. {
  13. protected internal readonly Engine _engine;
  14. protected internal Environment? _outerEnv;
  15. protected Environment(Engine engine) : base(InternalTypes.ObjectEnvironmentRecord)
  16. {
  17. _engine = engine;
  18. }
  19. /// <summary>
  20. /// Determines if an environment record has a binding for an identifier.
  21. /// </summary>
  22. /// <param name="name">The identifier of the binding</param>
  23. /// <returns><c>true</c> if it does and <c>false</c> if it does not.</returns>
  24. internal abstract bool HasBinding(Key name);
  25. internal abstract bool HasBinding(BindingName name);
  26. internal abstract bool TryGetBinding(BindingName name, [NotNullWhen(true)] out JsValue? value);
  27. /// <summary>
  28. /// Creates a new mutable binding in an environment record.
  29. /// </summary>
  30. /// <param name="name">The identifier of the binding.</param>
  31. /// <param name="canBeDeleted"><c>true</c> if the binding may be subsequently deleted.</param>
  32. internal abstract void CreateMutableBinding(Key name, bool canBeDeleted = false);
  33. /// <summary>
  34. /// Creates a new but uninitialized immutable binding in an environment record.
  35. /// </summary>
  36. /// <param name="name">The identifier of the binding.</param>
  37. /// <param name="strict"><c>false</c> if the binding may used before it's been initialized.</param>
  38. internal abstract void CreateImmutableBinding(Key name, bool strict = true);
  39. /// <summary>
  40. /// Set the value of an already existing but uninitialized binding in an Environment Record.
  41. /// </summary>
  42. /// <param name="name">The text of the bound name</param>
  43. /// <param name="value">The value for the binding.</param>
  44. internal abstract void InitializeBinding(Key name, JsValue value);
  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. ExceptionHelper.ThrowNotSupportedException();
  78. return null;
  79. }
  80. public override bool Equals(JsValue? other)
  81. {
  82. ExceptionHelper.ThrowNotSupportedException();
  83. return false;
  84. }
  85. internal abstract JsValue GetThisBinding();
  86. internal JsValue? NewTarget { get; set; }
  87. /// <summary>
  88. /// Helper to cache JsString/Key when environments use different lookups.
  89. /// </summary>
  90. [DebuggerDisplay("\"{Key.Name}\"")]
  91. internal sealed class BindingName
  92. {
  93. public readonly Key Key;
  94. public readonly JsString Value;
  95. public readonly JsValue? CalculatedValue;
  96. public BindingName(string value)
  97. {
  98. var key = (Key) value;
  99. Key = key;
  100. Value = JsString.Create(value);
  101. if (key == KnownKeys.Undefined)
  102. {
  103. CalculatedValue = Undefined;
  104. }
  105. }
  106. public BindingName(JsString value)
  107. {
  108. var key = (Key) value.ToString();
  109. Key = key;
  110. Value = value;
  111. if (key == KnownKeys.Undefined)
  112. {
  113. CalculatedValue = Undefined;
  114. }
  115. }
  116. }
  117. private sealed class EnvironmentDebugView
  118. {
  119. private readonly Environment _record;
  120. public EnvironmentDebugView(Environment record)
  121. {
  122. _record = record;
  123. }
  124. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  125. public KeyValuePair<JsValue, JsValue>[] Entries
  126. {
  127. get
  128. {
  129. var bindingNames = _record.GetAllBindingNames();
  130. var bindings = new KeyValuePair<JsValue, JsValue>[bindingNames.Length];
  131. var i = 0;
  132. foreach (var key in bindingNames)
  133. {
  134. bindings[i++] = new KeyValuePair<JsValue, JsValue>(key, _record.GetBindingValue(key, false));
  135. }
  136. return bindings;
  137. }
  138. }
  139. }
  140. }
  141. }