PropertyDescriptor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Jint.Native;
  2. using Jint.Native.Object;
  3. namespace Jint.Runtime.Descriptors
  4. {
  5. /// <summary>
  6. /// An element of a javascript object
  7. /// </summary>
  8. public abstract class PropertyDescriptor
  9. {
  10. public static PropertyDescriptor Undefined = new UndefinedPropertyDescriptor();
  11. /// <summary>
  12. /// If true, the property will be enumerated by a for-in
  13. /// enumeration (see 12.6.4). Otherwise, the property is said
  14. /// to be non-enumerable.
  15. /// </summary>
  16. public bool? Enumerable { get; set; }
  17. public bool EnumerableIsSet
  18. {
  19. get { return Enumerable.HasValue && Enumerable.Value; }
  20. }
  21. /// <summary>
  22. /// If false, attempts to delete the property, change the
  23. /// property to be a data property, or change its attributes will
  24. /// fail.
  25. /// </summary>
  26. public bool? Configurable { get; set; }
  27. public bool ConfigurableIsSet
  28. {
  29. get { return Configurable.HasValue && Configurable.Value; }
  30. }
  31. /// <summary>
  32. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1
  33. /// </summary>
  34. /// <returns></returns>
  35. public abstract bool IsAccessorDescriptor();
  36. /// <summary>
  37. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.2
  38. /// </summary>
  39. /// <returns></returns>
  40. public abstract bool IsDataDescriptor();
  41. /// <summary>
  42. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.3
  43. /// </summary>
  44. /// <returns></returns>
  45. public bool IsGenericDescriptor()
  46. {
  47. return !IsDataDescriptor() && !IsAccessorDescriptor();
  48. }
  49. public T As<T>() where T : PropertyDescriptor
  50. {
  51. return (T)this;
  52. }
  53. public static PropertyDescriptor ToPropertyDescriptor(Engine engine, object o)
  54. {
  55. var obj = o as ObjectInstance;
  56. if (obj == null)
  57. {
  58. throw new JavaScriptException(engine.TypeError);
  59. }
  60. if ((obj.HasProperty("value") || obj.HasProperty("writable")) &&
  61. (obj.HasProperty("get") || obj.HasProperty("set")))
  62. {
  63. throw new JavaScriptException(engine.TypeError);
  64. }
  65. bool? writable = obj.HasProperty("writable") ? TypeConverter.ToBoolean(obj.Get("writable")) : default(bool?);
  66. bool? enumerable = obj.HasProperty("enumerable") ? TypeConverter.ToBoolean(obj.Get("enumerable")) : default(bool?);
  67. bool? configurable = obj.HasProperty("configurable") ? TypeConverter.ToBoolean(obj.Get("configurable")) : default(bool?);
  68. if (obj.HasProperty("value"))
  69. {
  70. var value = obj.Get("value");
  71. return new DataDescriptor(value) { Configurable = configurable, Enumerable = enumerable, Writable = writable};
  72. }
  73. else
  74. {
  75. object getter = null, setter = null;
  76. if (obj.HasProperty("get"))
  77. {
  78. getter = obj.Get("get");
  79. if (getter != Native.Undefined.Instance && !(getter is ICallable))
  80. {
  81. throw new JavaScriptException(engine.TypeError);
  82. }
  83. }
  84. if (obj.HasProperty("set"))
  85. {
  86. setter = obj.Get("set");
  87. if (setter != Native.Undefined.Instance && !(setter is ICallable))
  88. {
  89. throw new JavaScriptException(engine.TypeError);
  90. }
  91. }
  92. return new AccessorDescriptor(getter as ICallable, setter as ICallable) { Configurable = configurable, Enumerable = enumerable };
  93. }
  94. }
  95. public static object FromPropertyDescriptor(Engine engine, PropertyDescriptor desc)
  96. {
  97. if (desc == PropertyDescriptor.Undefined)
  98. {
  99. return Native.Undefined.Instance;
  100. }
  101. var obj = engine.Object.Construct(Arguments.Empty);
  102. if (desc.IsDataDescriptor())
  103. {
  104. var datadesc = desc.As<DataDescriptor>();
  105. obj.DefineOwnProperty("value", new DataDescriptor(datadesc.Value) { Writable = true, Enumerable = true, Configurable = true }, false);
  106. obj.DefineOwnProperty("writable", new DataDescriptor(datadesc.Writable) { Writable = true, Enumerable = true, Configurable = true }, false);
  107. }
  108. else
  109. {
  110. var accdesc = desc.As<AccessorDescriptor>();
  111. obj.DefineOwnProperty("get", new DataDescriptor(accdesc.Get ?? Native.Undefined.Instance) { Writable = true, Enumerable = true, Configurable = true }, false);
  112. obj.DefineOwnProperty("set", new DataDescriptor(accdesc.Set ?? Native.Undefined.Instance) { Writable = true, Enumerable = true, Configurable = true }, false);
  113. }
  114. obj.DefineOwnProperty("enumerable", new DataDescriptor(desc.Enumerable) { Writable = true, Enumerable = true, Configurable = true }, false);
  115. obj.DefineOwnProperty("configurable", new DataDescriptor(desc.Configurable) { Writable = true, Enumerable = true, Configurable = true }, false);
  116. return obj;
  117. }
  118. /// <summary>
  119. /// Local implementation used to create a singleton representing
  120. /// an undefined result of a PropertyDescriptor. This prevents the rest
  121. /// of the code to return 'object' in order to be able to return
  122. /// Undefined.Instance
  123. /// </summary>
  124. internal sealed class UndefinedPropertyDescriptor : PropertyDescriptor
  125. {
  126. public override bool IsAccessorDescriptor()
  127. {
  128. return false;
  129. }
  130. public override bool IsDataDescriptor()
  131. {
  132. return false;
  133. }
  134. }
  135. }
  136. }