Environment.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(
  27. BindingName name,
  28. bool strict,
  29. out Binding binding,
  30. [NotNullWhen(true)] out JsValue? value);
  31. /// <summary>
  32. /// Creates a new mutable binding in an environment record.
  33. /// </summary>
  34. /// <param name="name">The identifier of the binding.</param>
  35. /// <param name="canBeDeleted"><c>true</c> if the binding may be subsequently deleted.</param>
  36. internal abstract void CreateMutableBinding(Key name, bool canBeDeleted = false);
  37. /// <summary>
  38. /// Creates a new but uninitialized immutable binding in an environment record.
  39. /// </summary>
  40. /// <param name="name">The identifier of the binding.</param>
  41. /// <param name="strict"><c>false</c> if the binding may used before it's been initialized.</param>
  42. internal abstract void CreateImmutableBinding(Key name, bool strict = true);
  43. /// <summary>
  44. /// Set the value of an already existing but uninitialized binding in an Environment Record.
  45. /// </summary>
  46. /// <param name="name">The text of the bound name</param>
  47. /// <param name="value">The value for the binding.</param>
  48. internal abstract void InitializeBinding(Key name, JsValue value);
  49. /// <summary>
  50. /// Sets the value of an already existing mutable binding in an environment record.
  51. /// </summary>
  52. /// <param name="name">The identifier of the binding</param>
  53. /// <param name="value">The value of the binding.</param>
  54. /// <param name="strict">The identify strict mode references.</param>
  55. internal abstract void SetMutableBinding(Key name, JsValue value, bool strict);
  56. internal abstract void SetMutableBinding(BindingName name, JsValue value, bool strict);
  57. /// <summary>
  58. /// Returns the value of an already existing binding from an environment record.
  59. /// </summary>
  60. /// <param name="name">The identifier of the binding</param>
  61. /// <param name="strict">The identify strict mode references.</param>
  62. /// <return>The value of an already existing binding from an environment record.</return>
  63. internal abstract JsValue GetBindingValue(Key name, bool strict);
  64. /// <summary>
  65. /// 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.
  66. /// </summary>
  67. /// <param name="name">The identifier of the binding</param>
  68. /// <returns><true>true</true> if the deletion is successfull.</returns>
  69. internal abstract bool DeleteBinding(Key name);
  70. internal abstract bool HasThisBinding();
  71. internal abstract bool HasSuperBinding();
  72. internal abstract JsValue WithBaseObject();
  73. internal abstract bool HasBindings();
  74. /// <summary>
  75. /// Returns an array of all the defined binding names
  76. /// </summary>
  77. /// <returns>The array of all defined bindings</returns>
  78. internal abstract string[] GetAllBindingNames();
  79. public override object ToObject()
  80. {
  81. ExceptionHelper.ThrowNotSupportedException();
  82. return null;
  83. }
  84. public override bool Equals(JsValue? other)
  85. {
  86. ExceptionHelper.ThrowNotSupportedException();
  87. return false;
  88. }
  89. internal abstract JsValue GetThisBinding();
  90. internal JsValue? NewTarget { get; set; }
  91. /// <summary>
  92. /// Helper to cache JsString/Key when environments use different lookups.
  93. /// </summary>
  94. [DebuggerDisplay("\"{Key.Name}\"")]
  95. internal sealed class BindingName
  96. {
  97. public readonly Key Key;
  98. public readonly JsString Value;
  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. if (key == KnownKeys.Undefined)
  106. {
  107. CalculatedValue = Undefined;
  108. }
  109. }
  110. public BindingName(JsString value)
  111. {
  112. var key = (Key) value.ToString();
  113. Key = key;
  114. Value = value;
  115. if (key == KnownKeys.Undefined)
  116. {
  117. CalculatedValue = Undefined;
  118. }
  119. }
  120. }
  121. private sealed class EnvironmentDebugView
  122. {
  123. private readonly Environment _record;
  124. public EnvironmentDebugView(Environment record)
  125. {
  126. _record = record;
  127. }
  128. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  129. public KeyValuePair<JsValue, JsValue>[] Entries
  130. {
  131. get
  132. {
  133. var bindingNames = _record.GetAllBindingNames();
  134. var bindings = new KeyValuePair<JsValue, JsValue>[bindingNames.Length];
  135. var i = 0;
  136. foreach (var key in bindingNames)
  137. {
  138. bindings[i++] = new KeyValuePair<JsValue, JsValue>(key, _record.GetBindingValue(key, false));
  139. }
  140. return bindings;
  141. }
  142. }
  143. }
  144. }
  145. }