PropertyDescriptor.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "PropertyDescriptor.h"
  27. #include "GetterSetter.h"
  28. #include "JSObject.h"
  29. #include "Operations.h"
  30. namespace JSC {
  31. unsigned PropertyDescriptor::defaultAttributes = (DontDelete << 1) - 1;
  32. bool PropertyDescriptor::writable() const
  33. {
  34. ASSERT(!isAccessorDescriptor());
  35. return !(m_attributes & ReadOnly);
  36. }
  37. bool PropertyDescriptor::enumerable() const
  38. {
  39. return !(m_attributes & DontEnum);
  40. }
  41. bool PropertyDescriptor::configurable() const
  42. {
  43. return !(m_attributes & DontDelete);
  44. }
  45. bool PropertyDescriptor::isDataDescriptor() const
  46. {
  47. return m_value || (m_seenAttributes & WritablePresent);
  48. }
  49. bool PropertyDescriptor::isGenericDescriptor() const
  50. {
  51. return !isAccessorDescriptor() && !isDataDescriptor();
  52. }
  53. bool PropertyDescriptor::isAccessorDescriptor() const
  54. {
  55. return m_getter || m_setter;
  56. }
  57. void PropertyDescriptor::setUndefined()
  58. {
  59. m_value = jsUndefined();
  60. m_attributes = ReadOnly | DontDelete | DontEnum;
  61. }
  62. JSValue PropertyDescriptor::getter() const
  63. {
  64. ASSERT(isAccessorDescriptor());
  65. return m_getter;
  66. }
  67. JSValue PropertyDescriptor::setter() const
  68. {
  69. ASSERT(isAccessorDescriptor());
  70. return m_setter;
  71. }
  72. JSObject* PropertyDescriptor::getterObject() const
  73. {
  74. ASSERT(isAccessorDescriptor() && getterPresent());
  75. return m_getter.isObject() ? asObject(m_getter) : 0;
  76. }
  77. JSObject* PropertyDescriptor::setterObject() const
  78. {
  79. ASSERT(isAccessorDescriptor() && setterPresent());
  80. return m_setter.isObject() ? asObject(m_setter) : 0;
  81. }
  82. void PropertyDescriptor::setDescriptor(JSValue value, unsigned attributes)
  83. {
  84. ASSERT(value);
  85. ASSERT(value.isGetterSetter() == !!(attributes & Accessor));
  86. m_attributes = attributes;
  87. if (value.isGetterSetter()) {
  88. m_attributes &= ~ReadOnly; // FIXME: we should be able to ASSERT this!
  89. GetterSetter* accessor = asGetterSetter(value);
  90. m_getter = accessor->getter() ? accessor->getter() : jsUndefined();
  91. m_setter = accessor->setter() ? accessor->setter() : jsUndefined();
  92. m_seenAttributes = EnumerablePresent | ConfigurablePresent;
  93. } else {
  94. m_value = value;
  95. m_seenAttributes = EnumerablePresent | ConfigurablePresent | WritablePresent;
  96. }
  97. }
  98. void PropertyDescriptor::setAccessorDescriptor(GetterSetter* accessor, unsigned attributes)
  99. {
  100. ASSERT(attributes & Accessor);
  101. attributes &= ~ReadOnly; // FIXME: we should be able to ASSERT this!
  102. m_attributes = attributes;
  103. m_getter = accessor->getter() ? accessor->getter() : jsUndefined();
  104. m_setter = accessor->setter() ? accessor->setter() : jsUndefined();
  105. m_seenAttributes = EnumerablePresent | ConfigurablePresent;
  106. }
  107. void PropertyDescriptor::setWritable(bool writable)
  108. {
  109. if (writable)
  110. m_attributes &= ~ReadOnly;
  111. else
  112. m_attributes |= ReadOnly;
  113. m_seenAttributes |= WritablePresent;
  114. }
  115. void PropertyDescriptor::setEnumerable(bool enumerable)
  116. {
  117. if (enumerable)
  118. m_attributes &= ~DontEnum;
  119. else
  120. m_attributes |= DontEnum;
  121. m_seenAttributes |= EnumerablePresent;
  122. }
  123. void PropertyDescriptor::setConfigurable(bool configurable)
  124. {
  125. if (configurable)
  126. m_attributes &= ~DontDelete;
  127. else
  128. m_attributes |= DontDelete;
  129. m_seenAttributes |= ConfigurablePresent;
  130. }
  131. void PropertyDescriptor::setSetter(JSValue setter)
  132. {
  133. m_setter = setter;
  134. m_attributes |= Accessor;
  135. m_attributes &= ~ReadOnly;
  136. }
  137. void PropertyDescriptor::setGetter(JSValue getter)
  138. {
  139. m_getter = getter;
  140. m_attributes |= Accessor;
  141. m_attributes &= ~ReadOnly;
  142. }
  143. // See ES5.1 9.12
  144. bool sameValue(ExecState* exec, JSValue a, JSValue b)
  145. {
  146. if (!a.isNumber())
  147. return JSValue::strictEqual(exec, a, b);
  148. if (!b.isNumber())
  149. return false;
  150. double x = a.asNumber();
  151. double y = b.asNumber();
  152. if (std::isnan(x))
  153. return std::isnan(y);
  154. return bitwise_cast<uint64_t>(x) == bitwise_cast<uint64_t>(y);
  155. }
  156. bool PropertyDescriptor::equalTo(ExecState* exec, const PropertyDescriptor& other) const
  157. {
  158. if (!other.m_value == m_value ||
  159. !other.m_getter == m_getter ||
  160. !other.m_setter == m_setter)
  161. return false;
  162. return (!m_value || sameValue(exec, other.m_value, m_value))
  163. && (!m_getter || JSValue::strictEqual(exec, other.m_getter, m_getter))
  164. && (!m_setter || JSValue::strictEqual(exec, other.m_setter, m_setter))
  165. && attributesEqual(other);
  166. }
  167. bool PropertyDescriptor::attributesEqual(const PropertyDescriptor& other) const
  168. {
  169. unsigned mismatch = other.m_attributes ^ m_attributes;
  170. unsigned sharedSeen = other.m_seenAttributes & m_seenAttributes;
  171. if (sharedSeen & WritablePresent && mismatch & ReadOnly)
  172. return false;
  173. if (sharedSeen & ConfigurablePresent && mismatch & DontDelete)
  174. return false;
  175. if (sharedSeen & EnumerablePresent && mismatch & DontEnum)
  176. return false;
  177. return true;
  178. }
  179. unsigned PropertyDescriptor::attributesOverridingCurrent(const PropertyDescriptor& current) const
  180. {
  181. unsigned currentAttributes = current.m_attributes;
  182. if (isDataDescriptor() && current.isAccessorDescriptor())
  183. currentAttributes |= ReadOnly;
  184. unsigned overrideMask = 0;
  185. if (writablePresent())
  186. overrideMask |= ReadOnly;
  187. if (enumerablePresent())
  188. overrideMask |= DontEnum;
  189. if (configurablePresent())
  190. overrideMask |= DontDelete;
  191. if (isAccessorDescriptor())
  192. overrideMask |= Accessor;
  193. return (m_attributes & overrideMask) | (currentAttributes & ~overrideMask);
  194. }
  195. }