DeclarativeEnvironmentRecord.cs 5.8 KB

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