/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
- * Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
#ifndef JSCell_h
#define JSCell_h
+#include "CallData.h"
+#include "CallFrame.h"
+#include "ConstructData.h"
+#include "Heap.h"
+#include "JSLock.h"
+#include "JSValueInlineMethods.h"
+#include "SlotVisitor.h"
+#include "WriteBarrier.h"
#include <wtf/Noncopyable.h>
-#include "Structure.h"
-#include "JSValue.h"
-#include "JSImmediate.h"
-#include "Collector.h"
namespace JSC {
- class JSCell : Noncopyable {
- friend class GetterSetter;
- friend class Heap;
- friend class JIT;
- friend class JSNumberCell;
- friend class JSObject;
- friend class JSPropertyNameIterator;
- friend class JSString;
+ class JSGlobalObject;
+ class LLIntOffsetsExtractor;
+ class PropertyDescriptor;
+ class PropertyNameArray;
+ class Structure;
+
+ enum EnumerationMode {
+ ExcludeDontEnumProperties,
+ IncludeDontEnumProperties
+ };
+
+ enum TypedArrayType {
+ TypedArrayNone,
+ TypedArrayInt8,
+ TypedArrayInt16,
+ TypedArrayInt32,
+ TypedArrayUint8,
+ TypedArrayUint8Clamped,
+ TypedArrayUint16,
+ TypedArrayUint32,
+ TypedArrayFloat32,
+ TypedArrayFloat64
+ };
+
+ class JSCell {
friend class JSValue;
- friend class JSAPIValueWrapper;
- friend struct VPtrSet;
+ friend class MarkedBlock;
+ template<typename T> friend void* allocateCell(Heap&);
- private:
- explicit JSCell(Structure*);
- virtual ~JSCell();
+ public:
+ enum CreatingEarlyCellTag { CreatingEarlyCell };
+ JSCell(CreatingEarlyCellTag);
+
+ protected:
+ JSCell(JSGlobalData&, Structure*);
+ JS_EXPORT_PRIVATE static void destroy(JSCell*);
public:
// Querying the type.
-#if USE(JSVALUE32)
- bool isNumber() const;
-#endif
bool isString() const;
bool isObject() const;
- virtual bool isGetterSetter() const;
- virtual bool isObject(const ClassInfo*) const;
- virtual bool isAPIValueWrapper() const { return false; }
+ bool isGetterSetter() const;
+ bool inherits(const ClassInfo*) const;
+ bool isAPIValueWrapper() const;
Structure* structure() const;
+ void setStructure(JSGlobalData&, Structure*);
+ void clearStructure() { m_structure.clear(); }
// Extracting the value.
- bool getString(UString&) const;
- UString getString() const; // null string if not a string
- JSObject* getObject(); // NULL if not an object
+ JS_EXPORT_PRIVATE bool getString(ExecState* exec, UString&) const;
+ JS_EXPORT_PRIVATE UString getString(ExecState* exec) const; // null string if not a string
+ JS_EXPORT_PRIVATE JSObject* getObject(); // NULL if not an object
const JSObject* getObject() const; // NULL if not an object
- virtual CallType getCallData(CallData&);
- virtual ConstructType getConstructData(ConstructData&);
-
- // Extracting integer values.
- // FIXME: remove these methods, can check isNumberCell in JSValue && then call asNumberCell::*.
- virtual bool getUInt32(uint32_t&) const;
+ JS_EXPORT_PRIVATE static CallType getCallData(JSCell*, CallData&);
+ JS_EXPORT_PRIVATE static ConstructType getConstructData(JSCell*, ConstructData&);
// Basic conversions.
- virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const = 0;
- virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue&) = 0;
- virtual bool toBoolean(ExecState*) const = 0;
- virtual double toNumber(ExecState*) const = 0;
- virtual UString toString(ExecState*) const = 0;
- virtual JSObject* toObject(ExecState*) const = 0;
-
- // Garbage collection.
- void* operator new(size_t, ExecState*);
- void* operator new(size_t, JSGlobalData*);
- void* operator new(size_t, void* placementNewDestination) { return placementNewDestination; }
- virtual void mark();
- bool marked() const;
+ JS_EXPORT_PRIVATE JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
+ bool getPrimitiveNumber(ExecState*, double& number, JSValue&) const;
+ bool toBoolean(ExecState*) const;
+ JS_EXPORT_PRIVATE double toNumber(ExecState*) const;
+ JS_EXPORT_PRIVATE JSObject* toObject(ExecState*, JSGlobalObject*) const;
- // Object operations, with the toObject operation included.
- virtual const ClassInfo* classInfo() const;
- virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
- virtual void put(ExecState*, unsigned propertyName, JSValue);
- virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
- virtual bool deleteProperty(ExecState*, unsigned propertyName);
-
- virtual JSObject* toThisObject(ExecState*) const;
- virtual UString toThisString(ExecState*) const;
- virtual JSString* toThisJSString(ExecState*);
- virtual JSValue getJSNumber();
- void* vptr() { return *reinterpret_cast<void**>(this); }
+ static void visitChildren(JSCell*, SlotVisitor&);
- private:
- // Base implementation; for non-object classes implements getPropertySlot.
- bool fastGetOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
- virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
- virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
+ // Object operations, with the toObject operation included.
+ const ClassInfo* classInfo() const;
+ const ClassInfo* validatedClassInfo() const;
+ const MethodTable* methodTable() const;
+ static void put(JSCell*, ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
+ static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
- Structure* m_structure;
- };
+ static bool deleteProperty(JSCell*, ExecState*, const Identifier& propertyName);
+ static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
- JSCell* asCell(JSValue);
+ static JSObject* toThisObject(JSCell*, ExecState*);
- inline JSCell* asCell(JSValue value)
- {
- return value.asCell();
- }
+ void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
+ bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
- inline JSCell::JSCell(Structure* structure)
- : m_structure(structure)
- {
- }
+ // FIXME: Rename getOwnPropertySlot to virtualGetOwnPropertySlot, and
+ // fastGetOwnPropertySlot to getOwnPropertySlot. Callers should always
+ // call this function, not its slower virtual counterpart. (For integer
+ // property names, we want a similar interface with appropriate optimizations.)
+ bool fastGetOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
+ JSValue fastGetOwnProperty(ExecState*, const UString&);
- inline JSCell::~JSCell()
- {
- }
+ static ptrdiff_t structureOffset()
+ {
+ return OBJECT_OFFSETOF(JSCell, m_structure);
+ }
-#if USE(JSVALUE32)
- inline bool JSCell::isNumber() const
- {
- return Heap::isNumber(const_cast<JSCell*>(this));
- }
+ static ptrdiff_t classInfoOffset()
+ {
+ return OBJECT_OFFSETOF(JSCell, m_classInfo);
+ }
+
+ void* structureAddress()
+ {
+ return &m_structure;
+ }
+
+#if ENABLE(GC_VALIDATION)
+ Structure* unvalidatedStructure() { return m_structure.unvalidatedGet(); }
#endif
+
+ static const TypedArrayType TypedArrayStorageType = TypedArrayNone;
+ protected:
- inline bool JSCell::isObject() const
- {
- return m_structure->typeInfo().type() == ObjectType;
- }
+ void finishCreation(JSGlobalData&);
+ void finishCreation(JSGlobalData&, Structure*, CreatingEarlyCellTag);
+
+ // Base implementation; for non-object classes implements getPropertySlot.
+ static bool getOwnPropertySlot(JSCell*, ExecState*, const Identifier& propertyName, PropertySlot&);
+ static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
+
+ // Dummy implementations of override-able static functions for classes to put in their MethodTable
+ static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
+ static NO_RETURN_DUE_TO_ASSERT void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
+ static NO_RETURN_DUE_TO_ASSERT void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
+ static UString className(const JSObject*);
+ static bool hasInstance(JSObject*, ExecState*, JSValue, JSValue prototypeProperty);
+ static NO_RETURN_DUE_TO_ASSERT void putDirectVirtual(JSObject*, ExecState*, const Identifier& propertyName, JSValue, unsigned attributes);
+ static bool defineOwnProperty(JSObject*, ExecState*, const Identifier& propertyName, PropertyDescriptor&, bool shouldThrow);
+ static bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&);
- inline bool JSCell::isString() const
+ private:
+ friend class LLIntOffsetsExtractor;
+
+ const ClassInfo* m_classInfo;
+ WriteBarrier<Structure> m_structure;
+ };
+
+ inline JSCell::JSCell(CreatingEarlyCellTag)
{
- return m_structure->typeInfo().type() == StringType;
}
- inline Structure* JSCell::structure() const
+ inline void JSCell::finishCreation(JSGlobalData& globalData)
{
- return m_structure;
+#if ENABLE(GC_VALIDATION)
+ ASSERT(globalData.isInitializingObject());
+ globalData.setInitializingObjectClass(0);
+#else
+ UNUSED_PARAM(globalData);
+#endif
+ ASSERT(m_structure);
}
- inline bool JSCell::marked() const
+ inline Structure* JSCell::structure() const
{
- return Heap::isCellMarked(this);
+ return m_structure.get();
}
- inline void JSCell::mark()
+ inline const ClassInfo* JSCell::classInfo() const
{
- return Heap::markCell(this);
+ return m_classInfo;
}
- inline void* JSCell::operator new(size_t size, JSGlobalData* globalData)
+ inline void JSCell::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
-#ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
- return globalData->heap.inlineAllocate(size);
-#else
- return globalData->heap.allocate(size);
-#endif
+ visitor.append(&cell->m_structure);
}
// --- JSValue inlines ----------------------------
return isCell() && asCell()->isString();
}
+ inline bool JSValue::isPrimitive() const
+ {
+ return !isCell() || asCell()->isString();
+ }
+
inline bool JSValue::isGetterSetter() const
{
return isCell() && asCell()->isGetterSetter();
return isCell() && asCell()->isObject();
}
- inline bool JSValue::getString(UString& s) const
- {
- return isCell() && asCell()->getString(s);
- }
-
- inline UString JSValue::getString() const
+ inline bool JSValue::getString(ExecState* exec, UString& s) const
{
- return isCell() ? asCell()->getString() : UString();
+ return isCell() && asCell()->getString(exec, s);
}
- inline JSObject* JSValue::getObject() const
+ inline UString JSValue::getString(ExecState* exec) const
{
- return isCell() ? asCell()->getObject() : 0;
+ return isCell() ? asCell()->getString(exec) : UString();
}
- inline CallType JSValue::getCallData(CallData& callData)
+ template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
{
- return isCell() ? asCell()->getCallData(callData) : CallTypeNone;
+ return jsValue().getString(exec);
}
- inline ConstructType JSValue::getConstructData(ConstructData& constructData)
+ inline JSObject* JSValue::getObject() const
{
- return isCell() ? asCell()->getConstructData(constructData) : ConstructTypeNone;
+ return isCell() ? asCell()->getObject() : 0;
}
ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
return false;
}
- inline void JSValue::mark()
- {
- asCell()->mark(); // callers should check !marked() before calling mark(), so this should only be called with cells
- }
-
- inline bool JSValue::marked() const
- {
- return !isCell() || asCell()->marked();
- }
-
-#if !USE(JSVALUE32_64)
- ALWAYS_INLINE JSCell* JSValue::asCell() const
- {
- ASSERT(isCell());
- return m_ptr;
- }
-#endif // !USE(JSVALUE32_64)
-
inline JSValue JSValue::toPrimitive(ExecState* exec, PreferredPrimitiveType preferredType) const
{
return isCell() ? asCell()->toPrimitive(exec, preferredType) : asValue();
return true;
}
ASSERT(isUndefined());
- number = nonInlineNaN();
+ number = std::numeric_limits<double>::quiet_NaN();
value = *this;
return true;
}
- inline bool JSValue::toBoolean(ExecState* exec) const
- {
- if (isInt32())
- return asInt32() != 0;
- if (isDouble())
- return asDouble() > 0.0 || asDouble() < 0.0; // false for NaN
- if (isCell())
- return asCell()->toBoolean(exec);
- return isTrue(); // false, null, and undefined all convert to false.
- }
-
ALWAYS_INLINE double JSValue::toNumber(ExecState* exec) const
{
if (isInt32())
return asInt32();
if (isDouble())
return asDouble();
- if (isCell())
- return asCell()->toNumber(exec);
- if (isTrue())
- return 1.0;
- return isUndefined() ? nonInlineNaN() : 0; // null and false both convert to 0.
+ return toNumberSlowCase(exec);
}
- inline UString JSValue::toString(ExecState* exec) const
+ inline JSObject* JSValue::toObject(ExecState* exec) const
{
- if (isCell())
- return asCell()->toString(exec);
- if (isInt32())
- return UString::from(asInt32());
- if (isDouble())
- return asDouble() == 0.0 ? "0" : UString::from(asDouble());
- if (isTrue())
- return "true";
- if (isFalse())
- return "false";
- if (isNull())
- return "null";
- ASSERT(isUndefined());
- return "undefined";
+ return isCell() ? asCell()->toObject(exec, exec->lexicalGlobalObject()) : toObjectSlowCase(exec, exec->lexicalGlobalObject());
}
- inline bool JSValue::needsThisConversion() const
+ inline JSObject* JSValue::toObject(ExecState* exec, JSGlobalObject* globalObject) const
{
- if (UNLIKELY(!isCell()))
- return true;
- return asCell()->structure()->typeInfo().needsThisConversion();
+ return isCell() ? asCell()->toObject(exec, globalObject) : toObjectSlowCase(exec, globalObject);
+ }
+
+#if COMPILER(CLANG)
+ template<class T>
+ struct NeedsDestructor {
+ static const bool value = !__has_trivial_destructor(T);
+ };
+#else
+ // Write manual specializations for this struct template if you care about non-clang compilers.
+ template<class T>
+ struct NeedsDestructor {
+ static const bool value = true;
+ };
+#endif
+
+ template<typename T>
+ void* allocateCell(Heap& heap)
+ {
+#if ENABLE(GC_VALIDATION)
+ ASSERT(!heap.globalData()->isInitializingObject());
+ heap.globalData()->setInitializingObjectClass(&T::s_info);
+#endif
+ JSCell* result = 0;
+ if (NeedsDestructor<T>::value)
+ result = static_cast<JSCell*>(heap.allocateWithDestructor(sizeof(T)));
+ else {
+ ASSERT(T::s_info.methodTable.destroy == JSCell::destroy);
+ result = static_cast<JSCell*>(heap.allocateWithoutDestructor(sizeof(T)));
+ }
+ result->clearStructure();
+ return result;
+ }
+
+ inline bool isZapped(const JSCell* cell)
+ {
+ return cell->isZapped();
}
- inline UString JSValue::toThisString(ExecState* exec) const
+ template<typename To, typename From>
+ inline To jsCast(From* from)
{
- return isCell() ? asCell()->toThisString(exec) : toString(exec);
+ ASSERT(!from || from->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
+ return static_cast<To>(from);
}
- inline JSValue JSValue::getJSNumber()
+ template<typename To>
+ inline To jsCast(JSValue from)
{
- if (isInt32() || isDouble())
- return *this;
- if (isCell())
- return asCell()->getJSNumber();
- return JSValue();
+ ASSERT(from.isCell() && from.asCell()->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
+ return static_cast<To>(from.asCell());
}
- inline JSObject* JSValue::toObject(ExecState* exec) const
+ template<typename To, typename From>
+ inline To jsDynamicCast(From* from)
{
- return isCell() ? asCell()->toObject(exec) : toObjectSlowCase(exec);
+ return from->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from) : 0;
}
- inline JSObject* JSValue::toThisObject(ExecState* exec) const
+ template<typename To>
+ inline To jsDynamicCast(JSValue from)
{
- return isCell() ? asCell()->toThisObject(exec) : toThisObjectSlowCase(exec);
+ return from.isCell() && from.asCell()->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from.asCell()) : 0;
}
} // namespace JSC