PropertyDescriptor.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 ConfigurableIsSetToTrue
  28. {
  29. get { return Configurable.HasValue && Configurable.Value; }
  30. }
  31. public bool ConfigurableIsSetToFalse
  32. {
  33. get { return Configurable.HasValue && !Configurable.Value; }
  34. }
  35. /// <summary>
  36. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.1
  37. /// </summary>
  38. /// <returns></returns>
  39. public abstract bool IsAccessorDescriptor();
  40. /// <summary>
  41. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.2
  42. /// </summary>
  43. /// <returns></returns>
  44. public abstract bool IsDataDescriptor();
  45. /// <summary>
  46. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.10.3
  47. /// </summary>
  48. /// <returns></returns>
  49. public bool IsGenericDescriptor()
  50. {
  51. return !IsDataDescriptor() && !IsAccessorDescriptor();
  52. }
  53. public T As<T>() where T : PropertyDescriptor
  54. {
  55. return (T)this;
  56. }
  57. public static PropertyDescriptor ToPropertyDescriptor(Engine engine, object o)
  58. {
  59. var obj = o as ObjectInstance;
  60. if (obj == null)
  61. {
  62. throw new JavaScriptException(engine.TypeError);
  63. }
  64. if ((obj.HasProperty("value") || obj.HasProperty("writable")) &&
  65. (obj.HasProperty("get") || obj.HasProperty("set")))
  66. {
  67. throw new JavaScriptException(engine.TypeError);
  68. }
  69. bool? writable = obj.HasProperty("writable") ? TypeConverter.ToBoolean(obj.Get("writable")) : default(bool?);
  70. bool? enumerable = obj.HasProperty("enumerable") ? TypeConverter.ToBoolean(obj.Get("enumerable")) : default(bool?);
  71. bool? configurable = obj.HasProperty("configurable") ? TypeConverter.ToBoolean(obj.Get("configurable")) : default(bool?);
  72. PropertyDescriptor desc = new DataDescriptor(Native.Undefined.Instance) { Configurable = configurable, Enumerable = enumerable, Writable = writable };
  73. if (obj.HasProperty("value"))
  74. {
  75. var value = obj.Get("value");
  76. desc = new DataDescriptor(value) { Configurable = configurable, Enumerable = enumerable, Writable = writable};
  77. }
  78. object getter = null, setter = null;
  79. if (obj.HasProperty("get"))
  80. {
  81. getter = obj.Get("get");
  82. if (getter != Native.Undefined.Instance && !(getter is ICallable))
  83. {
  84. throw new JavaScriptException(engine.TypeError);
  85. }
  86. }
  87. if (obj.HasProperty("set"))
  88. {
  89. setter = obj.Get("set");
  90. if (setter != Native.Undefined.Instance && !(setter is ICallable))
  91. {
  92. throw new JavaScriptException(engine.TypeError);
  93. }
  94. }
  95. if (getter != null || setter != null)
  96. {
  97. if (obj.HasProperty("value") || writable != null)
  98. {
  99. throw new JavaScriptException(engine.TypeError);
  100. }
  101. desc = new AccessorDescriptor(getter as ICallable, setter as ICallable) { Configurable = configurable, Enumerable = enumerable };
  102. }
  103. return desc;
  104. }
  105. public static object FromPropertyDescriptor(Engine engine, PropertyDescriptor desc)
  106. {
  107. if (desc == PropertyDescriptor.Undefined)
  108. {
  109. return Native.Undefined.Instance;
  110. }
  111. var obj = engine.Object.Construct(Arguments.Empty);
  112. if (desc.IsDataDescriptor())
  113. {
  114. var datadesc = desc.As<DataDescriptor>();
  115. obj.DefineOwnProperty("value", new DataDescriptor(datadesc.Value) { Writable = true, Enumerable = true, Configurable = true }, false);
  116. obj.DefineOwnProperty("writable", new DataDescriptor(datadesc.WritableIsSet) { Writable = true, Enumerable = true, Configurable = true }, false);
  117. }
  118. else
  119. {
  120. var accdesc = desc.As<AccessorDescriptor>();
  121. obj.DefineOwnProperty("get", new DataDescriptor(accdesc.Get ?? Native.Undefined.Instance) { Writable = true, Enumerable = true, Configurable = true }, false);
  122. obj.DefineOwnProperty("set", new DataDescriptor(accdesc.Set ?? Native.Undefined.Instance) { Writable = true, Enumerable = true, Configurable = true }, false);
  123. }
  124. obj.DefineOwnProperty("enumerable", new DataDescriptor(desc.EnumerableIsSet) { Writable = true, Enumerable = true, Configurable = true }, false);
  125. obj.DefineOwnProperty("configurable", new DataDescriptor(desc.ConfigurableIsSetToTrue) { Writable = true, Enumerable = true, Configurable = true }, false);
  126. return obj;
  127. }
  128. /// <summary>
  129. /// Local implementation used to create a singleton representing
  130. /// an undefined result of a PropertyDescriptor. This prevents the rest
  131. /// of the code to return 'object' in order to be able to return
  132. /// Undefined.Instance
  133. /// </summary>
  134. internal sealed class UndefinedPropertyDescriptor : PropertyDescriptor
  135. {
  136. public override bool IsAccessorDescriptor()
  137. {
  138. return false;
  139. }
  140. public override bool IsDataDescriptor()
  141. {
  142. return false;
  143. }
  144. }
  145. }
  146. }