]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSCell.h
JavaScriptCore-1097.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 "CallFrame.h"
28 #include "ConstructData.h"
29 #include "Heap.h"
30 #include "JSLock.h"
31 #include "JSValueInlineMethods.h"
32 #include "SlotVisitor.h"
33 #include "WriteBarrier.h"
34 #include <wtf/Noncopyable.h>
35
36 namespace JSC {
37
38 class JSGlobalObject;
39 class LLIntOffsetsExtractor;
40 class PropertyDescriptor;
41 class PropertyNameArray;
42 class Structure;
43
44 enum EnumerationMode {
45 ExcludeDontEnumProperties,
46 IncludeDontEnumProperties
47 };
48
49 enum TypedArrayType {
50 TypedArrayNone,
51 TypedArrayInt8,
52 TypedArrayInt16,
53 TypedArrayInt32,
54 TypedArrayUint8,
55 TypedArrayUint8Clamped,
56 TypedArrayUint16,
57 TypedArrayUint32,
58 TypedArrayFloat32,
59 TypedArrayFloat64
60 };
61
62 class JSCell {
63 friend class JSValue;
64 friend class MarkedBlock;
65 template<typename T> friend void* allocateCell(Heap&);
66
67 public:
68 enum CreatingEarlyCellTag { CreatingEarlyCell };
69 JSCell(CreatingEarlyCellTag);
70
71 protected:
72 JSCell(JSGlobalData&, Structure*);
73 JS_EXPORT_PRIVATE static void destroy(JSCell*);
74
75 public:
76 // Querying the type.
77 bool isString() const;
78 bool isObject() const;
79 bool isGetterSetter() const;
80 bool inherits(const ClassInfo*) const;
81 bool isAPIValueWrapper() const;
82
83 Structure* structure() const;
84 void setStructure(JSGlobalData&, Structure*);
85 void clearStructure() { m_structure.clear(); }
86
87 // Extracting the value.
88 JS_EXPORT_PRIVATE bool getString(ExecState* exec, UString&) const;
89 JS_EXPORT_PRIVATE UString getString(ExecState* exec) const; // null string if not a string
90 JS_EXPORT_PRIVATE JSObject* getObject(); // NULL if not an object
91 const JSObject* getObject() const; // NULL if not an object
92
93 JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&);
94 JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&);
95
96 // Basic conversions.
97 JS_EXPORT_PRIVATE JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
98 bool getPrimitiveNumber(ExecState*, double& number, JSValue&) const;
99 bool toBoolean(ExecState*) const;
100 JS_EXPORT_PRIVATE double toNumber(ExecState*) const;
101 JS_EXPORT_PRIVATE JSObject* toObject(ExecState*, JSGlobalObject*) const;
102
103 static void visitChildren(JSCell*, SlotVisitor&);
104
105 // Object operations, with the toObject operation included.
106 const ClassInfo* classInfo() const;
107 const ClassInfo* validatedClassInfo() const;
108 const MethodTable* methodTable() const;
109 static void put(JSCell*, ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
110 static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
111
112 static bool deleteProperty(JSCell*, ExecState*, const Identifier& propertyName);
113 static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
114
115 static JSObject* toThisObject(JSCell*, ExecState*);
116
117 void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
118 bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
119
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*, const Identifier& propertyName, PropertySlot&);
125 JSValue fastGetOwnProperty(ExecState*, const UString&);
126
127 static ptrdiff_t structureOffset()
128 {
129 return OBJECT_OFFSETOF(JSCell, m_structure);
130 }
131
132 static ptrdiff_t classInfoOffset()
133 {
134 return OBJECT_OFFSETOF(JSCell, m_classInfo);
135 }
136
137 void* structureAddress()
138 {
139 return &m_structure;
140 }
141
142 #if ENABLE(GC_VALIDATION)
143 Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); }
144 #endif
145
146 static const TypedArrayType TypedArrayStorageType = TypedArrayNone;
147 protected:
148
149 void finishCreation(JSGlobalData&);
150 void finishCreation(JSGlobalData&, Structure*, CreatingEarlyCellTag);
151
152 // Base implementation; for non-object classes implements getPropertySlot.
153 static bool getOwnPropertySlot(JSCell*, ExecState*, const Identifier& propertyName, PropertySlot&);
154 static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
155
156 // Dummy implementations of override-able static functions for classes to put in their MethodTable
157 static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
158 static NO_RETURN_DUE_TO_ASSERT void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
159 static NO_RETURN_DUE_TO_ASSERT void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
160 static UString className(const JSObject*);
161 static bool hasInstance(JSObject*, ExecState*, JSValue, JSValue prototypeProperty);
162 static NO_RETURN_DUE_TO_ASSERT void putDirectVirtual(JSObject*, ExecState*, const Identifier& propertyName, JSValue, unsigned attributes);
163 static bool defineOwnProperty(JSObject*, ExecState*, const Identifier& propertyName, PropertyDescriptor&, bool shouldThrow);
164 static bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&);
165
166 private:
167 friend class LLIntOffsetsExtractor;
168
169 const ClassInfo* m_classInfo;
170 WriteBarrier<Structure> m_structure;
171 };
172
173 inline JSCell::JSCell(CreatingEarlyCellTag)
174 {
175 }
176
177 inline void JSCell::finishCreation(JSGlobalData& globalData)
178 {
179 #if ENABLE(GC_VALIDATION)
180 ASSERT(globalData.isInitializingObject());
181 globalData.setInitializingObjectClass(0);
182 #else
183 UNUSED_PARAM(globalData);
184 #endif
185 ASSERT(m_structure);
186 }
187
188 inline Structure* JSCell::structure() const
189 {
190 return m_structure.get();
191 }
192
193 inline const ClassInfo* JSCell::classInfo() const
194 {
195 return m_classInfo;
196 }
197
198 inline void JSCell::visitChildren(JSCell* cell, SlotVisitor& visitor)
199 {
200 visitor.append(&cell->m_structure);
201 }
202
203 // --- JSValue inlines ----------------------------
204
205 inline bool JSValue::isString() const
206 {
207 return isCell() && asCell()->isString();
208 }
209
210 inline bool JSValue::isPrimitive() const
211 {
212 return !isCell() || asCell()->isString();
213 }
214
215 inline bool JSValue::isGetterSetter() const
216 {
217 return isCell() && asCell()->isGetterSetter();
218 }
219
220 inline bool JSValue::isObject() const
221 {
222 return isCell() && asCell()->isObject();
223 }
224
225 inline bool JSValue::getString(ExecState* exec, UString& s) const
226 {
227 return isCell() && asCell()->getString(exec, s);
228 }
229
230 inline UString JSValue::getString(ExecState* exec) const
231 {
232 return isCell() ? asCell()->getString(exec) : UString();
233 }
234
235 template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
236 {
237 return jsValue().getString(exec);
238 }
239
240 inline JSObject* JSValue::getObject() const
241 {
242 return isCell() ? asCell()->getObject() : 0;
243 }
244
245 ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
246 {
247 if (isInt32()) {
248 int32_t i = asInt32();
249 v = static_cast<uint32_t>(i);
250 return i >= 0;
251 }
252 if (isDouble()) {
253 double d = asDouble();
254 v = static_cast<uint32_t>(d);
255 return v == d;
256 }
257 return false;
258 }
259
260 inline JSValue JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
261 {
262 return isCell() ? asCell()->toPrimitive(exec, preferredType) : asValue();
263 }
264
265 inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value)
266 {
267 if (isInt32()) {
268 number = asInt32();
269 value = *this;
270 return true;
271 }
272 if (isDouble()) {
273 number = asDouble();
274 value = *this;
275 return true;
276 }
277 if (isCell())
278 return asCell()->getPrimitiveNumber(exec, number, value);
279 if (isTrue()) {
280 number = 1.0;
281 value = *this;
282 return true;
283 }
284 if (isFalse() || isNull()) {
285 number = 0.0;
286 value = *this;
287 return true;
288 }
289 ASSERT(isUndefined());
290 number = std::numeric_limits<double>::quiet_NaN();
291 value = *this;
292 return true;
293 }
294
295 ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
296 {
297 if (isInt32())
298 return asInt32();
299 if (isDouble())
300 return asDouble();
301 return toNumberSlowCase(exec);
302 }
303
304 inline JSObject* JSValue::toObject(ExecState* exec) const
305 {
306 return isCell() ? asCell()->toObject(exec, exec->lexicalGlobalObject()) : toObjectSlowCase(exec, exec->lexicalGlobalObject());
307 }
308
309 inline JSObject* JSValue::toObject(ExecState* exec, JSGlobalObject* globalObject) const
310 {
311 return isCell() ? asCell()->toObject(exec, globalObject) : toObjectSlowCase(exec, globalObject);
312 }
313
314 #if COMPILER(CLANG)
315 template<class T>
316 struct NeedsDestructor {
317 static const bool value = !__has_trivial_destructor(T);
318 };
319 #else
320 // Write manual specializations for this struct template if you care about non-clang compilers.
321 template<class T>
322 struct NeedsDestructor {
323 static const bool value = true;
324 };
325 #endif
326
327 template<typename T>
328 void* allocateCell(Heap& heap)
329 {
330 #if ENABLE(GC_VALIDATION)
331 ASSERT(!heap.globalData()->isInitializingObject());
332 heap.globalData()->setInitializingObjectClass(&T::s_info);
333 #endif
334 JSCell* result = 0;
335 if (NeedsDestructor<T>::value)
336 result = static_cast<JSCell*>(heap.allocateWithDestructor(sizeof(T)));
337 else {
338 ASSERT(T::s_info.methodTable.destroy == JSCell::destroy);
339 result = static_cast<JSCell*>(heap.allocateWithoutDestructor(sizeof(T)));
340 }
341 result->clearStructure();
342 return result;
343 }
344
345 inline bool isZapped(const JSCell* cell)
346 {
347 return cell->isZapped();
348 }
349
350 template<typename To, typename From>
351 inline To jsCast(From* from)
352 {
353 ASSERT(!from || from->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
354 return static_cast<To>(from);
355 }
356
357 template<typename To>
358 inline To jsCast(JSValue from)
359 {
360 ASSERT(from.isCell() && from.asCell()->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
361 return static_cast<To>(from.asCell());
362 }
363
364 template<typename To, typename From>
365 inline To jsDynamicCast(From* from)
366 {
367 return from->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from) : 0;
368 }
369
370 template<typename To>
371 inline To jsDynamicCast(JSValue from)
372 {
373 return from.isCell() && from.asCell()->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from.asCell()) : 0;
374 }
375
376 } // namespace JSC
377
378 #endif // JSCell_h