weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
oliver@apple.com | 5fca29f | 2009-08-11 04:35:02 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Apple Inc. All rights reserved. |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 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 | |
| 23 | #ifndef JSCell_h |
| 24 | #define JSCell_h |
| 25 | |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 26 | #include "CallData.h" |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 27 | #include "ConstructData.h" |
ggaren@apple.com | a8b3854 | 2011-01-10 23:43:56 +0000 | [diff] [blame] | 28 | #include "Heap.h" |
ggaren@apple.com | 2006bee | 2011-03-01 23:36:53 +0000 | [diff] [blame] | 29 | #include "JSLock.h" |
ggaren@apple.com | 6e1f8c1 | 2011-06-16 22:01:43 +0000 | [diff] [blame] | 30 | #include "SlotVisitor.h" |
fpizlo@apple.com | c14c8d3 | 2012-10-10 02:14:42 +0000 | [diff] [blame] | 31 | #include "TypedArrayDescriptor.h" |
ggaren@apple.com | 046c1e0 | 2011-05-11 02:29:51 +0000 | [diff] [blame] | 32 | #include "WriteBarrier.h" |
darin@apple.com | 8a1a5b5 | 2009-09-04 19:03:33 +0000 | [diff] [blame] | 33 | #include <wtf/Noncopyable.h> |
mhahnenberg@apple.com | 53d4064 | 2012-09-18 16:13:11 +0000 | [diff] [blame] | 34 | #include <wtf/TypeTraits.h> |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 35 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 36 | namespace JSC { |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 37 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 38 | class CopyVisitor; |
fpizlo@apple.com | a4b4cbe | 2013-01-12 04:47:03 +0000 | [diff] [blame] | 39 | class ExecState; |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 40 | class JSDestructibleObject; |
| 41 | class JSGlobalObject; |
| 42 | class LLIntOffsetsExtractor; |
| 43 | class PropertyDescriptor; |
| 44 | class PropertyNameArray; |
| 45 | class Structure; |
mhahnenberg@apple.com | 5726238 | 2011-11-03 00:25:45 +0000 | [diff] [blame] | 46 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 47 | enum EnumerationMode { |
| 48 | ExcludeDontEnumProperties, |
| 49 | IncludeDontEnumProperties |
| 50 | }; |
oliver@apple.com | 3b6dc57 | 2011-03-28 23:39:16 +0000 | [diff] [blame] | 51 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 52 | class JSCell { |
| 53 | friend class JSValue; |
| 54 | friend class MarkedBlock; |
| 55 | template<typename T> friend void* allocateCell(Heap&); |
| 56 | template<typename T> friend void* allocateCell(Heap&, size_t); |
ggaren@apple.com | f9419b7 | 2011-05-26 22:43:07 +0000 | [diff] [blame] | 57 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 58 | public: |
| 59 | static const unsigned StructureFlags = 0; |
ggaren@apple.com | 47e224a | 2012-08-26 03:25:31 +0000 | [diff] [blame] | 60 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 61 | static const bool needsDestruction = false; |
| 62 | static const bool hasImmortalStructure = false; |
mhahnenberg@apple.com | 30738a7 | 2012-10-03 17:51:28 +0000 | [diff] [blame] | 63 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 64 | enum CreatingEarlyCellTag { CreatingEarlyCell }; |
| 65 | JSCell(CreatingEarlyCellTag); |
ggaren@apple.com | 1caf69d | 2011-09-20 01:21:51 +0000 | [diff] [blame] | 66 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 67 | protected: |
| 68 | JSCell(JSGlobalData&, Structure*); |
| 69 | JS_EXPORT_PRIVATE static void destroy(JSCell*); |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 70 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 71 | public: |
| 72 | // Querying the type. |
| 73 | bool isString() const; |
| 74 | bool isObject() const; |
| 75 | bool isGetterSetter() const; |
| 76 | bool isProxy() const; |
| 77 | bool inherits(const ClassInfo*) const; |
| 78 | bool isAPIValueWrapper() const; |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 79 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 80 | Structure* structure() const; |
| 81 | void setStructure(JSGlobalData&, Structure*); |
| 82 | void clearStructure() { m_structure.clear(); } |
weinig@apple.com | 3412bb4 | 2008-09-01 21:22:54 +0000 | [diff] [blame] | 83 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 84 | const char* className(); |
msaboff@apple.com | 9d9eab6 | 2012-06-06 23:11:09 +0000 | [diff] [blame] | 85 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 86 | // Extracting the value. |
| 87 | JS_EXPORT_PRIVATE bool getString(ExecState*, String&) const; |
| 88 | JS_EXPORT_PRIVATE String getString(ExecState*) const; // null string if not a string |
| 89 | JS_EXPORT_PRIVATE JSObject* getObject(); // NULL if not an object |
| 90 | const JSObject* getObject() const; // NULL if not an object |
weinig@apple.com | 0e2d66e | 2008-07-06 05:26:58 +0000 | [diff] [blame] | 91 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 92 | JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&); |
| 93 | JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&); |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 94 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 95 | // Basic conversions. |
| 96 | JS_EXPORT_PRIVATE JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const; |
| 97 | bool getPrimitiveNumber(ExecState*, double& number, JSValue&) const; |
| 98 | bool toBoolean(ExecState*) const; |
| 99 | JS_EXPORT_PRIVATE double toNumber(ExecState*) const; |
| 100 | JS_EXPORT_PRIVATE JSObject* toObject(ExecState*, JSGlobalObject*) const; |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 101 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 102 | static void visitChildren(JSCell*, SlotVisitor&); |
| 103 | JS_EXPORT_PRIVATE static void copyBackingStore(JSCell*, CopyVisitor&); |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 104 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 105 | // Object operations, with the toObject operation included. |
| 106 | const ClassInfo* classInfo() const; |
| 107 | const MethodTable* methodTable() const; |
oliver@apple.com | b6b94a9 | 2013-01-30 01:31:37 +0000 | [diff] [blame] | 108 | const MethodTable* methodTableForDestruction() const; |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 109 | static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); |
| 110 | static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow); |
mhahnenberg@apple.com | 5e2b712 | 2011-10-08 00:06:07 +0000 | [diff] [blame] | 111 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 112 | static bool deleteProperty(JSCell*, ExecState*, PropertyName); |
| 113 | static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName); |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 114 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 115 | static JSObject* toThisObject(JSCell*, ExecState*); |
ggaren@apple.com | b94f6ba | 2011-09-24 22:15:40 +0000 | [diff] [blame] | 116 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 117 | void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; } |
| 118 | bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); } |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 119 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 120 | // FIXME: Rename getOwnPropertySlot to virtualGetOwnPropertySlot, and |
| 121 | // fastGetOwnPropertySlot to getOwnPropertySlot. Callers should always |
| 122 | // call this function, not its slower virtual counterpart. (For integer |
| 123 | // property names, we want a similar interface with appropriate optimizations.) |
| 124 | bool fastGetOwnPropertySlot(ExecState*, PropertyName, PropertySlot&); |
mhahnenberg@apple.com | 871ffe6 | 2013-03-15 21:52:35 +0000 | [diff] [blame] | 125 | JSValue fastGetOwnProperty(ExecState*, const String&); |
mjs@apple.com | bc644c9 | 2010-05-09 11:18:25 +0000 | [diff] [blame] | 126 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 127 | static ptrdiff_t structureOffset() |
oliver@apple.com | 4103716 | 2011-05-14 22:10:01 +0000 | [diff] [blame] | 128 | { |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 129 | return OBJECT_OFFSETOF(JSCell, m_structure); |
commit-queue@webkit.org | 9987883 | 2011-08-24 02:05:33 +0000 | [diff] [blame] | 130 | } |
| 131 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 132 | void* structureAddress() |
commit-queue@webkit.org | 9987883 | 2011-08-24 02:05:33 +0000 | [diff] [blame] | 133 | { |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 134 | return &m_structure; |
| 135 | } |
| 136 | |
oliver@apple.com | 4103716 | 2011-05-14 22:10:01 +0000 | [diff] [blame] | 137 | #if ENABLE(GC_VALIDATION) |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 138 | Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); } |
| 139 | #endif |
| 140 | |
| 141 | static const TypedArrayType TypedArrayStorageType = TypedArrayNone; |
| 142 | protected: |
| 143 | |
| 144 | void finishCreation(JSGlobalData&); |
| 145 | void finishCreation(JSGlobalData&, Structure*, CreatingEarlyCellTag); |
| 146 | |
| 147 | // Base implementation; for non-object classes implements getPropertySlot. |
| 148 | static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&); |
| 149 | static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&); |
| 150 | |
| 151 | // Dummy implementations of override-able static functions for classes to put in their MethodTable |
| 152 | static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType); |
oliver@apple.com | 5598c18 | 2013-01-23 22:25:07 +0000 | [diff] [blame] | 153 | static NO_RETURN_DUE_TO_CRASH void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); |
| 154 | static NO_RETURN_DUE_TO_CRASH void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); |
| 155 | static NO_RETURN_DUE_TO_CRASH void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 156 | static String className(const JSObject*); |
| 157 | JS_EXPORT_PRIVATE static bool customHasInstance(JSObject*, ExecState*, JSValue); |
oliver@apple.com | 5598c18 | 2013-01-23 22:25:07 +0000 | [diff] [blame] | 158 | static NO_RETURN_DUE_TO_CRASH void putDirectVirtual(JSObject*, ExecState*, PropertyName, JSValue, unsigned attributes); |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 159 | static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow); |
| 160 | static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&); |
| 161 | |
| 162 | private: |
| 163 | friend class LLIntOffsetsExtractor; |
| 164 | |
| 165 | WriteBarrier<Structure> m_structure; |
| 166 | }; |
| 167 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 168 | template<typename To, typename From> |
| 169 | inline To jsCast(From* from) |
| 170 | { |
| 171 | ASSERT(!from || from->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info)); |
| 172 | return static_cast<To>(from); |
| 173 | } |
mhahnenberg@apple.com | 30738a7 | 2012-10-03 17:51:28 +0000 | [diff] [blame] | 174 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 175 | template<typename To> |
| 176 | inline To jsCast(JSValue from) |
| 177 | { |
| 178 | ASSERT(from.isCell() && from.asCell()->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info)); |
| 179 | return static_cast<To>(from.asCell()); |
| 180 | } |
barraclough@apple.com | 484a9d3 | 2012-03-22 18:54:50 +0000 | [diff] [blame] | 181 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 182 | template<typename To, typename From> |
| 183 | inline To jsDynamicCast(From* from) |
| 184 | { |
| 185 | return from->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from) : 0; |
| 186 | } |
mhahnenberg@apple.com | b6f11ee | 2011-11-18 22:13:37 +0000 | [diff] [blame] | 187 | |
fpizlo@apple.com | f980ef6 | 2012-11-06 21:13:27 +0000 | [diff] [blame] | 188 | template<typename To> |
| 189 | inline To jsDynamicCast(JSValue from) |
| 190 | { |
| 191 | return from.isCell() && from.asCell()->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from.asCell()) : 0; |
| 192 | } |
barraclough@apple.com | 484a9d3 | 2012-03-22 18:54:50 +0000 | [diff] [blame] | 193 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 194 | } // namespace JSC |
weinig@apple.com | 6a03b4c | 2008-07-01 17:32:44 +0000 | [diff] [blame] | 195 | |
| 196 | #endif // JSCell_h |