]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSCell.h
JavaScriptCore-1218.33.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
A
27#include "ConstructData.h"
28#include "Heap.h"
29#include "JSLock.h"
6fe7ccc8 30#include "SlotVisitor.h"
93a37866 31#include "TypedArrayDescriptor.h"
14957cd0 32#include "WriteBarrier.h"
f9bf01c6 33#include <wtf/Noncopyable.h>
93a37866 34#include <wtf/TypeTraits.h>
9dae56ea
A
35
36namespace JSC {
37
93a37866
A
38class CopyVisitor;
39class ExecState;
40class JSDestructibleObject;
41class JSGlobalObject;
42class LLIntOffsetsExtractor;
43class PropertyDescriptor;
44class PropertyNameArray;
45class Structure;
46
47enum EnumerationMode {
48 ExcludeDontEnumProperties,
49 IncludeDontEnumProperties
50};
51
52class 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);
57
58public:
59 static const unsigned StructureFlags = 0;
60
61 static const bool needsDestruction = false;
62 static const bool hasImmortalStructure = false;
63
64 enum CreatingEarlyCellTag { CreatingEarlyCell };
65 JSCell(CreatingEarlyCellTag);
66
67protected:
68 JSCell(VM&, Structure*);
69 JS_EXPORT_PRIVATE static void destroy(JSCell*);
70
71public:
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;
79
80 Structure* structure() const;
81 void setStructure(VM&, Structure*);
82 void clearStructure() { m_structure.clear(); }
83
84 const char* className();
85
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
6fe7ccc8 91
93a37866
A
92 JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&);
93 JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&);
94
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 TriState pureToBoolean() 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 JS_EXPORT_PRIVATE static void copyBackingStore(JSCell*, CopyVisitor&);
105
106 // Object operations, with the toObject operation included.
107 const ClassInfo* classInfo() const;
108 const MethodTable* methodTable() const;
109 const MethodTable* methodTableForDestruction() const;
110 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
111 static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
9dae56ea 112
93a37866
A
113 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
114 static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
9dae56ea 115
93a37866 116 static JSObject* toThisObject(JSCell*, ExecState*);
9dae56ea 117
93a37866
A
118 void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
119 bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
9dae56ea 120
93a37866
A
121 // FIXME: Rename getOwnPropertySlot to virtualGetOwnPropertySlot, and
122 // fastGetOwnPropertySlot to getOwnPropertySlot. Callers should always
123 // call this function, not its slower virtual counterpart. (For integer
124 // property names, we want a similar interface with appropriate optimizations.)
125 bool fastGetOwnPropertySlot(ExecState*, PropertyName, PropertySlot&);
126 JSValue fastGetOwnProperty(ExecState*, const String&);
9dae56ea 127
93a37866 128 static ptrdiff_t structureOffset()
9dae56ea 129 {
93a37866 130 return OBJECT_OFFSETOF(JSCell, m_structure);
9dae56ea
A
131 }
132
93a37866 133 void* structureAddress()
9dae56ea 134 {
93a37866 135 return &m_structure;
9dae56ea 136 }
93a37866 137
6fe7ccc8 138#if ENABLE(GC_VALIDATION)
93a37866 139 Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); }
6fe7ccc8 140#endif
93a37866
A
141
142 static const TypedArrayType TypedArrayStorageType = TypedArrayNone;
143protected:
144
145 void finishCreation(VM&);
146 void finishCreation(VM&, Structure*, CreatingEarlyCellTag);
147
148 // Base implementation; for non-object classes implements getPropertySlot.
149 static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
150 static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
151
152 // Dummy implementations of override-able static functions for classes to put in their MethodTable
153 static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
154 static NO_RETURN_DUE_TO_CRASH void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
155 static NO_RETURN_DUE_TO_CRASH void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
156 static NO_RETURN_DUE_TO_CRASH void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
157 static String className(const JSObject*);
158 JS_EXPORT_PRIVATE static bool customHasInstance(JSObject*, ExecState*, JSValue);
159 static NO_RETURN_DUE_TO_CRASH void putDirectVirtual(JSObject*, ExecState*, PropertyName, JSValue, unsigned attributes);
160 static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
161 static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
162
163private:
164 friend class LLIntOffsetsExtractor;
165
166 WriteBarrier<Structure> m_structure;
167};
168
169template<typename To, typename From>
170inline To jsCast(From* from)
171{
172 ASSERT(!from || from->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
173 return static_cast<To>(from);
174}
14957cd0 175
93a37866
A
176template<typename To>
177inline To jsCast(JSValue from)
178{
179 ASSERT(from.isCell() && from.asCell()->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
180 return static_cast<To>(from.asCell());
181}
182
183template<typename To, typename From>
184inline To jsDynamicCast(From* from)
185{
186 return from->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from) : 0;
187}
188
189template<typename To>
190inline To jsDynamicCast(JSValue from)
191{
192 return from.isCell() && from.asCell()->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from.asCell()) : 0;
193}
14957cd0 194
9dae56ea
A
195} // namespace JSC
196
197#endif // JSCell_h