Home | History | Annotate | Download | only in heap
      1 /*
      2  * Copyright (C) 2011 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef Handle_h
     27 #define Handle_h
     28 
     29 #include "WriteBarrier.h"
     30 #include <wtf/Assertions.h>
     31 
     32 namespace JSC {
     33 
     34 /*
     35     A Handle is a smart pointer that updates automatically when the garbage
     36     collector moves the object to which it points.
     37 
     38     The base Handle class represents a temporary reference to a pointer whose
     39     lifetime is guaranteed by something else.
     40 */
     41 
     42 template <class T> class Handle;
     43 
     44 // Creating a JSValue Handle is invalid
     45 template <> class Handle<JSValue>;
     46 
     47 // Forward declare WeakGCMap
     48 template<typename KeyType, typename MappedType, typename FinalizerCallback, typename HashArg, typename KeyTraitsArg> class WeakGCMap;
     49 
     50 class HandleBase {
     51     friend class HandleHeap;
     52     friend struct JSCallbackObjectData;
     53     template <typename KeyType, typename MappedType, typename FinalizerCallback, typename HashArg, typename KeyTraitsArg> friend class WeakGCMap;
     54 
     55 public:
     56     bool operator!() const { return !m_slot || !*m_slot; }
     57 
     58     // This conversion operator allows implicit conversion to bool but not to other integer types.
     59     typedef JSValue (HandleBase::*UnspecifiedBoolType);
     60     operator UnspecifiedBoolType*() const { return (m_slot && *m_slot) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; }
     61 
     62 protected:
     63     HandleBase(HandleSlot slot)
     64         : m_slot(slot)
     65     {
     66     }
     67 
     68     void swap(HandleBase& other) { std::swap(m_slot, other.m_slot); }
     69 
     70     HandleSlot slot() const { return m_slot; }
     71     void setSlot(HandleSlot slot)
     72     {
     73         m_slot = slot;
     74     }
     75 
     76 private:
     77     HandleSlot m_slot;
     78 };
     79 
     80 template <typename T> struct HandleTypes {
     81     typedef T* ExternalType;
     82     static ExternalType getFromSlot(HandleSlot slot) { return (slot && *slot) ? reinterpret_cast<ExternalType>(slot->asCell()) : 0; }
     83     static JSValue toJSValue(T* cell) { return reinterpret_cast<JSCell*>(cell); }
     84     template <typename U> static void validateUpcast() { T* temp; temp = (U*)0; }
     85 };
     86 
     87 template <> struct HandleTypes<Unknown> {
     88     typedef JSValue ExternalType;
     89     static ExternalType getFromSlot(HandleSlot slot) { return slot ? *slot : JSValue(); }
     90     static JSValue toJSValue(const JSValue& v) { return v; }
     91     template <typename U> static void validateUpcast() {}
     92 };
     93 
     94 template <typename Base, typename T> struct HandleConverter {
     95     T* operator->()
     96     {
     97 #if ENABLE(JSC_ZOMBIES)
     98         ASSERT(!static_cast<const Base*>(this)->get() || !static_cast<const Base*>(this)->get()->isZombie());
     99 #endif
    100         return static_cast<Base*>(this)->get();
    101     }
    102     const T* operator->() const
    103     {
    104 #if ENABLE(JSC_ZOMBIES)
    105         ASSERT(!static_cast<const Base*>(this)->get() || !static_cast<const Base*>(this)->get()->isZombie());
    106 #endif
    107         return static_cast<const Base*>(this)->get();
    108     }
    109 
    110     T* operator*()
    111     {
    112 #if ENABLE(JSC_ZOMBIES)
    113         ASSERT(!static_cast<const Base*>(this)->get() || !static_cast<const Base*>(this)->get()->isZombie());
    114 #endif
    115         return static_cast<Base*>(this)->get();
    116     }
    117     const T* operator*() const
    118     {
    119 #if ENABLE(JSC_ZOMBIES)
    120         ASSERT(!static_cast<const Base*>(this)->get() || !static_cast<const Base*>(this)->get()->isZombie());
    121 #endif
    122         return static_cast<const Base*>(this)->get();
    123     }
    124 };
    125 
    126 template <typename Base> struct HandleConverter<Base, Unknown> {
    127     Handle<JSObject> asObject() const;
    128     bool isObject() const { return jsValue().isObject(); }
    129     bool getNumber(double number) const { return jsValue().getNumber(number); }
    130     UString getString(ExecState*) const;
    131     bool isUndefinedOrNull() const { return jsValue().isUndefinedOrNull(); }
    132 
    133 private:
    134     JSValue jsValue() const
    135     {
    136 #if ENABLE(JSC_ZOMBIES)
    137         ASSERT(!static_cast<const Base*>(this)->get() || !static_cast<const Base*>(this)->get().isZombie());
    138 #endif
    139         return static_cast<const Base*>(this)->get();
    140     }
    141 };
    142 
    143 template <typename T> class Handle : public HandleBase, public HandleConverter<Handle<T>, T> {
    144 public:
    145     template <typename A, typename B> friend class HandleConverter;
    146     typedef typename HandleTypes<T>::ExternalType ExternalType;
    147     template <typename U> Handle(Handle<U> o)
    148     {
    149         typename HandleTypes<T>::template validateUpcast<U>();
    150         setSlot(o.slot());
    151     }
    152 
    153     void swap(Handle& other) { HandleBase::swap(other); }
    154 
    155     ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); }
    156 
    157 protected:
    158     Handle(HandleSlot slot = 0)
    159         : HandleBase(slot)
    160     {
    161     }
    162 
    163 private:
    164     friend class HandleHeap;
    165 
    166     static Handle<T> wrapSlot(HandleSlot slot)
    167     {
    168         return Handle<T>(slot);
    169     }
    170 };
    171 
    172 template <typename Base> Handle<JSObject> HandleConverter<Base, Unknown>::asObject() const
    173 {
    174     return Handle<JSObject>::wrapSlot(static_cast<const Base*>(this)->slot());
    175 }
    176 
    177 template <typename T, typename U> inline bool operator==(const Handle<T>& a, const Handle<U>& b)
    178 {
    179     return a.get() == b.get();
    180 }
    181 
    182 template <typename T, typename U> inline bool operator==(const Handle<T>& a, U* b)
    183 {
    184     return a.get() == b;
    185 }
    186 
    187 template <typename T, typename U> inline bool operator==(T* a, const Handle<U>& b)
    188 {
    189     return a == b.get();
    190 }
    191 
    192 template <typename T, typename U> inline bool operator!=(const Handle<T>& a, const Handle<U>& b)
    193 {
    194     return a.get() != b.get();
    195 }
    196 
    197 template <typename T, typename U> inline bool operator!=(const Handle<T>& a, U* b)
    198 {
    199     return a.get() != b;
    200 }
    201 
    202 template <typename T, typename U> inline bool operator!=(T* a, const Handle<U>& b)
    203 {
    204     return a != b.get();
    205 }
    206 
    207 template <typename T, typename U> inline bool operator!=(const Handle<T>& a, JSValue b)
    208 {
    209     return a.get() != b;
    210 }
    211 
    212 template <typename T, typename U> inline bool operator!=(JSValue a, const Handle<U>& b)
    213 {
    214     return a != b.get();
    215 }
    216 
    217 }
    218 
    219 #endif
    220