]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSCell.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSCell.h
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
f9bf01c6 4 * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Apple Inc. All rights reserved.
9dae56ea
A
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
14957cd0 26#include "CallData.h"
14957cd0 27#include "ConstructData.h"
ed1e77d3 28#include "EnumerationMode.h"
14957cd0 29#include "Heap.h"
81345200 30#include "IndexingType.h"
14957cd0 31#include "JSLock.h"
81345200 32#include "JSTypeInfo.h"
6fe7ccc8 33#include "SlotVisitor.h"
81345200 34#include "TypedArrayType.h"
14957cd0 35#include "WriteBarrier.h"
f9bf01c6 36#include <wtf/Noncopyable.h>
9dae56ea
A
37
38namespace JSC {
39
93a37866
A
40class CopyVisitor;
41class ExecState;
ed1e77d3 42class Identifier;
81345200 43class JSArrayBufferView;
93a37866
A
44class JSDestructibleObject;
45class JSGlobalObject;
46class LLIntOffsetsExtractor;
47class PropertyDescriptor;
48class PropertyNameArray;
49class Structure;
50
81345200
A
51template<typename T> void* allocateCell(Heap&);
52template<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
93a37866
A
66class 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
72public:
73 static const unsigned StructureFlags = 0;
74
75 static const bool needsDestruction = false;
ed1e77d3
A
76
77 static JSCell* seenMultipleCalleeObjects() { return bitwise_cast<JSCell*>(static_cast<uintptr_t>(1)); }
93a37866
A
78
79 enum CreatingEarlyCellTag { CreatingEarlyCell };
80 JSCell(CreatingEarlyCellTag);
81
82protected:
83 JSCell(VM&, Structure*);
84 JS_EXPORT_PRIVATE static void destroy(JSCell*);
85
86public:
87 // Querying the type.
88 bool isString() const;
ed1e77d3 89 bool isSymbol() const;
93a37866
A
90 bool isObject() const;
91 bool isGetterSetter() const;
81345200 92 bool isCustomGetterSetter() const;
93a37866
A
93 bool isProxy() const;
94 bool inherits(const ClassInfo*) const;
95 bool isAPIValueWrapper() const;
96
81345200
A
97 JSType type() const;
98 IndexingType indexingType() const;
99 StructureID structureID() const { return m_structureID; }
93a37866 100 Structure* structure() const;
81345200 101 Structure* structure(VM&) const;
93a37866 102 void setStructure(VM&, Structure*);
81345200
A
103 void clearStructure() { m_structureID = 0; }
104
105 TypeInfo::InlineTypeFlags inlineTypeFlags() const { return m_flags; }
93a37866 106
81345200 107 const char* className() const;
93a37866 108
ed1e77d3
A
109 VM* vm() const;
110
93a37866
A
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
6fe7ccc8 116
ed1e77d3
A
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.
93a37866
A
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
81345200
A
134 void dump(PrintStream&) const;
135 JS_EXPORT_PRIVATE static void dumpToStream(const JSCell*, PrintStream&);
93a37866 136 static void visitChildren(JSCell*, SlotVisitor&);
81345200 137 JS_EXPORT_PRIVATE static void copyBackingStore(JSCell*, CopyVisitor&, CopyToken);
93a37866
A
138
139 // Object operations, with the toObject operation included.
140 const ClassInfo* classInfo() const;
141 const MethodTable* methodTable() const;
81345200 142 const MethodTable* methodTable(VM&) const;
93a37866
A
143 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
144 static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
9dae56ea 145
93a37866
A
146 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
147 static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
9dae56ea 148
81345200 149 static JSValue toThis(JSCell*, ExecState*, ECMAMode);
9dae56ea 150
93a37866
A
151 void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
152 bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
9dae56ea 153
81345200 154 static bool canUseFastGetOwnProperty(const Structure&);
ed1e77d3 155 JSValue fastGetOwnProperty(VM&, Structure&, PropertyName);
9dae56ea 156
81345200 157 enum GCData : uint8_t {
ed1e77d3
A
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.
81345200
A
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
9dae56ea 175 {
81345200
A
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;
9dae56ea 185 }
81345200 186 bool isRemembered() const { return m_gcData == MarkedAndRemembered; }
9dae56ea 187
81345200 188 static ptrdiff_t structureIDOffset()
9dae56ea 189 {
81345200 190 return OBJECT_OFFSETOF(JSCell, m_structureID);
9dae56ea 191 }
81345200
A
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;
93a37866
A
214protected:
215
216 void finishCreation(VM&);
217 void finishCreation(VM&, Structure*, CreatingEarlyCellTag);
218
93a37866
A
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);
ed1e77d3
A
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
93a37866
A
229 static String className(const JSObject*);
230 JS_EXPORT_PRIVATE static bool customHasInstance(JSObject*, ExecState*, JSValue);
81345200
A
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*);
93a37866
A
236
237private:
238 friend class LLIntOffsetsExtractor;
81345200
A
239
240 StructureID m_structureID;
241 IndexingType m_indexingType;
242 JSType m_type;
243 TypeInfo::InlineTypeFlags m_flags;
244 uint8_t m_gcData;
93a37866
A
245};
246
247template<typename To, typename From>
248inline To jsCast(From* from)
249{
ed1e77d3 250 ASSERT_WITH_SECURITY_IMPLICATION(!from || from->JSCell::inherits(std::remove_pointer<To>::type::info()));
93a37866
A
251 return static_cast<To>(from);
252}
14957cd0 253
93a37866
A
254template<typename To>
255inline To jsCast(JSValue from)
256{
ed1e77d3 257 ASSERT_WITH_SECURITY_IMPLICATION(from.isCell() && from.asCell()->JSCell::inherits(std::remove_pointer<To>::type::info()));
93a37866
A
258 return static_cast<To>(from.asCell());
259}
260
261template<typename To, typename From>
262inline To jsDynamicCast(From* from)
263{
81345200
A
264 if (LIKELY(from->inherits(std::remove_pointer<To>::type::info())))
265 return static_cast<To>(from);
266 return nullptr;
93a37866
A
267}
268
269template<typename To>
270inline To jsDynamicCast(JSValue from)
271{
81345200
A
272 if (LIKELY(from.isCell() && from.asCell()->inherits(std::remove_pointer<To>::type::info())))
273 return static_cast<To>(from.asCell());
274 return nullptr;
93a37866 275}
14957cd0 276
9dae56ea
A
277} // namespace JSC
278
279#endif // JSCell_h