JSCellInlines.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2012 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. #ifndef JSCellInlines_h
  26. #define JSCellInlines_h
  27. #include "CallFrame.h"
  28. #include "Handle.h"
  29. #include "JSCell.h"
  30. #include "JSObject.h"
  31. #include "JSString.h"
  32. #include "Structure.h"
  33. namespace JSC {
  34. inline JSCell::JSCell(CreatingEarlyCellTag)
  35. {
  36. }
  37. inline JSCell::JSCell(VM& vm, Structure* structure)
  38. : m_structure(vm, this, structure)
  39. {
  40. }
  41. inline void JSCell::finishCreation(VM& vm)
  42. {
  43. #if ENABLE(GC_VALIDATION)
  44. ASSERT(vm.isInitializingObject());
  45. vm.setInitializingObjectClass(0);
  46. #else
  47. UNUSED_PARAM(vm);
  48. #endif
  49. ASSERT(m_structure);
  50. }
  51. inline void JSCell::finishCreation(VM& vm, Structure* structure, CreatingEarlyCellTag)
  52. {
  53. #if ENABLE(GC_VALIDATION)
  54. ASSERT(vm.isInitializingObject());
  55. vm.setInitializingObjectClass(0);
  56. if (structure)
  57. #endif
  58. m_structure.setEarlyValue(vm, this, structure);
  59. // Very first set of allocations won't have a real structure.
  60. ASSERT(m_structure || !vm.structureStructure);
  61. }
  62. inline Structure* JSCell::structure() const
  63. {
  64. return m_structure.get();
  65. }
  66. inline void JSCell::visitChildren(JSCell* cell, SlotVisitor& visitor)
  67. {
  68. MARK_LOG_PARENT(visitor, cell);
  69. visitor.append(&cell->m_structure);
  70. }
  71. template<typename T>
  72. void* allocateCell(Heap& heap, size_t size)
  73. {
  74. #if (ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  75. ASSERT_NOT_REACHED_BY_DETACHED_JIT();
  76. return NULL;
  77. #else
  78. ASSERT(size >= sizeof(T));
  79. #if ENABLE(GC_VALIDATION)
  80. ASSERT(!heap.vm()->isInitializingObject());
  81. heap.vm()->setInitializingObjectClass(&T::s_info);
  82. #endif
  83. JSCell* result = 0;
  84. if (T::needsDestruction && T::hasImmortalStructure)
  85. result = static_cast<JSCell*>(heap.allocateWithImmortalStructureDestructor(size));
  86. else if (T::needsDestruction)
  87. result = static_cast<JSCell*>(heap.allocateWithNormalDestructor(size));
  88. else
  89. result = static_cast<JSCell*>(heap.allocateWithoutDestructor(size));
  90. result->clearStructure();
  91. return result;
  92. #endif
  93. }
  94. template<typename T>
  95. void* allocateCell(Heap& heap)
  96. {
  97. return allocateCell<T>(heap, sizeof(T));
  98. }
  99. inline bool isZapped(const JSCell* cell)
  100. {
  101. return cell->isZapped();
  102. }
  103. inline bool JSCell::isObject() const
  104. {
  105. return m_structure->isObject();
  106. }
  107. inline bool JSCell::isString() const
  108. {
  109. return m_structure->typeInfo().type() == StringType;
  110. }
  111. inline bool JSCell::isGetterSetter() const
  112. {
  113. return m_structure->typeInfo().type() == GetterSetterType;
  114. }
  115. inline bool JSCell::isProxy() const
  116. {
  117. return structure()->typeInfo().type() == ProxyType;
  118. }
  119. inline bool JSCell::isAPIValueWrapper() const
  120. {
  121. return m_structure->typeInfo().type() == APIValueWrapperType;
  122. }
  123. inline void JSCell::setStructure(VM& vm, Structure* structure)
  124. {
  125. ASSERT(structure->typeInfo().overridesVisitChildren() == this->structure()->typeInfo().overridesVisitChildren());
  126. ASSERT(structure->classInfo() == m_structure->classInfo());
  127. ASSERT(!m_structure
  128. || m_structure->transitionWatchpointSetHasBeenInvalidated()
  129. || m_structure.get() == structure);
  130. m_structure.set(vm, this, structure);
  131. }
  132. inline const MethodTable* JSCell::methodTableForDestruction() const
  133. {
  134. return &classInfo()->methodTable;
  135. }
  136. inline const MethodTable* JSCell::methodTable() const
  137. {
  138. if (Structure* rootStructure = m_structure->structure())
  139. RELEASE_ASSERT(rootStructure == rootStructure->structure());
  140. return &classInfo()->methodTable;
  141. }
  142. inline bool JSCell::inherits(const ClassInfo* info) const
  143. {
  144. return classInfo()->isSubClassOf(info);
  145. }
  146. ALWAYS_INLINE bool JSCell::fastGetOwnPropertySlot(ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  147. {
  148. if (!structure()->typeInfo().overridesGetOwnPropertySlot())
  149. return asObject(this)->inlineGetOwnPropertySlot(exec, propertyName, slot);
  150. return methodTable()->getOwnPropertySlot(this, exec, propertyName, slot);
  151. }
  152. // Fast call to get a property where we may not yet have converted the string to an
  153. // identifier. The first time we perform a property access with a given string, try
  154. // performing the property map lookup without forming an identifier. We detect this
  155. // case by checking whether the hash has yet been set for this string.
  156. ALWAYS_INLINE JSValue JSCell::fastGetOwnProperty(ExecState* exec, const String& name)
  157. {
  158. if (!structure()->typeInfo().overridesGetOwnPropertySlot() && !structure()->hasGetterSetterProperties()) {
  159. PropertyOffset offset = name.impl()->hasHash()
  160. ? structure()->get(exec->vm(), Identifier(exec, name))
  161. : structure()->get(exec->vm(), name);
  162. if (offset != invalidOffset)
  163. return asObject(this)->locationForOffset(offset)->get();
  164. }
  165. return JSValue();
  166. }
  167. inline bool JSCell::toBoolean(ExecState* exec) const
  168. {
  169. if (isString())
  170. return static_cast<const JSString*>(this)->toBoolean();
  171. return !structure()->masqueradesAsUndefined(exec->lexicalGlobalObject());
  172. }
  173. inline TriState JSCell::pureToBoolean() const
  174. {
  175. if (isString())
  176. return static_cast<const JSString*>(this)->toBoolean() ? TrueTriState : FalseTriState;
  177. return MixedTriState;
  178. }
  179. } // namespace JSC
  180. #endif // JSCellInlines_h