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