DeclarativeEnvironment.cs 6.1 KB

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