ClassInfo.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2001 Peter Kelly (pmk@post.com)
  4. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this library; see the file COPYING.LIB. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. #ifndef ClassInfo_h
  23. #define ClassInfo_h
  24. #include "CallFrame.h"
  25. #include "ConstructData.h"
  26. #include "JSCell.h"
  27. #if ENABLE(DETACHED_JIT)
  28. #include "JSCBridge.h"
  29. #endif
  30. namespace JSC {
  31. class HashEntry;
  32. struct HashTable;
  33. struct MethodTable {
  34. typedef void (*DestroyFunctionPtr)(JSCell*);
  35. DestroyFunctionPtr destroy;
  36. typedef void (*VisitChildrenFunctionPtr)(JSCell*, SlotVisitor&);
  37. VisitChildrenFunctionPtr visitChildren;
  38. typedef void (*CopyBackingStoreFunctionPtr)(JSCell*, CopyVisitor&);
  39. CopyBackingStoreFunctionPtr copyBackingStore;
  40. typedef CallType (*GetCallDataFunctionPtr)(JSCell*, CallData&);
  41. GetCallDataFunctionPtr getCallData;
  42. typedef ConstructType (*GetConstructDataFunctionPtr)(JSCell*, ConstructData&);
  43. GetConstructDataFunctionPtr getConstructData;
  44. typedef void (*PutFunctionPtr)(JSCell*, ExecState*, PropertyName propertyName, JSValue, PutPropertySlot&);
  45. PutFunctionPtr put;
  46. typedef void (*PutByIndexFunctionPtr)(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
  47. PutByIndexFunctionPtr putByIndex;
  48. typedef bool (*DeletePropertyFunctionPtr)(JSCell*, ExecState*, PropertyName);
  49. DeletePropertyFunctionPtr deleteProperty;
  50. typedef bool (*DeletePropertyByIndexFunctionPtr)(JSCell*, ExecState*, unsigned);
  51. DeletePropertyByIndexFunctionPtr deletePropertyByIndex;
  52. typedef bool (*GetOwnPropertySlotFunctionPtr)(JSCell*, ExecState*, PropertyName, PropertySlot&);
  53. GetOwnPropertySlotFunctionPtr getOwnPropertySlot;
  54. typedef bool (*GetOwnPropertySlotByIndexFunctionPtr)(JSCell*, ExecState*, unsigned, PropertySlot&);
  55. GetOwnPropertySlotByIndexFunctionPtr getOwnPropertySlotByIndex;
  56. typedef JSObject* (*ToThisObjectFunctionPtr)(JSCell*, ExecState*);
  57. ToThisObjectFunctionPtr toThisObject;
  58. typedef JSValue (*DefaultValueFunctionPtr)(const JSObject*, ExecState*, PreferredPrimitiveType);
  59. DefaultValueFunctionPtr defaultValue;
  60. typedef void (*GetOwnPropertyNamesFunctionPtr)(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  61. GetOwnPropertyNamesFunctionPtr getOwnPropertyNames;
  62. typedef void (*GetOwnNonIndexPropertyNamesFunctionPtr)(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  63. GetOwnNonIndexPropertyNamesFunctionPtr getOwnNonIndexPropertyNames;
  64. typedef void (*GetPropertyNamesFunctionPtr)(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  65. GetPropertyNamesFunctionPtr getPropertyNames;
  66. typedef String (*ClassNameFunctionPtr)(const JSObject*);
  67. ClassNameFunctionPtr className;
  68. typedef bool (*CustomHasInstanceFunctionPtr)(JSObject*, ExecState*, JSValue);
  69. CustomHasInstanceFunctionPtr customHasInstance;
  70. typedef void (*PutWithAttributesFunctionPtr)(JSObject*, ExecState*, PropertyName propertyName, JSValue, unsigned attributes);
  71. PutWithAttributesFunctionPtr putDirectVirtual;
  72. typedef bool (*DefineOwnPropertyFunctionPtr)(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool);
  73. DefineOwnPropertyFunctionPtr defineOwnProperty;
  74. typedef bool (*GetOwnPropertyDescriptorFunctionPtr)(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
  75. GetOwnPropertyDescriptorFunctionPtr getOwnPropertyDescriptor;
  76. };
  77. #define CREATE_MEMBER_CHECKER(member) \
  78. template <typename T> \
  79. struct MemberCheck##member { \
  80. struct Fallback { \
  81. void member(...); \
  82. }; \
  83. struct Derived : T, Fallback { }; \
  84. template <typename U, U> struct Check; \
  85. typedef char Yes[2]; \
  86. typedef char No[1]; \
  87. template <typename U> \
  88. static No &func(Check<void (Fallback::*)(...), &U::member>*); \
  89. template <typename U> \
  90. static Yes &func(...); \
  91. enum { has = sizeof(func<Derived>(0)) == sizeof(Yes) }; \
  92. }
  93. #define HAS_MEMBER_NAMED(klass, name) (MemberCheck##name<klass>::has)
  94. #define CREATE_METHOD_TABLE(ClassName) { \
  95. &ClassName::destroy, \
  96. &ClassName::visitChildren, \
  97. &ClassName::copyBackingStore, \
  98. &ClassName::getCallData, \
  99. &ClassName::getConstructData, \
  100. &ClassName::put, \
  101. &ClassName::putByIndex, \
  102. &ClassName::deleteProperty, \
  103. &ClassName::deletePropertyByIndex, \
  104. &ClassName::getOwnPropertySlot, \
  105. &ClassName::getOwnPropertySlotByIndex, \
  106. &ClassName::toThisObject, \
  107. &ClassName::defaultValue, \
  108. &ClassName::getOwnPropertyNames, \
  109. &ClassName::getOwnNonIndexPropertyNames, \
  110. &ClassName::getPropertyNames, \
  111. &ClassName::className, \
  112. &ClassName::customHasInstance, \
  113. &ClassName::putDirectVirtual, \
  114. &ClassName::defineOwnProperty, \
  115. &ClassName::getOwnPropertyDescriptor, \
  116. }, \
  117. ClassName::TypedArrayStorageType
  118. struct ClassInfo {
  119. /**
  120. * A string denoting the class name. Example: "Window".
  121. */
  122. const char* className;
  123. /**
  124. * Pointer to the class information of the base class.
  125. * 0L if there is none.
  126. */
  127. const ClassInfo* parentClass;
  128. /**
  129. * Static hash-table of properties.
  130. * For classes that can be used from multiple threads, it is accessed via a getter function that would typically return a pointer to thread-specific value.
  131. */
  132. const HashTable* propHashTable(ExecState* exec) const
  133. {
  134. if (classPropHashTableGetterFunction)
  135. return classPropHashTableGetterFunction(exec);
  136. return staticPropHashTable;
  137. }
  138. bool isSubClassOf(const ClassInfo* other) const
  139. {
  140. #if ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT
  141. JSCBridge & bridge(*JSCBridge::sharedInstance());
  142. ClassInfo const * me(bridge.mapFromVMToCompiler(this));
  143. other = bridge.mapFromVMToCompiler(other);
  144. for (const ClassInfo* ci = me; ci; ci = ci->parentClass) {
  145. ci = ci ? bridge.mapFromVMToCompiler(ci) : ci;
  146. if (ci == other)
  147. return true;
  148. }
  149. return false;
  150. #else
  151. for (const ClassInfo* ci = this; ci; ci = ci->parentClass) {
  152. if (ci == other)
  153. return true;
  154. }
  155. return false;
  156. #endif
  157. }
  158. bool hasStaticProperties() const
  159. {
  160. for (const ClassInfo* ci = this; ci; ci = ci->parentClass) {
  161. if (ci->staticPropHashTable || ci->classPropHashTableGetterFunction)
  162. return true;
  163. }
  164. return false;
  165. }
  166. const HashTable* staticPropHashTable;
  167. typedef const HashTable* (*ClassPropHashTableGetterFunction)(ExecState*);
  168. const ClassPropHashTableGetterFunction classPropHashTableGetterFunction;
  169. MethodTable methodTable;
  170. TypedArrayType typedArrayStorageType;
  171. #if ENABLE(DETACHED_JIT)
  172. ClassInfo(
  173. const char* className,
  174. const ClassInfo* parentClass,
  175. const HashTable* staticPropHashTable,
  176. ClassPropHashTableGetterFunction classPropHashTableGetterFunction,
  177. MethodTable const & methodTable,
  178. TypedArrayType typedArrayStorageType)
  179. : className(className)
  180. , parentClass(parentClass)
  181. , staticPropHashTable(staticPropHashTable)
  182. , classPropHashTableGetterFunction(classPropHashTableGetterFunction)
  183. , methodTable(methodTable)
  184. , typedArrayStorageType(typedArrayStorageType)
  185. {
  186. JSCBridge::registerRuntimeClassinfo(this);
  187. }
  188. private:
  189. ClassInfo(ClassInfo const & other);
  190. ClassInfo & operator=(ClassInfo const & other);
  191. #endif
  192. };
  193. } // namespace JSC
  194. #endif // ClassInfo_h