DeclarativeEnvironmentRecord.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections.Generic;
  2. using System.Linq;
  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. public sealed class DeclarativeEnvironmentRecord : EnvironmentRecord
  11. {
  12. private readonly Engine _engine;
  13. private readonly IDictionary<string, Binding> _bindings = new Dictionary<string, Binding>();
  14. public DeclarativeEnvironmentRecord(Engine engine) : base(engine)
  15. {
  16. _engine = engine;
  17. }
  18. public override bool HasBinding(string name)
  19. {
  20. return _bindings.ContainsKey(name);
  21. }
  22. public override void CreateMutableBinding(string name, bool canBeDeleted = false)
  23. {
  24. _bindings.Add(name, new Binding
  25. {
  26. Value = Undefined.Instance,
  27. CanBeDeleted = canBeDeleted,
  28. Mutable = true
  29. });
  30. }
  31. public override void SetMutableBinding(string name, JsValue value, bool strict)
  32. {
  33. var binding = _bindings[name];
  34. if (binding.Mutable)
  35. {
  36. binding.Value = value;
  37. }
  38. else
  39. {
  40. if (strict)
  41. {
  42. throw new JavaScriptException(_engine.TypeError, "Can't update the value of an immutable binding.");
  43. }
  44. }
  45. }
  46. public override JsValue GetBindingValue(string name, bool strict)
  47. {
  48. var binding = _bindings[name];
  49. if (!binding.Mutable && binding.Value == Undefined.Instance)
  50. {
  51. if (strict)
  52. {
  53. throw new JavaScriptException(_engine.ReferenceError, "Can't access anm uninitiazed immutable binding.");
  54. }
  55. return Undefined.Instance;
  56. }
  57. return binding.Value;
  58. }
  59. public override bool DeleteBinding(string name)
  60. {
  61. Binding binding;
  62. if (!_bindings.TryGetValue(name, out binding))
  63. {
  64. return true;
  65. }
  66. if (!binding.CanBeDeleted)
  67. {
  68. return false;
  69. }
  70. _bindings.Remove(name);
  71. return true;
  72. }
  73. public override JsValue ImplicitThisValue()
  74. {
  75. return Undefined.Instance;
  76. }
  77. /// <summary>
  78. /// Creates a new but uninitialised immutable binding in an environment record.
  79. /// </summary>
  80. /// <param name="name">The identifier of the binding.</param>
  81. public void CreateImmutableBinding(string name)
  82. {
  83. _bindings.Add(name, new Binding
  84. {
  85. Value = Undefined.Instance,
  86. Mutable = false,
  87. CanBeDeleted = false
  88. });
  89. }
  90. /// <summary>
  91. /// Sets the value of an already existing but uninitialised immutable binding in an environment record.
  92. /// </summary>
  93. /// <param name="name">The identifier of the binding.</param>
  94. /// <param name="value">The value of the binding.</param>
  95. public void InitializeImmutableBinding(string name, JsValue value)
  96. {
  97. var binding = _bindings[name];
  98. binding.Value = value;
  99. }
  100. /// <summary>
  101. /// Returns an array of all the defined binding names
  102. /// </summary>
  103. /// <returns>The array of all defined bindings</returns>
  104. public override string[] GetAllBindingNames()
  105. {
  106. return _bindings.Keys.ToArray();
  107. }
  108. }
  109. }