2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "HandleTypes.h"
34 A Handle is a smart pointer that updates automatically when the garbage
35 collector moves the object to which it points.
37 The base Handle class represents a temporary reference to a pointer whose
38 lifetime is guaranteed by something else.
41 template <class T
> class Handle
;
43 // Creating a JSValue Handle is invalid
44 template <> class Handle
<JSValue
>;
46 // Forward declare WeakGCMap
47 template<typename KeyType
, typename MappedType
, typename FinalizerCallback
, typename HashArg
, typename KeyTraitsArg
> class WeakGCMap
;
50 template <typename T
> friend class Weak
;
51 friend class HandleHeap
;
52 friend struct JSCallbackObjectData
;
53 template <typename KeyType
, typename MappedType
, typename FinalizerCallback
, typename HashArg
, typename KeyTraitsArg
> friend class WeakGCMap
;
56 bool operator!() const { return !m_slot
|| !*m_slot
; }
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; }
63 HandleBase(HandleSlot slot
)
68 void swap(HandleBase
& other
) { std::swap(m_slot
, other
.m_slot
); }
70 HandleSlot
slot() const { return m_slot
; }
71 void setSlot(HandleSlot slot
)
80 template <typename Base
, typename T
> struct HandleConverter
{
83 #if ENABLE(JSC_ZOMBIES)
84 ASSERT(!static_cast<const Base
*>(this)->get() || !static_cast<const Base
*>(this)->get()->isZombie());
86 return static_cast<Base
*>(this)->get();
88 const T
* operator->() const
90 #if ENABLE(JSC_ZOMBIES)
91 ASSERT(!static_cast<const Base
*>(this)->get() || !static_cast<const Base
*>(this)->get()->isZombie());
93 return static_cast<const Base
*>(this)->get();
98 #if ENABLE(JSC_ZOMBIES)
99 ASSERT(!static_cast<const Base
*>(this)->get() || !static_cast<const Base
*>(this)->get()->isZombie());
101 return static_cast<Base
*>(this)->get();
103 const T
* operator*() const
105 #if ENABLE(JSC_ZOMBIES)
106 ASSERT(!static_cast<const Base
*>(this)->get() || !static_cast<const Base
*>(this)->get()->isZombie());
108 return static_cast<const Base
*>(this)->get();
112 template <typename Base
> struct HandleConverter
<Base
, Unknown
> {
113 Handle
<JSObject
> asObject() const;
114 bool isObject() const { return jsValue().isObject(); }
115 bool getNumber(double number
) const { return jsValue().getNumber(number
); }
116 UString
getString(ExecState
*) const;
117 bool isUndefinedOrNull() const { return jsValue().isUndefinedOrNull(); }
120 JSValue
jsValue() const
122 #if ENABLE(JSC_ZOMBIES)
123 ASSERT(!static_cast<const Base
*>(this)->get() || !static_cast<const Base
*>(this)->get().isZombie());
125 return static_cast<const Base
*>(this)->get();
129 template <typename T
> class Handle
: public HandleBase
, public HandleConverter
<Handle
<T
>, T
> {
131 template <typename A
, typename B
> friend class HandleConverter
;
132 typedef typename HandleTypes
<T
>::ExternalType ExternalType
;
133 template <typename U
> Handle(Handle
<U
> o
)
135 typename HandleTypes
<T
>::template validateUpcast
<U
>();
139 void swap(Handle
& other
) { HandleBase::swap(other
); }
141 ExternalType
get() const { return HandleTypes
<T
>::getFromSlot(this->slot()); }
144 Handle(HandleSlot slot
= 0)
150 friend class HandleHeap
;
152 static Handle
<T
> wrapSlot(HandleSlot slot
)
154 return Handle
<T
>(slot
);
158 template <typename Base
> Handle
<JSObject
> HandleConverter
<Base
, Unknown
>::asObject() const
160 return Handle
<JSObject
>::wrapSlot(static_cast<const Base
*>(this)->slot());
163 template <typename T
, typename U
> inline bool operator==(const Handle
<T
>& a
, const Handle
<U
>& b
)
165 return a
.get() == b
.get();
168 template <typename T
, typename U
> inline bool operator==(const Handle
<T
>& a
, U
* b
)
173 template <typename T
, typename U
> inline bool operator==(T
* a
, const Handle
<U
>& b
)
178 template <typename T
, typename U
> inline bool operator!=(const Handle
<T
>& a
, const Handle
<U
>& b
)
180 return a
.get() != b
.get();
183 template <typename T
, typename U
> inline bool operator!=(const Handle
<T
>& a
, U
* b
)
188 template <typename T
, typename U
> inline bool operator!=(T
* a
, const Handle
<U
>& b
)
193 template <typename T
, typename U
> inline bool operator!=(const Handle
<T
>& a
, JSValue b
)
198 template <typename T
, typename U
> inline bool operator!=(JSValue a
, const Handle
<U
>& b
)