PropertyDescriptor.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Jint.Native;
  2. using Jint.Native.Object;
  3. namespace Jint.Runtime.Descriptors
  4. {
  5. public enum Fields
  6. {
  7. Get,
  8. Set,
  9. Enumerable,
  10. Configurable,
  11. Writable,
  12. Value
  13. }
  14. public class PropertyDescriptor
  15. {
  16. public static PropertyDescriptor Undefined = new PropertyDescriptor();
  17. public PropertyDescriptor()
  18. {
  19. }
  20. public PropertyDescriptor(JsValue? value, bool? writable, bool? enumerable, bool? configurable)
  21. {
  22. Value = value;
  23. if (writable.HasValue)
  24. {
  25. Writable = new JsValue(writable.Value);
  26. }
  27. if (enumerable.HasValue)
  28. {
  29. Enumerable = new JsValue(enumerable.Value);
  30. }
  31. if (configurable.HasValue)
  32. {
  33. Configurable = new JsValue(configurable.Value);
  34. }
  35. }
  36. public PropertyDescriptor(JsValue? get, JsValue? set, bool? enumerable = null, bool? configurable = null)
  37. {
  38. Get = get;
  39. Set = set;
  40. if (enumerable.HasValue)
  41. {
  42. Enumerable = new JsValue(enumerable.Value);
  43. }
  44. if (configurable.HasValue)
  45. {
  46. Configurable = new JsValue(configurable.Value);
  47. }
  48. }
  49. public PropertyDescriptor(PropertyDescriptor descriptor)
  50. {
  51. Get = descriptor.Get;
  52. Set = descriptor.Set;
  53. Value = descriptor.Value;
  54. Enumerable = descriptor.Enumerable;
  55. Configurable = descriptor.Configurable;
  56. Writable = descriptor.Writable;
  57. }
  58. public JsValue? Get { get; set; }
  59. public JsValue? Set { get; set; }
  60. public JsValue? Enumerable { get; set; }
  61. public JsValue? Writable { get; set; }
  62. public JsValue? Configurable { get; set; }
  63. public virtual JsValue? Value { get; set; }
  64. public bool IsAccessorDescriptor()
  65. {
  66. if (!Get.HasValue && !Set.HasValue)
  67. {
  68. return false;
  69. }
  70. return true;
  71. }
  72. public bool IsDataDescriptor()
  73. {
  74. if (!Writable.HasValue && !Value.HasValue)
  75. {
  76. return false;
  77. }
  78. return true;
  79. }
  80. /// <summary>
  81. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.3
  82. /// </summary>
  83. /// <returns></returns>
  84. public bool IsGenericDescriptor()
  85. {
  86. return !IsDataDescriptor() && !IsAccessorDescriptor();
  87. }
  88. public static PropertyDescriptor ToPropertyDescriptor(Engine engine, JsValue o)
  89. {
  90. var obj = o.TryCast<ObjectInstance>();
  91. if (obj == null)
  92. {
  93. throw new JavaScriptException(engine.TypeError);
  94. }
  95. if ((obj.HasProperty("value") || obj.HasProperty("writable")) &&
  96. (obj.HasProperty("get") || obj.HasProperty("set")))
  97. {
  98. throw new JavaScriptException(engine.TypeError);
  99. }
  100. var desc = new PropertyDescriptor();
  101. if (obj.HasProperty("enumerable"))
  102. {
  103. desc.Enumerable = TypeConverter.ToBoolean(obj.Get("enumerable"));
  104. }
  105. if (obj.HasProperty("configurable"))
  106. {
  107. desc.Configurable = TypeConverter.ToBoolean(obj.Get("configurable"));
  108. }
  109. if (obj.HasProperty("value"))
  110. {
  111. var value = obj.Get("value");
  112. desc.Value = value;
  113. }
  114. if (obj.HasProperty("writable"))
  115. {
  116. desc.Writable = TypeConverter.ToBoolean(obj.Get("writable"));
  117. }
  118. if (obj.HasProperty("get"))
  119. {
  120. var getter = obj.Get("get");
  121. if (getter != JsValue.Undefined && getter.TryCast<ICallable>() == null)
  122. {
  123. throw new JavaScriptException(engine.TypeError);
  124. }
  125. desc.Get = getter;
  126. }
  127. if (obj.HasProperty("set"))
  128. {
  129. var setter = obj.Get("set");
  130. if (setter != Native.Undefined.Instance && setter.TryCast<ICallable>() == null)
  131. {
  132. throw new JavaScriptException(engine.TypeError);
  133. }
  134. desc.Set = setter;
  135. }
  136. if (desc.Get.HasValue || desc.Get.HasValue)
  137. {
  138. if (desc.Value.HasValue || desc.Writable.HasValue)
  139. {
  140. throw new JavaScriptException(engine.TypeError);
  141. }
  142. }
  143. return desc;
  144. }
  145. public static JsValue FromPropertyDescriptor(Engine engine, PropertyDescriptor desc)
  146. {
  147. if (desc == Undefined)
  148. {
  149. return Native.Undefined.Instance;
  150. }
  151. var obj = engine.Object.Construct(Arguments.Empty);
  152. if (desc.IsDataDescriptor())
  153. {
  154. obj.DefineOwnProperty("value", new PropertyDescriptor(value: desc.Value.HasValue ? desc.Value.Value : Native.Undefined.Instance, writable: true, enumerable: true, configurable: true ), false);
  155. obj.DefineOwnProperty("writable", new PropertyDescriptor(value: desc.Writable.HasValue && desc.Writable.Value.AsBoolean(), writable: true, enumerable: true, configurable: true), false);
  156. }
  157. else
  158. {
  159. obj.DefineOwnProperty("get", new PropertyDescriptor(desc.Get ?? Native.Undefined.Instance, writable: true, enumerable: true, configurable: true ), false);
  160. obj.DefineOwnProperty("set", new PropertyDescriptor(desc.Set ?? Native.Undefined.Instance, writable: true, enumerable: true, configurable: true), false);
  161. }
  162. obj.DefineOwnProperty("enumerable", new PropertyDescriptor(value: desc.Enumerable.HasValue && desc.Enumerable.Value.AsBoolean(), writable: true, enumerable: true, configurable: true), false);
  163. obj.DefineOwnProperty("configurable", new PropertyDescriptor(value: desc.Configurable.HasValue && desc.Configurable.Value.AsBoolean(), writable: true, enumerable: true, configurable: true), false);
  164. return obj;
  165. }
  166. }
  167. }