ObjectEnvironmentRecord.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. internal override void FunctionWasCalled()
  69. {
  70. }
  71. }
  72. }