DeclarativeEnvironment.cs 5.5 KB

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