]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSCell.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSCell.h
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, 2007, 2008, 2009 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
23 #ifndef JSCell_h
24 #define JSCell_h
25
26 #include "CallData.h"
27 #include "ConstructData.h"
28 #include "EnumerationMode.h"
29 #include "Heap.h"
30 #include "IndexingType.h"
31 #include "JSLock.h"
32 #include "JSTypeInfo.h"
33 #include "SlotVisitor.h"
34 #include "TypedArrayType.h"
35 #include "WriteBarrier.h"
36 #include <wtf/Noncopyable.h>
37
38 namespace JSC {
39
40 class CopyVisitor;
41 class ExecState;
42 class Identifier;
43 class JSArrayBufferView;
44 class JSDestructibleObject;
45 class JSGlobalObject;
46 class LLIntOffsetsExtractor;
47 class PropertyDescriptor;
48 class PropertyNameArray;
49 class Structure;
50
51 template<typename T> void* allocateCell(Heap&);
52 template<typename T> void* allocateCell(Heap&, size_t);
53
54 #define DECLARE_EXPORT_INFO \
55 protected: \
56 static JS_EXPORTDATA const ::JSC::ClassInfo s_info; \
57 public: \
58 static const ::JSC::ClassInfo* info() { return &s_info; }
59
60 #define DECLARE_INFO \
61 protected: \
62 static const ::JSC::ClassInfo s_info; \
63 public: \
64 static const ::JSC::ClassInfo* info() { return &s_info; }
65
66 class JSCell {
67 friend class JSValue;
68 friend class MarkedBlock;
69 template<typename T> friend void* allocateCell(Heap&);
70 template<typename T> friend void* allocateCell(Heap&, size_t);
71
72 public:
73 static const unsigned StructureFlags = 0;
74
75 static const bool needsDestruction = false;
76
77 static JSCell* seenMultipleCalleeObjects() { return bitwise_cast<JSCell*>(static_cast<uintptr_t>(1)); }
78
79 enum CreatingEarlyCellTag { CreatingEarlyCell };
80 JSCell(CreatingEarlyCellTag);
81
82 protected:
83 JSCell(VM&, Structure*);
84 JS_EXPORT_PRIVATE static void destroy(JSCell*);
85
86 public:
87 // Querying the type.
88 bool isString() const;
89 bool isSymbol() const;
90 bool isObject() const;
91 bool isGetterSetter() const;
92 bool isCustomGetterSetter() const;
93 bool isProxy() const;
94 bool inherits(const ClassInfo*) const;
95 bool isAPIValueWrapper() const;
96
97 JSType type() const;
98 IndexingType indexingType() const;
99 StructureID structureID() const { return m_structureID; }
100 Structure* structure() const;
101 Structure* structure(VM&) const;
102 void setStructure(VM&, Structure*);
103 void clearStructure() { m_structureID = 0; }
104
105 TypeInfo::InlineTypeFlags inlineTypeFlags() const { return m_flags; }
106
107 const char* className() const;
108
109 VM* vm() const;
110
111 // Extracting the value.
112 JS_EXPORT_PRIVATE bool getString(ExecState*, String&) const;
113 JS_EXPORT_PRIVATE String getString(ExecState*) const; // null string if not a string
114 JS_EXPORT_PRIVATE JSObject* getObject(); // NULL if not an object
115 const JSObject* getObject() const; // NULL if not an object
116
117 // Returns information about how to call/construct this cell as a function/constructor. May tell
118 // you that the cell is not callable or constructor (default is that it's not either). If it
119 // says that the function is callable, and the TypeOfShouldCallGetCallData type flag is set, and
120 // this is an object, then typeof will return "function" instead of "object". These methods
121 // cannot change their minds and must be thread-safe. They are sometimes called from compiler
122 // threads.
123 JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&);
124 JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&);
125
126 // Basic conversions.
127 JS_EXPORT_PRIVATE JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
128 bool getPrimitiveNumber(ExecState*, double& number, JSValue&) const;
129 bool toBoolean(ExecState*) const;
130 TriState pureToBoolean() const;
131 JS_EXPORT_PRIVATE double toNumber(ExecState*) const;
132 JS_EXPORT_PRIVATE JSObject* toObject(ExecState*, JSGlobalObject*) const;
133
134 void dump(PrintStream&) const;
135 JS_EXPORT_PRIVATE static void dumpToStream(const JSCell*, PrintStream&);
136 static void visitChildren(JSCell*, SlotVisitor&);
137 JS_EXPORT_PRIVATE static void copyBackingStore(JSCell*, CopyVisitor&, CopyToken);
138
139 // Object operations, with the toObject operation included.
140 const ClassInfo* classInfo() const;
141 const MethodTable* methodTable() const;
142 const MethodTable* methodTable(VM&) const;
143 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
144 static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
145
146 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
147 static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
148
149 static JSValue toThis(JSCell*, ExecState*, ECMAMode);
150
151 void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
152 bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
153
154 static bool canUseFastGetOwnProperty(const Structure&);
155 JSValue fastGetOwnProperty(VM&, Structure&, PropertyName);
156
157 enum GCData : uint8_t {
158 Marked = 0, // The object has survived a GC and is in the old gen.
159 NotMarked = 1, // The object is new and in the eden gen.
160 MarkedAndRemembered = 2, // The object is in the GC's remembered set.
161
162 // The object being in the GC's remembered set implies that it is also
163 // Marked. This is because objects are only added to the remembered sets
164 // by write barriers, and write barriers are only interested in old gen
165 // objects that point to potential eden gen objects.
166 };
167
168 void setMarked() { m_gcData = Marked; }
169 void setRemembered(bool remembered)
170 {
171 ASSERT(m_gcData == (remembered ? Marked : MarkedAndRemembered));
172 m_gcData = remembered ? MarkedAndRemembered : Marked;
173 }
174 bool isMarked() const
175 {
176 switch (m_gcData) {
177 case Marked:
178 case MarkedAndRemembered:
179 return true;
180 case NotMarked:
181 return false;
182 }
183 RELEASE_ASSERT_NOT_REACHED();
184 return false;
185 }
186 bool isRemembered() const { return m_gcData == MarkedAndRemembered; }
187
188 static ptrdiff_t structureIDOffset()
189 {
190 return OBJECT_OFFSETOF(JSCell, m_structureID);
191 }
192
193 static ptrdiff_t typeInfoFlagsOffset()
194 {
195 return OBJECT_OFFSETOF(JSCell, m_flags);
196 }
197
198 static ptrdiff_t typeInfoTypeOffset()
199 {
200 return OBJECT_OFFSETOF(JSCell, m_type);
201 }
202
203 static ptrdiff_t indexingTypeOffset()
204 {
205 return OBJECT_OFFSETOF(JSCell, m_indexingType);
206 }
207
208 static ptrdiff_t gcDataOffset()
209 {
210 return OBJECT_OFFSETOF(JSCell, m_gcData);
211 }
212
213 static const TypedArrayType TypedArrayStorageType = NotTypedArray;
214 protected:
215
216 void finishCreation(VM&);
217 void finishCreation(VM&, Structure*, CreatingEarlyCellTag);
218
219 // Dummy implementations of override-able static functions for classes to put in their MethodTable
220 static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
221 static NO_RETURN_DUE_TO_CRASH void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
222 static NO_RETURN_DUE_TO_CRASH void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
223 static NO_RETURN_DUE_TO_CRASH void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
224
225 static uint32_t getEnumerableLength(ExecState*, JSObject*);
226 static NO_RETURN_DUE_TO_CRASH void getStructurePropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
227 static NO_RETURN_DUE_TO_CRASH void getGenericPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
228
229 static String className(const JSObject*);
230 JS_EXPORT_PRIVATE static bool customHasInstance(JSObject*, ExecState*, JSValue);
231 static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
232 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
233 static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned propertyName, PropertySlot&);
234 JS_EXPORT_PRIVATE static ArrayBuffer* slowDownAndWasteMemory(JSArrayBufferView*);
235 JS_EXPORT_PRIVATE static PassRefPtr<ArrayBufferView> getTypedArrayImpl(JSArrayBufferView*);
236
237 private:
238 friend class LLIntOffsetsExtractor;
239
240 StructureID m_structureID;
241 IndexingType m_indexingType;
242 JSType m_type;
243 TypeInfo::InlineTypeFlags m_flags;
244 uint8_t m_gcData;
245 };
246
247 template<typename To, typename From>
248 inline To jsCast(From* from)
249 {
250 ASSERT_WITH_SECURITY_IMPLICATION(!from || from->JSCell::inherits(std::remove_pointer<To>::type::info()));
251 return static_cast<To>(from);
252 }
253
254 template<typename To>
255 inline To jsCast(JSValue from)
256 {
257 ASSERT_WITH_SECURITY_IMPLICATION(from.isCell() && from.asCell()->JSCell::inherits(std::remove_pointer<To>::type::info()));
258 return static_cast<To>(from.asCell());
259 }
260
261 template<typename To, typename From>
262 inline To jsDynamicCast(From* from)
263 {
264 if (LIKELY(from->inherits(std::remove_pointer<To>::type::info())))
265 return static_cast<To>(from);
266 return nullptr;
267 }
268
269 template<typename To>
270 inline To jsDynamicCast(JSValue from)
271 {
272 if (LIKELY(from.isCell() && from.asCell()->inherits(std::remove_pointer<To>::type::info())))
273 return static_cast<To>(from.asCell());
274 return nullptr;
275 }
276
277 } // namespace JSC
278
279 #endif // JSCell_h