blob: 534228893bf8ebccb85c3f97ad3e55385e4974ec [file] [log] [blame]
weinig@apple.com6a03b4c2008-07-01 17:32:44 +00001/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
oliver@apple.com5fca29f2009-08-11 04:35:02 +00004 * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Apple Inc. All rights reserved.
weinig@apple.com6a03b4c2008-07-01 17:32:44 +00005 *
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.com99ff3432010-06-03 20:00:18 +000026#include "CallData.h"
barraclough@apple.com99ff3432010-06-03 20:00:18 +000027#include "ConstructData.h"
ggaren@apple.coma8b38542011-01-10 23:43:56 +000028#include "Heap.h"
ggaren@apple.com2006bee2011-03-01 23:36:53 +000029#include "JSLock.h"
ggaren@apple.com6e1f8c12011-06-16 22:01:43 +000030#include "SlotVisitor.h"
fpizlo@apple.comc14c8d32012-10-10 02:14:42 +000031#include "TypedArrayDescriptor.h"
ggaren@apple.com046c1e02011-05-11 02:29:51 +000032#include "WriteBarrier.h"
darin@apple.com8a1a5b52009-09-04 19:03:33 +000033#include <wtf/Noncopyable.h>
mhahnenberg@apple.com53d40642012-09-18 16:13:11 +000034#include <wtf/TypeTraits.h>
weinig@apple.com6a03b4c2008-07-01 17:32:44 +000035
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000036namespace JSC {
weinig@apple.com6a03b4c2008-07-01 17:32:44 +000037
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000038class CopyVisitor;
fpizlo@apple.coma4b4cbe2013-01-12 04:47:03 +000039class ExecState;
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000040class JSDestructibleObject;
41class JSGlobalObject;
42class LLIntOffsetsExtractor;
43class PropertyDescriptor;
44class PropertyNameArray;
45class Structure;
mhahnenberg@apple.com57262382011-11-03 00:25:45 +000046
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000047enum EnumerationMode {
48 ExcludeDontEnumProperties,
49 IncludeDontEnumProperties
50};
oliver@apple.com3b6dc572011-03-28 23:39:16 +000051
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000052class 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.comf9419b72011-05-26 22:43:07 +000057
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000058public:
59 static const unsigned StructureFlags = 0;
ggaren@apple.com47e224a2012-08-26 03:25:31 +000060
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000061 static const bool needsDestruction = false;
62 static const bool hasImmortalStructure = false;
mhahnenberg@apple.com30738a72012-10-03 17:51:28 +000063
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000064 enum CreatingEarlyCellTag { CreatingEarlyCell };
65 JSCell(CreatingEarlyCellTag);
ggaren@apple.com1caf69d2011-09-20 01:21:51 +000066
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000067protected:
68 JSCell(JSGlobalData&, Structure*);
69 JS_EXPORT_PRIVATE static void destroy(JSCell*);
weinig@apple.com6a03b4c2008-07-01 17:32:44 +000070
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000071public:
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.com6a03b4c2008-07-01 17:32:44 +000079
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000080 Structure* structure() const;
81 void setStructure(JSGlobalData&, Structure*);
82 void clearStructure() { m_structure.clear(); }
weinig@apple.com3412bb42008-09-01 21:22:54 +000083
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000084 const char* className();
msaboff@apple.com9d9eab62012-06-06 23:11:09 +000085
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000086 // 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.com0e2d66e2008-07-06 05:26:58 +000091
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000092 JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&);
93 JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&);
weinig@apple.com6a03b4c2008-07-01 17:32:44 +000094
fpizlo@apple.comf980ef62012-11-06 21:13:27 +000095 // 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.com6a03b4c2008-07-01 17:32:44 +0000101
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000102 static void visitChildren(JSCell*, SlotVisitor&);
103 JS_EXPORT_PRIVATE static void copyBackingStore(JSCell*, CopyVisitor&);
weinig@apple.com6a03b4c2008-07-01 17:32:44 +0000104
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000105 // Object operations, with the toObject operation included.
106 const ClassInfo* classInfo() const;
107 const MethodTable* methodTable() const;
oliver@apple.comb6b94a92013-01-30 01:31:37 +0000108 const MethodTable* methodTableForDestruction() const;
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000109 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
110 static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
mhahnenberg@apple.com5e2b7122011-10-08 00:06:07 +0000111
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000112 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
113 static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
weinig@apple.com6a03b4c2008-07-01 17:32:44 +0000114
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000115 static JSObject* toThisObject(JSCell*, ExecState*);
ggaren@apple.comb94f6ba2011-09-24 22:15:40 +0000116
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000117 void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
118 bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
weinig@apple.com6a03b4c2008-07-01 17:32:44 +0000119
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000120 // 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.com871ffe62013-03-15 21:52:35 +0000125 JSValue fastGetOwnProperty(ExecState*, const String&);
mjs@apple.combc644c92010-05-09 11:18:25 +0000126
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000127 static ptrdiff_t structureOffset()
oliver@apple.com41037162011-05-14 22:10:01 +0000128 {
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000129 return OBJECT_OFFSETOF(JSCell, m_structure);
commit-queue@webkit.org99878832011-08-24 02:05:33 +0000130 }
131
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000132 void* structureAddress()
commit-queue@webkit.org99878832011-08-24 02:05:33 +0000133 {
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000134 return &m_structure;
135 }
136
oliver@apple.com41037162011-05-14 22:10:01 +0000137#if ENABLE(GC_VALIDATION)
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000138 Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); }
139#endif
140
141 static const TypedArrayType TypedArrayStorageType = TypedArrayNone;
142protected:
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.com5598c182013-01-23 22:25:07 +0000153 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.comf980ef62012-11-06 21:13:27 +0000156 static String className(const JSObject*);
157 JS_EXPORT_PRIVATE static bool customHasInstance(JSObject*, ExecState*, JSValue);
oliver@apple.com5598c182013-01-23 22:25:07 +0000158 static NO_RETURN_DUE_TO_CRASH void putDirectVirtual(JSObject*, ExecState*, PropertyName, JSValue, unsigned attributes);
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000159 static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
160 static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
161
162private:
163 friend class LLIntOffsetsExtractor;
164
165 WriteBarrier<Structure> m_structure;
166};
167
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000168template<typename To, typename From>
169inline 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.com30738a72012-10-03 17:51:28 +0000174
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000175template<typename To>
176inline 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.com484a9d32012-03-22 18:54:50 +0000181
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000182template<typename To, typename From>
183inline To jsDynamicCast(From* from)
184{
185 return from->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from) : 0;
186}
mhahnenberg@apple.comb6f11ee2011-11-18 22:13:37 +0000187
fpizlo@apple.comf980ef62012-11-06 21:13:27 +0000188template<typename To>
189inline 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.com484a9d32012-03-22 18:54:50 +0000193
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000194} // namespace JSC
weinig@apple.com6a03b4c2008-07-01 17:32:44 +0000195
196#endif // JSCell_h