OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 * dereferencing the handle (for instance, to extract the Object* from | 164 * dereferencing the handle (for instance, to extract the Object* from |
165 * a Handle<Object>); the value will still be governed by a handle | 165 * a Handle<Object>); the value will still be governed by a handle |
166 * behind the scenes and the same rules apply to these values as to | 166 * behind the scenes and the same rules apply to these values as to |
167 * their handles. | 167 * their handles. |
168 */ | 168 */ |
169 template <class T> class Handle { | 169 template <class T> class Handle { |
170 public: | 170 public: |
171 /** | 171 /** |
172 * Creates an empty handle. | 172 * Creates an empty handle. |
173 */ | 173 */ |
174 inline Handle(); | 174 inline Handle() : val_(0) {} |
175 | 175 |
176 /** | 176 /** |
177 * Creates a new handle for the specified value. | 177 * Creates a new handle for the specified value. |
178 */ | 178 */ |
179 inline explicit Handle(T* val) : val_(val) { } | 179 inline explicit Handle(T* val) : val_(val) {} |
180 | 180 |
181 /** | 181 /** |
182 * Creates a handle for the contents of the specified handle. This | 182 * Creates a handle for the contents of the specified handle. This |
183 * constructor allows you to pass handles as arguments by value and | 183 * constructor allows you to pass handles as arguments by value and |
184 * to assign between handles. However, if you try to assign between | 184 * to assign between handles. However, if you try to assign between |
185 * incompatible handles, for instance from a Handle<String> to a | 185 * incompatible handles, for instance from a Handle<String> to a |
186 * Handle<Number> it will cause a compile-time error. Assigning | 186 * Handle<Number> it will cause a compile-time error. Assigning |
187 * between compatible handles, for instance assigning a | 187 * between compatible handles, for instance assigning a |
188 * Handle<String> to a variable declared as Handle<Value>, is legal | 188 * Handle<String> to a variable declared as Handle<Value>, is legal |
189 * because String is a subclass of Value. | 189 * because String is a subclass of Value. |
190 */ | 190 */ |
191 template <class S> inline Handle(Handle<S> that) | 191 template <class S> inline Handle(Handle<S> that) |
192 : val_(reinterpret_cast<T*>(*that)) { | 192 : val_(reinterpret_cast<T*>(*that)) { |
193 /** | 193 /** |
194 * This check fails when trying to convert between incompatible | 194 * This check fails when trying to convert between incompatible |
195 * handles. For example, converting from a Handle<String> to a | 195 * handles. For example, converting from a Handle<String> to a |
196 * Handle<Number>. | 196 * Handle<Number>. |
197 */ | 197 */ |
198 TYPE_CHECK(T, S); | 198 TYPE_CHECK(T, S); |
199 } | 199 } |
200 | 200 |
201 /** | 201 /** |
202 * Returns true if the handle is empty. | 202 * Returns true if the handle is empty. |
203 */ | 203 */ |
204 inline bool IsEmpty() const { return val_ == 0; } | 204 inline bool IsEmpty() const { return val_ == 0; } |
205 | 205 |
| 206 /** |
| 207 * Sets the handle to be empty. IsEmpty() will then return true. |
| 208 */ |
| 209 inline void Clear() { val_ = 0; } |
| 210 |
206 inline T* operator->() const { return val_; } | 211 inline T* operator->() const { return val_; } |
207 | 212 |
208 inline T* operator*() const { return val_; } | 213 inline T* operator*() const { return val_; } |
209 | 214 |
210 /** | 215 /** |
211 * Sets the handle to be empty. IsEmpty() will then return true. | |
212 */ | |
213 inline void Clear() { this->val_ = 0; } | |
214 | |
215 /** | |
216 * Checks whether two handles are the same. | 216 * Checks whether two handles are the same. |
217 * Returns true if both are empty, or if the objects | 217 * Returns true if both are empty, or if the objects |
218 * to which they refer are identical. | 218 * to which they refer are identical. |
219 * The handles' references are not checked. | 219 * The handles' references are not checked. |
220 */ | 220 */ |
221 template <class S> inline bool operator==(Handle<S> that) const { | 221 template <class S> inline bool operator==(Handle<S> that) const { |
222 internal::Object** a = reinterpret_cast<internal::Object**>(**this); | 222 internal::Object** a = reinterpret_cast<internal::Object**>(**this); |
223 internal::Object** b = reinterpret_cast<internal::Object**>(*that); | 223 internal::Object** b = reinterpret_cast<internal::Object**>(*that); |
224 if (a == 0) return b == 0; | 224 if (a == 0) return b == 0; |
225 if (b == 0) return false; | 225 if (b == 0) return false; |
(...skipping 3594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3820 static inline bool CanCastToHeapObject(Object* o) { return true; } | 3820 static inline bool CanCastToHeapObject(Object* o) { return true; } |
3821 static inline bool CanCastToHeapObject(Message* o) { return true; } | 3821 static inline bool CanCastToHeapObject(Message* o) { return true; } |
3822 static inline bool CanCastToHeapObject(StackTrace* o) { return true; } | 3822 static inline bool CanCastToHeapObject(StackTrace* o) { return true; } |
3823 static inline bool CanCastToHeapObject(StackFrame* o) { return true; } | 3823 static inline bool CanCastToHeapObject(StackFrame* o) { return true; } |
3824 }; | 3824 }; |
3825 | 3825 |
3826 } // namespace internal | 3826 } // namespace internal |
3827 | 3827 |
3828 | 3828 |
3829 template <class T> | 3829 template <class T> |
3830 Handle<T>::Handle() : val_(0) { } | |
3831 | |
3832 | |
3833 template <class T> | |
3834 Local<T>::Local() : Handle<T>() { } | 3830 Local<T>::Local() : Handle<T>() { } |
3835 | 3831 |
3836 | 3832 |
3837 template <class T> | 3833 template <class T> |
3838 Local<T> Local<T>::New(Handle<T> that) { | 3834 Local<T> Local<T>::New(Handle<T> that) { |
3839 if (that.IsEmpty()) return Local<T>(); | 3835 if (that.IsEmpty()) return Local<T>(); |
3840 T* that_ptr = *that; | 3836 T* that_ptr = *that; |
3841 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); | 3837 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); |
3842 if (internal::Internals::CanCastToHeapObject(that_ptr)) { | 3838 if (internal::Internals::CanCastToHeapObject(that_ptr)) { |
3843 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( | 3839 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4201 | 4197 |
4202 | 4198 |
4203 } // namespace v8 | 4199 } // namespace v8 |
4204 | 4200 |
4205 | 4201 |
4206 #undef V8EXPORT | 4202 #undef V8EXPORT |
4207 #undef TYPE_CHECK | 4203 #undef TYPE_CHECK |
4208 | 4204 |
4209 | 4205 |
4210 #endif // V8_H_ | 4206 #endif // V8_H_ |
OLD | NEW |