ObjectEnvironmentRecord.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Linq;
  3. using Jint.Native;
  4. using Jint.Native.Object;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Descriptors.Specialized;
  7. namespace Jint.Runtime.Environments
  8. {
  9. /// <summary>
  10. /// Represents an object environment record
  11. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2
  12. /// </summary>
  13. public sealed class ObjectEnvironmentRecord : EnvironmentRecord
  14. {
  15. private readonly Engine _engine;
  16. private readonly ObjectInstance _bindingObject;
  17. private readonly bool _provideThis;
  18. public ObjectEnvironmentRecord(Engine engine, ObjectInstance bindingObject, bool provideThis) : base(engine)
  19. {
  20. _engine = engine;
  21. _bindingObject = bindingObject;
  22. _provideThis = provideThis;
  23. }
  24. public override bool HasBinding(string name)
  25. {
  26. return _bindingObject.HasProperty(name);
  27. }
  28. /// <summary>
  29. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2.2
  30. /// </summary>
  31. /// <param name="name"></param>
  32. /// <param name="configurable"></param>
  33. public override void CreateMutableBinding(string name, bool configurable = true)
  34. {
  35. var propertyDescriptor = configurable
  36. ? (IPropertyDescriptor) new ConfigurableEnumerableWritablePropertyDescriptor(Undefined)
  37. : new NonConfigurablePropertyDescriptor(Undefined);
  38. _bindingObject.SetOwnProperty(name, propertyDescriptor);
  39. }
  40. public override void SetMutableBinding(string name, JsValue value, bool strict)
  41. {
  42. _bindingObject.Put(name, value, strict);
  43. }
  44. public override JsValue GetBindingValue(string name, bool strict)
  45. {
  46. // todo: can be optimized
  47. if (!_bindingObject.HasProperty(name))
  48. {
  49. if(!strict)
  50. {
  51. return Undefined;
  52. }
  53. throw new JavaScriptException(_engine.ReferenceError);
  54. }
  55. return _bindingObject.Get(name);
  56. }
  57. public override bool DeleteBinding(string name)
  58. {
  59. return _bindingObject.Delete(name, false);
  60. }
  61. public override JsValue ImplicitThisValue()
  62. {
  63. if (_provideThis)
  64. {
  65. return _bindingObject;
  66. }
  67. return Undefined;
  68. }
  69. public override string[] GetAllBindingNames()
  70. {
  71. if (_bindingObject != null)
  72. {
  73. return _bindingObject.GetOwnProperties().Select( x=> x.Key).ToArray();
  74. }
  75. return Array.Empty<string>();
  76. }
  77. }
  78. }