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.
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.
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.
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.
27 #include "CallFrame.h"
28 #include "ConstructData.h"
31 #include "JSValueInlineMethods.h"
32 #include "SlotVisitor.h"
33 #include "WriteBarrier.h"
34 #include <wtf/Noncopyable.h>
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);
49 MSVCBugWorkaround() { }
50 ~MSVCBugWorkaround() { }
53 class JSCell : MSVCBugWorkaround {
56 WTF_MAKE_NONCOPYABLE(JSCell);
59 friend class ExecutableBase;
60 friend class GetterSetter;
62 friend class JSObject;
63 friend class JSPropertyNameIterator;
64 friend class JSString;
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;
75 enum CreatingEarlyCellTag { CreatingEarlyCell };
78 enum VPtrStealingHackType { VPtrStealingHack };
81 explicit JSCell(VPtrStealingHackType) { }
82 JSCell(JSGlobalData&, Structure*);
83 JSCell(CreatingEarlyCellTag);
88 bool isString() const;
89 bool isObject() const;
90 bool isGetterSetter() const;
91 bool inherits(const ClassInfo*) const;
92 bool isAPIValueWrapper() const;
94 Structure* structure() const;
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
102 virtual CallType getCallData(CallData&);
103 virtual ConstructType getConstructData(ConstructData&);
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;
113 // Garbage collection.
114 void* operator new(size_t, void* placementNewDestination) { return placementNewDestination; }
116 virtual void visitChildren(SlotVisitor&);
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);
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; }
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&);
137 static ptrdiff_t structureOffset()
139 return OBJECT_OFFSETOF(JSCell, m_structure);
142 #if ENABLE(GC_VALIDATION)
143 Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); }
148 void finishCreation(JSGlobalData&);
149 void finishCreation(JSGlobalData&, Structure*, CreatingEarlyCellTag);
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&);
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*);
161 WriteBarrier<Structure> m_structure;
164 inline JSCell::JSCell(JSGlobalData& globalData, Structure* structure)
165 : m_structure(globalData, this, structure)
169 inline JSCell::JSCell(CreatingEarlyCellTag)
173 inline void JSCell::finishCreation(JSGlobalData& globalData)
175 #if ENABLE(GC_VALIDATION)
176 ASSERT(globalData.isInitializingObject());
177 globalData.setInitializingObject(false);
179 UNUSED_PARAM(globalData);
184 inline void JSCell::finishCreation(JSGlobalData& globalData, Structure* structure, CreatingEarlyCellTag)
186 #if ENABLE(GC_VALIDATION)
187 ASSERT(globalData.isInitializingObject());
188 globalData.setInitializingObject(false);
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);
196 inline JSCell::~JSCell()
198 #if ENABLE(GC_VALIDATION)
203 inline Structure* JSCell::structure() const
205 return m_structure.get();
208 inline void JSCell::visitChildren(SlotVisitor& visitor)
210 visitor.append(&m_structure);
213 // --- JSValue inlines ----------------------------
215 inline bool JSValue::isString() const
217 return isCell() && asCell()->isString();
220 inline bool JSValue::isPrimitive() const
222 return !isCell() || asCell()->isString();
225 inline bool JSValue::isGetterSetter() const
227 return isCell() && asCell()->isGetterSetter();
230 inline bool JSValue::isObject() const
232 return isCell() && asCell()->isObject();
235 inline bool JSValue::getString(ExecState* exec, UString& s) const
237 return isCell() && asCell()->getString(exec, s);
240 inline UString JSValue::getString(ExecState* exec) const
242 return isCell() ? asCell()->getString(exec) : UString();
245 template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
247 return jsValue().getString(exec);
250 inline JSObject* JSValue::getObject() const
252 return isCell() ? asCell()->getObject() : 0;
255 inline CallType getCallData(JSValue value, CallData& callData)
257 CallType result = value.isCell() ? value.asCell()->getCallData(callData) : CallTypeNone;
258 ASSERT(result == CallTypeNone || value.isValidCallee());
262 inline ConstructType getConstructData(JSValue value, ConstructData& constructData)
264 ConstructType result = value.isCell() ? value.asCell()->getConstructData(constructData) : ConstructTypeNone;
265 ASSERT(result == ConstructTypeNone || value.isValidCallee());
269 ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
272 int32_t i = asInt32();
273 v = static_cast<uint32_t>(i);
277 double d = asDouble();
278 v = static_cast<uint32_t>(d);
284 inline JSValue JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
286 return isCell() ? asCell()->toPrimitive(exec, preferredType) : asValue();
289 inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue& value)
302 return asCell()->getPrimitiveNumber(exec, number, value);
308 if (isFalse() || isNull()) {
313 ASSERT(isUndefined());
314 number = std::numeric_limits<double>::quiet_NaN();
319 ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
325 return toNumberSlowCase(exec);
328 inline JSValue JSValue::getJSNumber()
330 if (isInt32() || isDouble())
333 return asCell()->getJSNumber();
337 inline JSObject* JSValue::toObject(ExecState* exec) const
339 return isCell() ? asCell()->toObject(exec, exec->lexicalGlobalObject()) : toObjectSlowCase(exec, exec->lexicalGlobalObject());
342 inline JSObject* JSValue::toObject(ExecState* exec, JSGlobalObject* globalObject) const
344 return isCell() ? asCell()->toObject(exec, globalObject) : toObjectSlowCase(exec, globalObject);
347 inline JSObject* JSValue::toThisObject(ExecState* exec) const
349 return isCell() ? asCell()->toThisObject(exec) : toThisObjectSlowCase(exec);
352 template <typename T> void* allocateCell(Heap& heap)
354 #if ENABLE(GC_VALIDATION)
355 ASSERT(!heap.globalData()->isInitializingObject());
356 heap.globalData()->setInitializingObject(true);
358 return heap.allocate(sizeof(T));