EnvironmentRecord.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Jint.Native;
  2. namespace Jint.Runtime.Environments
  3. {
  4. /// <summary>
  5. /// Base implementation of an Environment Record
  6. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1
  7. /// </summary>
  8. public abstract class EnvironmentRecord : JsValue
  9. {
  10. protected readonly Engine _engine;
  11. protected EnvironmentRecord(Engine engine) : base(InternalTypes.ObjectEnvironmentRecord)
  12. {
  13. _engine = engine;
  14. }
  15. /// <summary>
  16. /// Determines if an environment record has a binding for an identifier.
  17. /// </summary>
  18. /// <param name="name">The identifier of the binding</param>
  19. /// <returns><c>true</c> if it does and <c>false</c> if it does not.</returns>
  20. public abstract bool HasBinding(string name);
  21. internal abstract bool TryGetBinding(
  22. in BindingName name,
  23. bool strict,
  24. out Binding binding,
  25. out JsValue value);
  26. /// <summary>
  27. /// Creates a new mutable binding in an environment record.
  28. /// </summary>
  29. /// <param name="name">The identifier of the binding.</param>
  30. /// <param name="canBeDeleted"><c>true</c> if the binding may be subsequently deleted.</param>
  31. public abstract void CreateMutableBinding(string name, bool canBeDeleted = false);
  32. /// <summary>
  33. /// Creates a new but uninitialized immutable binding in an environment record.
  34. /// </summary>
  35. /// <param name="name">The identifier of the binding.</param>
  36. /// <param name="strict"><c>false</c> if the binding may used before it's been initialized.</param>
  37. public abstract void CreateImmutableBinding(string name, bool strict = true);
  38. /// <summary>
  39. /// Set the value of an already existing but uninitialized binding in an Environment Record.
  40. /// </summary>
  41. /// <param name="name">The text of the bound name</param>
  42. /// <param name="value">The value for the binding.</param>
  43. public abstract void InitializeBinding(string name, JsValue value);
  44. /// <summary>
  45. /// Sets the value of an already existing mutable binding in an environment record.
  46. /// </summary>
  47. /// <param name="name">The identifier of the binding</param>
  48. /// <param name="value">The value of the binding.</param>
  49. /// <param name="strict">The identify strict mode references.</param>
  50. public abstract void SetMutableBinding(string name, JsValue value, bool strict);
  51. internal abstract void SetMutableBinding(in BindingName name, JsValue value, bool strict);
  52. /// <summary>
  53. /// Returns the value of an already existing binding from an environment record.
  54. /// </summary>
  55. /// <param name="name">The identifier of the binding</param>
  56. /// <param name="strict">The identify strict mode references.</param>
  57. /// <return>The value of an already existing binding from an environment record.</return>
  58. public abstract JsValue GetBindingValue(string name, bool strict);
  59. /// <summary>
  60. /// Delete a binding from an environment record. The String value N is the text of the bound name If a binding for N exists, remove the binding and return true. If the binding exists but cannot be removed return false. If the binding does not exist return true.
  61. /// </summary>
  62. /// <param name="name">The identifier of the binding</param>
  63. /// <returns><true>true</true> if the deletion is successfull.</returns>
  64. public abstract bool DeleteBinding(string name);
  65. public abstract bool HasThisBinding();
  66. public abstract bool HasSuperBinding();
  67. public abstract JsValue WithBaseObject();
  68. /// <summary>
  69. /// Returns the value to use as the <c>this</c> value on calls to function objects that are obtained as binding values from this environment record.
  70. /// </summary>
  71. /// <returns>The value to use as <c>this</c>.</returns>
  72. public abstract JsValue ImplicitThisValue();
  73. /// <summary>
  74. /// Returns an array of all the defined binding names
  75. /// </summary>
  76. /// <returns>The array of all defined bindings</returns>
  77. internal abstract string[] GetAllBindingNames();
  78. public override object ToObject()
  79. {
  80. return ExceptionHelper.ThrowNotSupportedException<object>();
  81. }
  82. public override bool Equals(JsValue other)
  83. {
  84. return ExceptionHelper.ThrowNotSupportedException<bool>();
  85. }
  86. public abstract JsValue GetThisBinding();
  87. /// <summary>
  88. /// Helper to cache JsString/Key when environments use different lookups.
  89. /// </summary>
  90. internal readonly struct BindingName
  91. {
  92. public readonly Key Key;
  93. public readonly JsString StringValue;
  94. public BindingName(string value)
  95. {
  96. Key = (Key) value;
  97. StringValue = JsString.Create(value);
  98. }
  99. }
  100. }
  101. }