DeclarativeEnvironmentRecord.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Runtime.CompilerServices;
  2. using Jint.Collections;
  3. using Jint.Native;
  4. namespace Jint.Runtime.Environments
  5. {
  6. /// <summary>
  7. /// Represents a declarative environment record
  8. /// https://tc39.es/ecma262/#sec-declarative-environment-records
  9. /// </summary>
  10. internal class DeclarativeEnvironmentRecord : EnvironmentRecord
  11. {
  12. internal readonly HybridDictionary<Binding> _dictionary = new HybridDictionary<Binding>();
  13. internal bool _hasBindings;
  14. internal readonly bool _catchEnvironment;
  15. public DeclarativeEnvironmentRecord(Engine engine, bool catchEnvironment = false) : base(engine)
  16. {
  17. _catchEnvironment = catchEnvironment;
  18. }
  19. public sealed override bool HasBinding(string name)
  20. {
  21. return _dictionary.ContainsKey(name);
  22. }
  23. internal sealed override bool TryGetBinding(
  24. in BindingName name,
  25. bool strict,
  26. out Binding binding,
  27. out JsValue value)
  28. {
  29. binding = default;
  30. var success = _dictionary.TryGetValue(name.Key, out binding);
  31. value = success && binding.IsInitialized() ? UnwrapBindingValue(strict, binding) : default;
  32. return success;
  33. }
  34. internal void CreateMutableBindingAndInitialize(Key name, bool canBeDeleted, JsValue value)
  35. {
  36. _hasBindings = true;
  37. _dictionary[name] = new Binding(value, canBeDeleted, mutable: true, strict: false);
  38. }
  39. internal void CreateImmutableBindingAndInitialize(Key name, bool strict, JsValue value)
  40. {
  41. _hasBindings = true;
  42. _dictionary[name] = new Binding(value, canBeDeleted: false, mutable: false, strict);
  43. }
  44. public sealed override void CreateMutableBinding(string name, bool canBeDeleted = false)
  45. {
  46. _hasBindings = true;
  47. _dictionary[name] = new Binding(null, canBeDeleted, mutable: true, strict: false);
  48. }
  49. public sealed override void CreateImmutableBinding(string name, bool strict = true)
  50. {
  51. _hasBindings = true;
  52. _dictionary[name] = new Binding(null, canBeDeleted: false, mutable: false, strict);
  53. }
  54. public sealed override void InitializeBinding(string name, JsValue value)
  55. {
  56. _hasBindings = true;
  57. _dictionary.TryGetValue(name, out var binding);
  58. _dictionary[name] = binding.ChangeValue(value);
  59. }
  60. internal override void SetMutableBinding(in BindingName name, JsValue value, bool strict)
  61. {
  62. SetMutableBinding(name.Key.Name, value, strict);
  63. }
  64. public sealed override void SetMutableBinding(string name, JsValue value, bool strict)
  65. {
  66. var key = (Key) name;
  67. if (!_dictionary.TryGetValue(key, out var binding))
  68. {
  69. if (strict)
  70. {
  71. ExceptionHelper.ThrowReferenceError(_engine.Realm, key);
  72. }
  73. CreateMutableBindingAndInitialize(key, canBeDeleted: true, value);
  74. return;
  75. }
  76. if (binding.Strict)
  77. {
  78. strict = true;
  79. }
  80. // Is it an uninitialized binding?
  81. if (!binding.IsInitialized())
  82. {
  83. ExceptionHelper.ThrowReferenceError(_engine.Realm, "Cannot access '" + key + "' before initialization");
  84. }
  85. if (binding.Mutable)
  86. {
  87. _hasBindings = true;
  88. _dictionary[key] = binding.ChangeValue(value);
  89. }
  90. else
  91. {
  92. if (strict)
  93. {
  94. ExceptionHelper.ThrowTypeError(_engine.Realm, "Assignment to constant variable.");
  95. }
  96. }
  97. }
  98. public sealed override JsValue GetBindingValue(string name, bool strict)
  99. {
  100. _dictionary.TryGetValue(name, out var binding);
  101. return UnwrapBindingValue(strict, binding);
  102. }
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. private JsValue UnwrapBindingValue(bool strict, in Binding binding)
  105. {
  106. if (!binding.Mutable && !binding.IsInitialized())
  107. {
  108. if (strict)
  109. {
  110. ThrowUninitializedBindingException();
  111. }
  112. return Undefined;
  113. }
  114. return binding.Value;
  115. }
  116. private void ThrowUninitializedBindingException()
  117. {
  118. throw new JavaScriptException(_engine.Realm.Intrinsics.ReferenceError, "Can't access an uninitialized immutable binding.");
  119. }
  120. public sealed override bool DeleteBinding(string name)
  121. {
  122. if (!_dictionary.TryGetValue(name, out var binding))
  123. {
  124. return true;
  125. }
  126. if (!binding.CanBeDeleted)
  127. {
  128. return false;
  129. }
  130. _dictionary.Remove(name);
  131. _hasBindings = _dictionary.Count > 0;
  132. return true;
  133. }
  134. public override bool HasThisBinding() => false;
  135. public override bool HasSuperBinding() => false;
  136. public override JsValue WithBaseObject() => Undefined;
  137. /// <inheritdoc />
  138. internal sealed override string[] GetAllBindingNames()
  139. {
  140. if (_dictionary is null)
  141. {
  142. return System.Array.Empty<string>();
  143. }
  144. var keys = new string[_dictionary.Count];
  145. var n = 0;
  146. foreach (var entry in _dictionary)
  147. {
  148. keys[n++] = entry.Key;
  149. }
  150. return keys;
  151. }
  152. public override JsValue GetThisBinding()
  153. {
  154. throw new System.NotImplementedException();
  155. }
  156. }
  157. }