ObjectEnvironmentRecord.cs 2.5 KB

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