PropertySlot.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #ifndef PropertySlot_h
  21. #define PropertySlot_h
  22. #include "JSCJSValue.h"
  23. #include "PropertyName.h"
  24. #include "PropertyOffset.h"
  25. #include "Register.h"
  26. #include <wtf/Assertions.h>
  27. #include <wtf/NotFound.h>
  28. namespace JSC {
  29. class ExecState;
  30. class JSObject;
  31. #define JSC_VALUE_MARKER 0
  32. #define INDEX_GETTER_MARKER reinterpret_cast<GetValueFunc>(2)
  33. #define GETTER_FUNCTION_MARKER reinterpret_cast<GetValueFunc>(3)
  34. class PropertySlot {
  35. public:
  36. enum CachedPropertyType {
  37. Uncacheable,
  38. Getter,
  39. Custom,
  40. Value
  41. };
  42. PropertySlot()
  43. : m_cachedPropertyType(Uncacheable)
  44. {
  45. clearBase();
  46. clearOffset();
  47. clearValue();
  48. }
  49. explicit PropertySlot(const JSValue base)
  50. : m_slotBase(base)
  51. , m_cachedPropertyType(Uncacheable)
  52. {
  53. clearOffset();
  54. clearValue();
  55. }
  56. typedef JSValue (*GetValueFunc)(ExecState*, JSValue slotBase, PropertyName);
  57. typedef JSValue (*GetIndexValueFunc)(ExecState*, JSValue slotBase, unsigned);
  58. JSValue getValue(ExecState* exec, PropertyName propertyName) const
  59. {
  60. if (m_getValue == JSC_VALUE_MARKER)
  61. return m_value;
  62. if (m_getValue == INDEX_GETTER_MARKER)
  63. return m_getIndexValue(exec, slotBase(), index());
  64. if (m_getValue == GETTER_FUNCTION_MARKER)
  65. return functionGetter(exec);
  66. return m_getValue(exec, slotBase(), propertyName);
  67. }
  68. JSValue getValue(ExecState* exec, unsigned propertyName) const
  69. {
  70. if (m_getValue == JSC_VALUE_MARKER)
  71. return m_value;
  72. if (m_getValue == INDEX_GETTER_MARKER)
  73. return m_getIndexValue(exec, m_slotBase, m_data.index);
  74. if (m_getValue == GETTER_FUNCTION_MARKER)
  75. return functionGetter(exec);
  76. return m_getValue(exec, slotBase(), Identifier::from(exec, propertyName));
  77. }
  78. CachedPropertyType cachedPropertyType() const { return m_cachedPropertyType; }
  79. bool isCacheable() const { return m_cachedPropertyType != Uncacheable; }
  80. bool isCacheableValue() const { return m_cachedPropertyType == Value; }
  81. PropertyOffset cachedOffset() const
  82. {
  83. ASSERT(isCacheable());
  84. return m_offset;
  85. }
  86. void setValue(JSValue slotBase, JSValue value)
  87. {
  88. ASSERT(value);
  89. clearOffset();
  90. m_getValue = JSC_VALUE_MARKER;
  91. m_slotBase = slotBase;
  92. m_value = value;
  93. }
  94. void setValue(JSValue slotBase, JSValue value, PropertyOffset offset)
  95. {
  96. ASSERT(value);
  97. m_getValue = JSC_VALUE_MARKER;
  98. m_slotBase = slotBase;
  99. m_value = value;
  100. m_offset = offset;
  101. m_cachedPropertyType = Value;
  102. }
  103. void setValue(JSValue value)
  104. {
  105. ASSERT(value);
  106. clearBase();
  107. clearOffset();
  108. m_getValue = JSC_VALUE_MARKER;
  109. m_value = value;
  110. }
  111. void setCustom(JSValue slotBase, GetValueFunc getValue)
  112. {
  113. ASSERT(slotBase);
  114. ASSERT(getValue);
  115. m_getValue = getValue;
  116. m_getIndexValue = 0;
  117. m_slotBase = slotBase;
  118. }
  119. void setCacheableCustom(JSValue slotBase, GetValueFunc getValue)
  120. {
  121. ASSERT(slotBase);
  122. ASSERT(getValue);
  123. m_getValue = getValue;
  124. m_getIndexValue = 0;
  125. m_slotBase = slotBase;
  126. m_cachedPropertyType = Custom;
  127. }
  128. void setCustomIndex(JSValue slotBase, unsigned index, GetIndexValueFunc getIndexValue)
  129. {
  130. ASSERT(slotBase);
  131. ASSERT(getIndexValue);
  132. m_getValue = INDEX_GETTER_MARKER;
  133. m_getIndexValue = getIndexValue;
  134. m_slotBase = slotBase;
  135. m_data.index = index;
  136. }
  137. void setGetterSlot(JSObject* getterFunc)
  138. {
  139. ASSERT(getterFunc);
  140. m_thisValue = m_slotBase;
  141. m_getValue = GETTER_FUNCTION_MARKER;
  142. m_data.getterFunc = getterFunc;
  143. }
  144. void setCacheableGetterSlot(JSValue slotBase, JSObject* getterFunc, PropertyOffset offset)
  145. {
  146. ASSERT(getterFunc);
  147. m_getValue = GETTER_FUNCTION_MARKER;
  148. m_thisValue = m_slotBase;
  149. m_slotBase = slotBase;
  150. m_data.getterFunc = getterFunc;
  151. m_offset = offset;
  152. m_cachedPropertyType = Getter;
  153. }
  154. void setUndefined()
  155. {
  156. setValue(jsUndefined());
  157. }
  158. JSValue slotBase() const
  159. {
  160. return m_slotBase;
  161. }
  162. void setBase(JSValue base)
  163. {
  164. ASSERT(m_slotBase);
  165. ASSERT(base);
  166. m_slotBase = base;
  167. }
  168. void clearBase()
  169. {
  170. #ifndef NDEBUG
  171. m_slotBase = JSValue();
  172. #endif
  173. }
  174. void clearValue()
  175. {
  176. #ifndef NDEBUG
  177. m_value = JSValue();
  178. #endif
  179. }
  180. void clearOffset()
  181. {
  182. // Clear offset even in release builds, in case this PropertySlot has been used before.
  183. // (For other data members, we don't need to clear anything because reuse would meaningfully overwrite them.)
  184. m_offset = invalidOffset;
  185. m_cachedPropertyType = Uncacheable;
  186. }
  187. unsigned index() const { return m_data.index; }
  188. GetValueFunc customGetter() const
  189. {
  190. ASSERT(m_cachedPropertyType == Custom);
  191. return m_getValue;
  192. }
  193. private:
  194. JS_EXPORT_PRIVATE JSValue functionGetter(ExecState*) const;
  195. GetValueFunc m_getValue;
  196. GetIndexValueFunc m_getIndexValue;
  197. JSValue m_slotBase;
  198. union {
  199. JSObject* getterFunc;
  200. unsigned index;
  201. } m_data;
  202. JSValue m_value;
  203. JSValue m_thisValue;
  204. PropertyOffset m_offset;
  205. CachedPropertyType m_cachedPropertyType;
  206. };
  207. } // namespace JSC
  208. #endif // PropertySlot_h