]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - runtime/JSCell.h
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / JSCell.h
index ae5aca3bf422eccde0516fce73fdc44cf66683f6..431e67145320908d0860d13f97c2a4be70747157 100644 (file)
 #ifndef JSCell_h
 #define JSCell_h
 
-#include "Collector.h"
-#include "JSImmediate.h"
-#include "JSValue.h"
-#include "MarkStack.h"
-#include "Structure.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>
 
 namespace JSC {
 
-    class JSCell : public NoncopyableCustomAllocated {
-        friend class GetterSetter;
-        friend class Heap;
-        friend class JIT;
-        friend class JSNumberCell;
-        friend class JSObject;
-        friend class JSPropertyNameIterator;
-        friend class JSString;
-        friend class JSValue;
-        friend class JSAPIValueWrapper;
-        friend class JSZombie;
-        friend class JSGlobalData;
+    class JSGlobalObject;
+    class LLIntOffsetsExtractor;
+    class PropertyDescriptor;
+    class PropertyNameArray;
+    class Structure;
 
-    private:
-        explicit JSCell(Structure*);
-        virtual ~JSCell();
+    enum EnumerationMode {
+        ExcludeDontEnumProperties,
+        IncludeDontEnumProperties
+    };
+
+    enum TypedArrayType {
+        TypedArrayNone,
+        TypedArrayInt8,
+        TypedArrayInt16,
+        TypedArrayInt32,
+        TypedArrayUint8,
+        TypedArrayUint8Clamped,
+        TypedArrayUint16,
+        TypedArrayUint32,
+        TypedArrayFloat32,
+        TypedArrayFloat64
+    };
+
+    class JSCell {
+        friend class JSValue;
+        friend class MarkedBlock;
+        template<typename T> friend void* allocateCell(Heap&);
 
     public:
-        static PassRefPtr<Structure> createDummyStructure()
-        {
-            return Structure::create(jsNull(), TypeInfo(UnspecifiedType), AnonymousSlotCount);
-        }
+        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;
+        bool isGetterSetter() const;
         bool inherits(const ClassInfo*) const;
-        virtual bool isAPIValueWrapper() const { return false; }
-        virtual bool isPropertyNameIterator() const { return false; }
+        bool isAPIValueWrapper() const;
 
         Structure* structure() const;
+        void setStructure(JSGlobalData&, Structure*);
+        void clearStructure() { m_structure.clear(); }
 
         // Extracting the value.
-        bool getString(ExecState* exec, UString&) const;
-        UString getString(ExecState* exec) 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;
-        virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue&);
-        virtual bool toBoolean(ExecState*) const;
-        virtual double toNumber(ExecState*) const;
-        virtual UString toString(ExecState*) const;
-        virtual JSObject* toObject(ExecState*) const;
-
-        // 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 markChildren(MarkStack&);
-#if ENABLE(JSC_ZOMBIES)
-        virtual bool isZombie() const { return false; }
-#endif
+        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;
+
+        static void visitChildren(JSCell*, SlotVisitor&);
 
         // 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);
+        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);
+        
+        static bool deleteProperty(JSCell*, ExecState*, const Identifier& propertyName);
+        static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
+
+        static JSObject* toThisObject(JSCell*, ExecState*);
 
-        virtual JSObject* toThisObject(ExecState*) const;
-        virtual JSValue getJSNumber();
-        void* vptr() { return *reinterpret_cast<void**>(this); }
-        void setVPtr(void* vptr) { *reinterpret_cast<void**>(this) = vptr; }
+        void zap() { *reinterpret_cast<uintptr_t**>(this) = 0; }
+        bool isZapped() const { return !*reinterpret_cast<uintptr_t* const*>(this); }
 
         // 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&);
+
+        static ptrdiff_t structureOffset()
+        {
+            return OBJECT_OFFSETOF(JSCell, m_structure);
+        }
 
+        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:
-        static const unsigned AnonymousSlotCount = 0;
 
-    private:
+        void finishCreation(JSGlobalData&);
+        void finishCreation(JSGlobalData&, Structure*, CreatingEarlyCellTag);
+
         // Base implementation; for non-object classes implements getPropertySlot.
-        virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
-        virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
+        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&);
+
+    private:
+        friend class LLIntOffsetsExtractor;
         
-        Structure* m_structure;
+        const ClassInfo* m_classInfo;
+        WriteBarrier<Structure> m_structure;
     };
 
-    inline JSCell::JSCell(Structure* structure)
-        : m_structure(structure)
-    {
-    }
-
-    inline JSCell::~JSCell()
+    inline JSCell::JSCell(CreatingEarlyCellTag)
     {
     }
 
-#if USE(JSVALUE32)
-    inline bool JSCell::isNumber() const
+    inline void JSCell::finishCreation(JSGlobalData& globalData)
     {
-        return m_structure->typeInfo().type() == NumberType;
-    }
+#if ENABLE(GC_VALIDATION)
+        ASSERT(globalData.isInitializingObject());
+        globalData.setInitializingObjectClass(0);
+#else
+        UNUSED_PARAM(globalData);
 #endif
-
-    inline bool JSCell::isObject() const
-    {
-        return m_structure->typeInfo().type() == ObjectType;
-    }
-
-    inline bool JSCell::isString() const
-    {
-        return m_structure->typeInfo().type() == StringType;
+        ASSERT(m_structure);
     }
 
     inline Structure* JSCell::structure() const
     {
-        return m_structure;
-    }
-
-    inline void JSCell::markChildren(MarkStack&)
-    {
+        return m_structure.get();
     }
 
-    inline void* JSCell::operator new(size_t size, JSGlobalData* globalData)
+    inline const ClassInfo* JSCell::classInfo() const
     {
-        return globalData->heap.allocate(size);
+        return m_classInfo;
     }
 
-    inline void* JSCell::operator new(size_t size, ExecState* exec)
+    inline void JSCell::visitChildren(JSCell* cell, SlotVisitor& visitor)
     {
-        return exec->heap()->allocate(size);
+        visitor.append(&cell->m_structure);
     }
 
     // --- JSValue inlines ----------------------------
@@ -180,6 +207,11 @@ namespace JSC {
         return isCell() && asCell()->isString();
     }
 
+    inline bool JSValue::isPrimitive() const
+    {
+        return !isCell() || asCell()->isString();
+    }
+
     inline bool JSValue::isGetterSetter() const
     {
         return isCell() && asCell()->isGetterSetter();
@@ -200,19 +232,14 @@ namespace JSC {
         return isCell() ? asCell()->getString(exec) : UString();
     }
 
-    inline JSObject* JSValue::getObject() const
+    template <typename Base> UString HandleConverter<Base, Unknown>::getString(ExecState* exec) const
     {
-        return isCell() ? asCell()->getObject() : 0;
+        return jsValue().getString(exec);
     }
 
-    inline CallType JSValue::getCallData(CallData& callData)
-    {
-        return isCell() ? asCell()->getCallData(callData) : CallTypeNone;
-    }
-
-    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
@@ -230,14 +257,6 @@ namespace JSC {
         return false;
     }
 
-#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();
@@ -268,97 +287,92 @@ namespace JSC {
             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 bool JSValue::needsThisConversion() const
+    inline JSObject* JSValue::toObject(ExecState* exec) const
     {
-        if (UNLIKELY(!isCell()))
-            return true;
-        return asCell()->structure()->typeInfo().needsThisConversion();
+        return isCell() ? asCell()->toObject(exec, exec->lexicalGlobalObject()) : toObjectSlowCase(exec, exec->lexicalGlobalObject());
     }
 
-    inline JSValue JSValue::getJSNumber()
+    inline JSObject* JSValue::toObject(ExecState* exec, JSGlobalObject* globalObject) const
     {
-        if (isInt32() || isDouble())
-            return *this;
-        if (isCell())
-            return asCell()->getJSNumber();
-        return JSValue();
+        return isCell() ? asCell()->toObject(exec, globalObject) : toObjectSlowCase(exec, globalObject);
     }
 
-    inline JSObject* JSValue::toObject(ExecState* exec) const
-    {
-        return isCell() ? asCell()->toObject(exec) : toObjectSlowCase(exec);
-    }
+#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
 
-    inline JSObject* JSValue::toThisObject(ExecState* exec) const
+    template<typename T>
+    void* allocateCell(Heap& heap)
     {
-        return isCell() ? asCell()->toThisObject(exec) : toThisObjectSlowCase(exec);
+#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;
     }
-
-    ALWAYS_INLINE void MarkStack::append(JSCell* cell)
+    
+    inline bool isZapped(const JSCell* cell)
     {
-        ASSERT(!m_isCheckingForDefaultMarkViolation);
-        ASSERT(cell);
-        if (Heap::isCellMarked(cell))
-            return;
-        Heap::markCell(cell);
-        if (cell->structure()->typeInfo().type() >= CompoundType)
-            m_values.append(cell);
+        return cell->isZapped();
     }
 
-    ALWAYS_INLINE void MarkStack::append(JSValue value)
+    template<typename To, typename From>
+    inline To jsCast(From* from)
     {
-        ASSERT(value);
-        if (value.isCell())
-            append(value.asCell());
+        ASSERT(!from || from->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
+        return static_cast<To>(from);
     }
 
-    inline Heap* Heap::heap(JSValue v)
+    template<typename To>
+    inline To jsCast(JSValue from)
     {
-        if (!v.isCell())
-            return 0;
-        return heap(v.asCell());
+        ASSERT(from.isCell() && from.asCell()->JSCell::inherits(&WTF::RemovePointer<To>::Type::s_info));
+        return static_cast<To>(from.asCell());
     }
 
-    inline Heap* Heap::heap(JSCell* c)
+    template<typename To, typename From>
+    inline To jsDynamicCast(From* from)
     {
-        return cellBlock(c)->heap;
+        return from->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from) : 0;
     }
-    
-#if ENABLE(JSC_ZOMBIES)
-    inline bool JSValue::isZombie() const
+
+    template<typename To>
+    inline To jsDynamicCast(JSValue from)
     {
-        return isCell() && asCell() && asCell()->isZombie();
+        return from.isCell() && from.asCell()->inherits(&WTF::RemovePointer<To>::Type::s_info) ? static_cast<To>(from.asCell()) : 0;
     }
-#endif
+
 } // namespace JSC
 
 #endif // JSCell_h