initial import
[vuplus_webkit] / Source / JavaScriptCore / 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 Structure;
40
41 #if COMPILER(MSVC)
42     // If WTF_MAKE_NONCOPYABLE is applied to JSCell we end up with a bunch of
43     // undefined references to the JSCell copy constructor and assignment operator
44     // when linking JavaScriptCore.
45     class MSVCBugWorkaround {
46         WTF_MAKE_NONCOPYABLE(MSVCBugWorkaround);
47
48     protected:
49         MSVCBugWorkaround() { }
50         ~MSVCBugWorkaround() { }
51     };
52
53     class JSCell : MSVCBugWorkaround {
54 #else
55     class JSCell {
56         WTF_MAKE_NONCOPYABLE(JSCell);
57 #endif
58
59         friend class ExecutableBase;
60         friend class GetterSetter;
61         friend class Heap;
62         friend class JSObject;
63         friend class JSPropertyNameIterator;
64         friend class JSString;
65         friend class JSValue;
66         friend class JSAPIValueWrapper;
67         friend class JSGlobalData;
68         friend class NewSpace;
69         friend class MarkedBlock;
70         friend class ScopeChainNode;
71         friend class Structure;
72         friend class StructureChain;
73         friend class RegExp;
74
75         enum CreatingEarlyCellTag { CreatingEarlyCell };
76
77     protected:
78         enum VPtrStealingHackType { VPtrStealingHack };
79
80     private:
81         explicit JSCell(VPtrStealingHackType) { }
82         JSCell(JSGlobalData&, Structure*);
83         JSCell(CreatingEarlyCellTag);
84         virtual ~JSCell();
85
86     public:
87         // Querying the type.
88         bool isString() const;
89         bool isObject() const;
90         bool isGetterSetter() const;
91         bool inherits(const ClassInfo*) const;
92         bool isAPIValueWrapper() const;
93
94         Structure* structure() const;
95
96         // Extracting the value.
97         bool getString(ExecState* exec, UString&) const;
98         UString getString(ExecState* exec) const; // null string if not a string
99         JSObject* getObject(); // NULL if not an object
100         const JSObject* getObject() const; // NULL if not an object
101         
102         virtual CallType getCallData(CallData&);
103         virtual ConstructType getConstructData(ConstructData&);
104
105         // Basic conversions.
106         virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
107         virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue&);
108         bool toBoolean(ExecState*) const;
109         virtual double toNumber(ExecState*) const;
110         virtual UString toString(ExecState*) const;
111         virtual JSObject* toObject(ExecState*, JSGlobalObject*) const;
112
113         // Garbage collection.
114         void* operator new(size_t, void* placementNewDestination) { return placementNewDestination; }
115
116         virtual void visitChildren(SlotVisitor&);
117
118         // Object operations, with the toObject operation included.
119         const ClassInfo* classInfo() const;
120         virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
121         virtual void put(ExecState*, unsigned propertyName, JSValue);
122         virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
123         virtual bool deleteProperty(ExecState*, unsigned propertyName);
124
125         virtual JSObject* toThisObject(ExecState*) const;
126         virtual JSValue getJSNumber();
127         void* vptr() { return *reinterpret_cast<void**>(this); }
128         void setVPtr(void* vptr) { *reinterpret_cast<void**>(this) = vptr; }
129
130         // FIXME: Rename getOwnPropertySlot to virtualGetOwnPropertySlot, and
131         // fastGetOwnPropertySlot to getOwnPropertySlot. Callers should always
132         // call this function, not its slower virtual counterpart. (For integer
133         // property names, we want a similar interface with appropriate optimizations.)
134         bool fastGetOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
135         JSValue fastGetOwnProperty(ExecState*, const UString&);
136
137         static ptrdiff_t structureOffset()
138         {
139             return OBJECT_OFFSETOF(JSCell, m_structure);
140         }
141
142 #if ENABLE(GC_VALIDATION)
143         Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); }
144 #endif
145         
146     protected:
147
148         void finishCreation(JSGlobalData&);
149         void finishCreation(JSGlobalData&, Structure*, CreatingEarlyCellTag);
150
151     private:
152         // Base implementation; for non-object classes implements getPropertySlot.
153         virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
154         virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
155         
156         // Note that the first two declarations of operator new have no corresponding implementation and 
157         // will cause link errors if you use them.
158         void* operator new(size_t, ExecState*);
159         void* operator new(size_t, JSGlobalData*);
160         
161         WriteBarrier<Structure> m_structure;
162     };
163
164     inline JSCell::JSCell(JSGlobalData& globalData, Structure* structure)
165         : m_structure(globalData, this, structure)
166     {
167     }
168
169     inline JSCell::JSCell(CreatingEarlyCellTag)
170     {
171     }
172
173     inline void JSCell::finishCreation(JSGlobalData& globalData)
174     {
175 #if ENABLE(GC_VALIDATION)
176         ASSERT(globalData.isInitializingObject());
177         globalData.setInitializingObject(false);
178 #else
179         UNUSED_PARAM(globalData);
180 #endif
181         ASSERT(m_structure);
182     }
183
184     inline void JSCell::finishCreation(JSGlobalData& globalData, Structure* structure, CreatingEarlyCellTag)
185     {
186 #if ENABLE(GC_VALIDATION)
187         ASSERT(globalData.isInitializingObject());
188         globalData.setInitializingObject(false);
189         if (structure)
190 #endif
191             m_structure.setEarlyValue(globalData, this, structure);
192         // Very first set of allocations won't have a real structure.
193         ASSERT(m_structure || !globalData.structureStructure);
194     }
195
196     inline JSCell::~JSCell()
197     {
198 #if ENABLE(GC_VALIDATION)
199         m_structure.clear();
200 #endif
201     }
202
203     inline Structure* JSCell::structure() const
204     {
205         return m_structure.get();
206     }
207
208     inline void JSCell::visitChildren(SlotVisitor& visitor)
209     {
210         visitor.append(&m_structure);
211     }
212
213     // --- JSValue inlines ----------------------------
214
215     inline bool JSValue::isString() const
216     {
217         return isCell() && asCell()->isString();
218     }
219
220     inline bool JSValue::isPrimitive() const
221     {
222         return !isCell() || asCell()->isString();
223     }
224
225     inline bool JSValue::isGetterSetter() const
226     {
227         return isCell() && asCell()->isGetterSetter();
228     }
229
230     inline bool JSValue::isObject() const
231     {
232         return isCell() && asCell()->isObject();
233     }
234
235     inline bool JSValue::getString(ExecState* exec, UString& s) const
236     {
237         return isCell() && asCell()->getString(exec, s);
238     }
239
240     inline UString JSValue::getString(ExecState* exec) const
241     {
242         return isCell() ? asCell()->getString(exec) : UString();
243     }
244
245     template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
246     {
247         return jsValue().getString(exec);
248     }
249
250     inline JSObject* JSValue::getObject() const
251     {
252         return isCell() ? asCell()->getObject() : 0;
253     }
254
255     inline CallType getCallData(JSValue value, CallData& callData)
256     {
257         CallType result = value.isCell() ? value.asCell()->getCallData(callData) : CallTypeNone;
258         ASSERT(result == CallTypeNone || value.isValidCallee());
259         return result;
260     }
261
262     inline ConstructType getConstructData(JSValue value, ConstructData& constructData)
263     {
264         ConstructType result = value.isCell() ? value.asCell()->getConstructData(constructData) : ConstructTypeNone;
265         ASSERT(result == ConstructTypeNone || value.isValidCallee());
266         return result;
267     }
268
269     ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
270     {
271         if (isInt32()) {
272             int32_t i = asInt32();
273             v = static_cast<uint32_t>(i);
274             return i >= 0;
275         }
276         if (isDouble()) {
277             double d = asDouble();
278             v = static_cast<uint32_t>(d);
279             return v == d;
280         }
281         return false;
282     }
283
284     inline JSValue JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
285     {
286         return isCell() ? asCell()->toPrimitive(exec, preferredType) : asValue();
287     }
288
289     inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value)
290     {
291         if (isInt32()) {
292             number = asInt32();
293             value = *this;
294             return true;
295         }
296         if (isDouble()) {
297             number = asDouble();
298             value = *this;
299             return true;
300         }
301         if (isCell())
302             return asCell()->getPrimitiveNumber(exec, number, value);
303         if (isTrue()) {
304             number = 1.0;
305             value = *this;
306             return true;
307         }
308         if (isFalse() || isNull()) {
309             number = 0.0;
310             value = *this;
311             return true;
312         }
313         ASSERT(isUndefined());
314         number = std::numeric_limits<double>::quiet_NaN();
315         value = *this;
316         return true;
317     }
318
319     ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
320     {
321         if (isInt32())
322             return asInt32();
323         if (isDouble())
324             return asDouble();
325         return toNumberSlowCase(exec);
326     }
327
328     inline JSValue JSValue::getJSNumber()
329     {
330         if (isInt32() || isDouble())
331             return *this;
332         if (isCell())
333             return asCell()->getJSNumber();
334         return JSValue();
335     }
336
337     inline JSObject* JSValue::toObject(ExecState* exec) const
338     {
339         return isCell() ? asCell()->toObject(exec, exec->lexicalGlobalObject()) : toObjectSlowCase(exec, exec->lexicalGlobalObject());
340     }
341
342     inline JSObject* JSValue::toObject(ExecState* exec, JSGlobalObject* globalObject) const
343     {
344         return isCell() ? asCell()->toObject(exec, globalObject) : toObjectSlowCase(exec, globalObject);
345     }
346
347     inline JSObject* JSValue::toThisObject(ExecState* exec) const
348     {
349         return isCell() ? asCell()->toThisObject(exec) : toThisObjectSlowCase(exec);
350     }
351
352     template <typename T> void* allocateCell(Heap& heap)
353     {
354 #if ENABLE(GC_VALIDATION)
355         ASSERT(!heap.globalData()->isInitializingObject());
356         heap.globalData()->setInitializingObject(true);
357 #endif
358         return heap.allocate(sizeof(T));
359     }
360         
361 } // namespace JSC
362
363 #endif // JSCell_h