2
0

Environment.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, [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. internal abstract void InitializeBinding(Key name, JsValue value);
  44. /// <summary>
  45. /// Sets the value of an already existing mutable binding in an environment record.
  46. /// </summary>
  47. /// <param name="name">The identifier of the binding</param>
  48. /// <param name="value">The value of the binding.</param>
  49. /// <param name="strict">The identify strict mode references.</param>
  50. internal abstract void SetMutableBinding(Key name, JsValue value, bool strict);
  51. internal abstract void SetMutableBinding(BindingName name, JsValue value, bool strict);
  52. /// <summary>
  53. /// Returns the value of an already existing binding from an environment record.
  54. /// </summary>
  55. /// <param name="name">The identifier of the binding</param>
  56. /// <param name="strict">The identify strict mode references.</param>
  57. /// <return>The value of an already existing binding from an environment record.</return>
  58. internal abstract JsValue GetBindingValue(Key name, bool strict);
  59. /// <summary>
  60. /// 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.
  61. /// </summary>
  62. /// <param name="name">The identifier of the binding</param>
  63. /// <returns><true>true</true> if the deletion is successfull.</returns>
  64. internal abstract bool DeleteBinding(Key name);
  65. internal abstract bool HasThisBinding();
  66. internal abstract bool HasSuperBinding();
  67. internal abstract JsValue WithBaseObject();
  68. internal abstract bool HasBindings();
  69. /// <summary>
  70. /// Returns an array of all the defined binding names
  71. /// </summary>
  72. /// <returns>The array of all defined bindings</returns>
  73. internal abstract string[] GetAllBindingNames();
  74. public override object ToObject()
  75. {
  76. ExceptionHelper.ThrowNotSupportedException();
  77. return null;
  78. }
  79. public override bool Equals(JsValue? other)
  80. {
  81. ExceptionHelper.ThrowNotSupportedException();
  82. return false;
  83. }
  84. internal abstract JsValue GetThisBinding();
  85. internal JsValue? NewTarget { get; set; }
  86. /// <summary>
  87. /// Helper to cache JsString/Key when environments use different lookups.
  88. /// </summary>
  89. [DebuggerDisplay("\"{Key.Name}\"")]
  90. internal sealed class BindingName
  91. {
  92. public readonly Key Key;
  93. public readonly JsString Value;
  94. public readonly JsValue? CalculatedValue;
  95. public BindingName(string value)
  96. {
  97. var key = (Key) value;
  98. Key = key;
  99. Value = JsString.Create(value);
  100. if (key == KnownKeys.Undefined)
  101. {
  102. CalculatedValue = Undefined;
  103. }
  104. }
  105. public BindingName(JsString value)
  106. {
  107. var key = (Key) value.ToString();
  108. Key = key;
  109. Value = value;
  110. if (key == KnownKeys.Undefined)
  111. {
  112. CalculatedValue = Undefined;
  113. }
  114. }
  115. }
  116. private sealed class EnvironmentDebugView
  117. {
  118. private readonly Environment _record;
  119. public EnvironmentDebugView(Environment record)
  120. {
  121. _record = record;
  122. }
  123. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  124. public KeyValuePair<JsValue, JsValue>[] Entries
  125. {
  126. get
  127. {
  128. var bindingNames = _record.GetAllBindingNames();
  129. var bindings = new KeyValuePair<JsValue, JsValue>[bindingNames.Length];
  130. var i = 0;
  131. foreach (var key in bindingNames)
  132. {
  133. bindings[i++] = new KeyValuePair<JsValue, JsValue>(key, _record.GetBindingValue(key, false));
  134. }
  135. return bindings;
  136. }
  137. }
  138. }
  139. }