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#pragma once
27
28#include "HandleTypes.h"
29
30namespace JSC {
31
32/*
33 A Handle is a smart pointer that updates automatically when the garbage
34 collector moves the object to which it points.
35
36 The base Handle class represents a temporary reference to a pointer whose
37 lifetime is guaranteed by something else.
38*/
39
40template <class T> class Handle;
41
42// Creating a JSValue Handle is invalid
43template <> class Handle<JSValue>;
44
45class HandleBase {
46 template <typename T> friend class Weak;
47 template <typename T> friend class Strong;
48 friend class HandleSet;
49 friend struct JSCallbackObjectData;
50
51public:
52 bool operator!() const { return !m_slot || !*m_slot; }
53
54 explicit operator bool() const { return m_slot && *m_slot; }
55
56 HandleSlot slot() const { return m_slot; }
57
58protected:
59 HandleBase(HandleSlot slot)
60 : m_slot(slot)
61 {
62 }
63
64 void swap(HandleBase& other) { std::swap(m_slot, other.m_slot); }
65
66 void setSlot(HandleSlot slot)
67 {
68 m_slot = slot;
69 }
70
71private:
72 HandleSlot m_slot;
73};
74
75template <typename Base, typename T> struct HandleConverter {
76 T* operator->()
77 {
78 return static_cast<Base*>(this)->get();
79 }
80 const T* operator->() const
81 {
82 return static_cast<const Base*>(this)->get();
83 }
84
85 T* operator*()
86 {
87 return static_cast<Base*>(this)->get();
88 }
89 const T* operator*() const
90 {
91 return static_cast<const Base*>(this)->get();
92 }
93};
94
95template <typename Base> struct HandleConverter<Base, Unknown> {
96 Handle<JSObject> asObject() const;
97 bool isObject() const { return jsValue().isObject(); }
98 bool getNumber(double number) const { return jsValue().getNumber(number); }
99 WTF::String getString(ExecState*) const;
100 bool isUndefinedOrNull() const { return jsValue().isUndefinedOrNull(); }
101
102private:
103 JSValue jsValue() const
104 {
105 return static_cast<const Base*>(this)->get();
106 }
107};
108
109template <typename T> class Handle : public HandleBase, public HandleConverter<Handle<T>, T> {
110public:
111 template <typename A, typename B> friend struct HandleConverter;
112 typedef typename HandleTypes<T>::ExternalType ExternalType;
113 template <typename U> Handle(Handle<U> o)
114 {
115 typename HandleTypes<T>::template validateUpcast<U>();
116 setSlot(o.slot());
117 }
118
119 void swap(Handle& other) { HandleBase::swap(other); }
120
121 ExternalType get() const { return HandleTypes<T>::getFromSlot(this->slot()); }
122
123protected:
124 Handle(HandleSlot slot = 0)
125 : HandleBase(slot)
126 {
127 }
128
129private:
130 friend class HandleSet;
131 friend class WeakBlock;
132
133 static Handle<T> wrapSlot(HandleSlot slot)
134 {
135 return Handle<T>(slot);
136 }
137};
138
139template <typename Base> Handle<JSObject> HandleConverter<Base, Unknown>::asObject() const
140{
141 return Handle<JSObject>::wrapSlot(static_cast<const Base*>(this)->slot());
142}
143
144template <typename T, typename U> inline bool operator==(const Handle<T>& a, const Handle<U>& b)
145{
146 return a.get() == b.get();
147}
148
149template <typename T, typename U> inline bool operator==(const Handle<T>& a, U* b)
150{
151 return a.get() == b;
152}
153
154template <typename T, typename U> inline bool operator==(T* a, const Handle<U>& b)
155{
156 return a == b.get();
157}
158
159template <typename T, typename U> inline bool operator!=(const Handle<T>& a, const Handle<U>& b)
160{
161 return a.get() != b.get();
162}
163
164template <typename T, typename U> inline bool operator!=(const Handle<T>& a, U* b)
165{
166 return a.get() != b;
167}
168
169template <typename T, typename U> inline bool operator!=(T* a, const Handle<U>& b)
170{
171 return a != b.get();
172}
173
174template <typename T, typename U> inline bool operator!=(const Handle<T>& a, JSValue b)
175{
176 return a.get() != b;
177}
178
179template <typename T, typename U> inline bool operator!=(JSValue a, const Handle<U>& b)
180{
181 return a != b.get();
182}
183
184} // namespace JSC
185