ObjectEnvironmentRecord.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ObjectInstance _bindingObject;
  15. private readonly bool _provideThis;
  16. public ObjectEnvironmentRecord(Engine engine, ObjectInstance bindingObject, bool provideThis) : base(engine)
  17. {
  18. _bindingObject = bindingObject;
  19. _provideThis = provideThis;
  20. }
  21. public override bool HasBinding(string name)
  22. {
  23. return _bindingObject.HasProperty(name);
  24. }
  25. /// <summary>
  26. /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2.2
  27. /// </summary>
  28. public override void CreateMutableBinding(string name, JsValue value, bool configurable = true)
  29. {
  30. var propertyDescriptor = configurable
  31. ? new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable)
  32. : new PropertyDescriptor(value, PropertyFlag.NonConfigurable);
  33. _bindingObject.SetOwnProperty(name, propertyDescriptor);
  34. }
  35. public override void SetMutableBinding(string name, JsValue value, bool strict)
  36. {
  37. _bindingObject.Put(name, value, strict);
  38. }
  39. public override JsValue GetBindingValue(string name, bool strict)
  40. {
  41. var desc = _bindingObject.GetProperty(name);
  42. if (strict && desc == PropertyDescriptor.Undefined)
  43. {
  44. ExceptionHelper.ThrowReferenceError(_engine);
  45. }
  46. return ObjectInstance.UnwrapJsValue(desc, this);
  47. }
  48. public override bool DeleteBinding(string name)
  49. {
  50. return _bindingObject.Delete(name, false);
  51. }
  52. public override JsValue ImplicitThisValue()
  53. {
  54. if (_provideThis)
  55. {
  56. return _bindingObject;
  57. }
  58. return Undefined;
  59. }
  60. public override string[] GetAllBindingNames()
  61. {
  62. if (!ReferenceEquals(_bindingObject, null))
  63. {
  64. return _bindingObject.GetOwnProperties().Select( x=> x.Key).ToArray();
  65. }
  66. return ArrayExt.Empty<string>();
  67. }
  68. }
  69. }