Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: include/v8.h

Issue 679143002: make Handle a synonym of Local (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: victory!... assuming android doesn't do anything stupid Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/checks.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** \mainpage V8 API Reference Guide 5 /** \mainpage V8 API Reference Guide
6 * 6 *
7 * V8 is Google's open source JavaScript engine. 7 * V8 is Google's open source JavaScript engine.
8 * 8 *
9 * This set of documents provides reference material generated from the 9 * This set of documents provides reference material generated from the
10 * V8 header file, include/v8.h. 10 * V8 header file, include/v8.h.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 class StackFrame; 99 class StackFrame;
100 class StackTrace; 100 class StackTrace;
101 class String; 101 class String;
102 class StringObject; 102 class StringObject;
103 class Symbol; 103 class Symbol;
104 class SymbolObject; 104 class SymbolObject;
105 class Private; 105 class Private;
106 class Uint32; 106 class Uint32;
107 class Utils; 107 class Utils;
108 class Value; 108 class Value;
109 template <class T> class Handle;
110 template <class T> class Local; 109 template <class T> class Local;
111 template <class T> 110 template <class T>
112 class MaybeLocal; 111 class MaybeLocal;
113 template <class T> class Eternal; 112 template <class T> class Eternal;
114 template<class T> class NonCopyablePersistentTraits; 113 template<class T> class NonCopyablePersistentTraits;
115 template<class T> class PersistentBase; 114 template<class T> class PersistentBase;
116 template<class T, 115 template<class T,
117 class M = NonCopyablePersistentTraits<T> > class Persistent; 116 class M = NonCopyablePersistentTraits<T> > class Persistent;
118 template <class T> 117 template <class T>
119 class Global; 118 class Global;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 * 195 *
197 * There are two types of handles: local and persistent handles. 196 * There are two types of handles: local and persistent handles.
198 * Local handles are light-weight and transient and typically used in 197 * Local handles are light-weight and transient and typically used in
199 * local operations. They are managed by HandleScopes. Persistent 198 * local operations. They are managed by HandleScopes. Persistent
200 * handles can be used when storing objects across several independent 199 * handles can be used when storing objects across several independent
201 * operations and have to be explicitly deallocated when they're no 200 * operations and have to be explicitly deallocated when they're no
202 * longer used. 201 * longer used.
203 * 202 *
204 * It is safe to extract the object stored in the handle by 203 * It is safe to extract the object stored in the handle by
205 * dereferencing the handle (for instance, to extract the Object* from 204 * dereferencing the handle (for instance, to extract the Object* from
206 * a Handle<Object>); the value will still be governed by a handle 205 * a Local<Object>); the value will still be governed by a handle
207 * behind the scenes and the same rules apply to these values as to 206 * behind the scenes and the same rules apply to these values as to
208 * their handles. 207 * their handles.
209 */ 208 */
210 template <class T> class Handle { 209 template <class T>
210 class Local {
211 public: 211 public:
212 /** 212 V8_INLINE Local() : val_(0) {}
213 * Creates an empty handle. 213 template <class S>
214 */ 214 V8_INLINE Local(Local<S> that)
215 V8_INLINE Handle() : val_(0) {}
216
217 /**
218 * Creates a handle for the contents of the specified handle. This
219 * constructor allows you to pass handles as arguments by value and
220 * to assign between handles. However, if you try to assign between
221 * incompatible handles, for instance from a Handle<String> to a
222 * Handle<Number> it will cause a compile-time error. Assigning
223 * between compatible handles, for instance assigning a
224 * Handle<String> to a variable declared as Handle<Value>, is legal
225 * because String is a subclass of Value.
226 */
227 template <class S> V8_INLINE Handle(Handle<S> that)
228 : val_(reinterpret_cast<T*>(*that)) { 215 : val_(reinterpret_cast<T*>(*that)) {
229 /** 216 /**
230 * This check fails when trying to convert between incompatible 217 * This check fails when trying to convert between incompatible
231 * handles. For example, converting from a Handle<String> to a 218 * handles. For example, converting from a Handle<String> to a
232 * Handle<Number>. 219 * Handle<Number>.
233 */ 220 */
234 TYPE_CHECK(T, S); 221 TYPE_CHECK(T, S);
235 } 222 }
236 223
237 /** 224 /**
238 * Returns true if the handle is empty. 225 * Returns true if the handle is empty.
239 */ 226 */
240 V8_INLINE bool IsEmpty() const { return val_ == 0; } 227 V8_INLINE bool IsEmpty() const { return val_ == 0; }
241 228
242 /** 229 /**
243 * Sets the handle to be empty. IsEmpty() will then return true. 230 * Sets the handle to be empty. IsEmpty() will then return true.
244 */ 231 */
245 V8_INLINE void Clear() { val_ = 0; } 232 V8_INLINE void Clear() { val_ = 0; }
246 233
247 V8_INLINE T* operator->() const { return val_; } 234 V8_INLINE T* operator->() const { return val_; }
248 235
249 V8_INLINE T* operator*() const { return val_; } 236 V8_INLINE T* operator*() const { return val_; }
250 237
251 /** 238 /**
252 * Checks whether two handles are the same. 239 * Checks whether two handles are the same.
253 * Returns true if both are empty, or if the objects 240 * Returns true if both are empty, or if the objects
254 * to which they refer are identical. 241 * to which they refer are identical.
255 * The handles' references are not checked. 242 * The handles' references are not checked.
256 */ 243 */
257 template <class S> V8_INLINE bool operator==(const Handle<S>& that) const { 244 template <class S>
245 V8_INLINE bool operator==(const Local<S>& that) const {
258 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); 246 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
259 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); 247 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
260 if (a == 0) return b == 0; 248 if (a == 0) return b == 0;
261 if (b == 0) return false; 249 if (b == 0) return false;
262 return *a == *b; 250 return *a == *b;
263 } 251 }
264 252
265 template <class S> V8_INLINE bool operator==( 253 template <class S> V8_INLINE bool operator==(
266 const PersistentBase<S>& that) const { 254 const PersistentBase<S>& that) const {
267 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); 255 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
268 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); 256 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
269 if (a == 0) return b == 0; 257 if (a == 0) return b == 0;
270 if (b == 0) return false; 258 if (b == 0) return false;
271 return *a == *b; 259 return *a == *b;
272 } 260 }
273 261
274 /** 262 /**
275 * Checks whether two handles are different. 263 * Checks whether two handles are different.
276 * Returns true if only one of the handles is empty, or if 264 * Returns true if only one of the handles is empty, or if
277 * the objects to which they refer are different. 265 * the objects to which they refer are different.
278 * The handles' references are not checked. 266 * The handles' references are not checked.
279 */ 267 */
280 template <class S> V8_INLINE bool operator!=(const Handle<S>& that) const { 268 template <class S>
269 V8_INLINE bool operator!=(const Local<S>& that) const {
281 return !operator==(that); 270 return !operator==(that);
282 } 271 }
283 272
284 template <class S> V8_INLINE bool operator!=( 273 template <class S> V8_INLINE bool operator!=(
285 const Persistent<S>& that) const { 274 const Persistent<S>& that) const {
286 return !operator==(that); 275 return !operator==(that);
287 } 276 }
288 277
289 template <class S> V8_INLINE static Handle<T> Cast(Handle<S> that) {
290 #ifdef V8_ENABLE_CHECKS
291 // If we're going to perform the type check then we have to check
292 // that the handle isn't empty before doing the checked cast.
293 if (that.IsEmpty()) return Handle<T>();
294 #endif
295 return Handle<T>(T::Cast(*that));
296 }
297
298 template <class S> V8_INLINE Handle<S> As() {
299 return Handle<S>::Cast(*this);
300 }
301
302 V8_INLINE static Handle<T> New(Isolate* isolate, Handle<T> that) {
303 return New(isolate, that.val_);
304 }
305 V8_INLINE static Handle<T> New(Isolate* isolate,
306 const PersistentBase<T>& that) {
307 return New(isolate, that.val_);
308 }
309
310 private:
311 friend class Utils;
312 template<class F, class M> friend class Persistent;
313 template<class F> friend class PersistentBase;
314 template<class F> friend class Handle;
315 template<class F> friend class Local;
316 template <class F>
317 friend class MaybeLocal;
318 template<class F> friend class FunctionCallbackInfo;
319 template<class F> friend class PropertyCallbackInfo;
320 template<class F> friend class internal::CustomArguments;
321 friend Handle<Primitive> Undefined(Isolate* isolate);
322 friend Handle<Primitive> Null(Isolate* isolate);
323 friend Handle<Boolean> True(Isolate* isolate);
324 friend Handle<Boolean> False(Isolate* isolate);
325 friend class Context;
326 friend class HandleScope;
327 friend class Object;
328 friend class Private;
329
330 /**
331 * Creates a new handle for the specified value.
332 */
333 V8_INLINE explicit Handle(T* val) : val_(val) {}
334
335 V8_INLINE static Handle<T> New(Isolate* isolate, T* that);
336
337 T* val_;
338 };
339
340
341 /**
342 * A light-weight stack-allocated object handle. All operations
343 * that return objects from within v8 return them in local handles. They
344 * are created within HandleScopes, and all local handles allocated within a
345 * handle scope are destroyed when the handle scope is destroyed. Hence it
346 * is not necessary to explicitly deallocate local handles.
347 */
348 template <class T> class Local : public Handle<T> {
349 public:
350 V8_INLINE Local();
351 template <class S> V8_INLINE Local(Local<S> that)
352 : Handle<T>(reinterpret_cast<T*>(*that)) {
353 /**
354 * This check fails when trying to convert between incompatible
355 * handles. For example, converting from a Handle<String> to a
356 * Handle<Number>.
357 */
358 TYPE_CHECK(T, S);
359 }
360
361
362 template <class S> V8_INLINE static Local<T> Cast(Local<S> that) { 278 template <class S> V8_INLINE static Local<T> Cast(Local<S> that) {
363 #ifdef V8_ENABLE_CHECKS 279 #ifdef V8_ENABLE_CHECKS
364 // If we're going to perform the type check then we have to check 280 // If we're going to perform the type check then we have to check
365 // that the handle isn't empty before doing the checked cast. 281 // that the handle isn't empty before doing the checked cast.
366 if (that.IsEmpty()) return Local<T>(); 282 if (that.IsEmpty()) return Local<T>();
367 #endif 283 #endif
368 return Local<T>(T::Cast(*that)); 284 return Local<T>(T::Cast(*that));
369 } 285 }
370 template <class S> V8_INLINE Local(Handle<S> that) 286
371 : Handle<T>(reinterpret_cast<T*>(*that)) {
372 TYPE_CHECK(T, S);
373 }
374 287
375 template <class S> V8_INLINE Local<S> As() { 288 template <class S> V8_INLINE Local<S> As() {
376 return Local<S>::Cast(*this); 289 return Local<S>::Cast(*this);
377 } 290 }
378 291
379 /** 292 /**
380 * Create a local handle for the content of another handle. 293 * Create a local handle for the content of another handle.
381 * The referee is kept alive by the local handle even when 294 * The referee is kept alive by the local handle even when
382 * the original handle is destroyed/disposed. 295 * the original handle is destroyed/disposed.
383 */ 296 */
384 V8_INLINE static Local<T> New(Isolate* isolate, Handle<T> that); 297 V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that);
385 V8_INLINE static Local<T> New(Isolate* isolate, 298 V8_INLINE static Local<T> New(Isolate* isolate,
386 const PersistentBase<T>& that); 299 const PersistentBase<T>& that);
387 300
388 private: 301 private:
389 friend class Utils; 302 friend class Utils;
390 template<class F> friend class Eternal; 303 template<class F> friend class Eternal;
391 template<class F> friend class PersistentBase; 304 template<class F> friend class PersistentBase;
392 template<class F, class M> friend class Persistent; 305 template<class F, class M> friend class Persistent;
393 template<class F> friend class Handle;
394 template<class F> friend class Local; 306 template<class F> friend class Local;
395 template <class F> 307 template <class F>
396 friend class MaybeLocal; 308 friend class MaybeLocal;
397 template<class F> friend class FunctionCallbackInfo; 309 template<class F> friend class FunctionCallbackInfo;
398 template<class F> friend class PropertyCallbackInfo; 310 template<class F> friend class PropertyCallbackInfo;
399 friend class String; 311 friend class String;
400 friend class Object; 312 friend class Object;
401 friend class Context; 313 friend class Context;
314 friend class Private;
402 template<class F> friend class internal::CustomArguments; 315 template<class F> friend class internal::CustomArguments;
316 friend Local<Primitive> Undefined(Isolate* isolate);
317 friend Local<Primitive> Null(Isolate* isolate);
318 friend Local<Boolean> True(Isolate* isolate);
319 friend Local<Boolean> False(Isolate* isolate);
403 friend class HandleScope; 320 friend class HandleScope;
404 friend class EscapableHandleScope; 321 friend class EscapableHandleScope;
405 template <class F1, class F2, class F3> 322 template <class F1, class F2, class F3>
406 friend class PersistentValueMapBase; 323 friend class PersistentValueMapBase;
407 template<class F1, class F2> friend class PersistentValueVector; 324 template<class F1, class F2> friend class PersistentValueVector;
408 325
409 template <class S> V8_INLINE Local(S* that) : Handle<T>(that) { } 326 template <class S>
327 V8_INLINE Local(S* that)
328 : val_(that) {}
410 V8_INLINE static Local<T> New(Isolate* isolate, T* that); 329 V8_INLINE static Local<T> New(Isolate* isolate, T* that);
330 T* val_;
411 }; 331 };
412 332
413 333
334 // Handle is an alias for Local for historical reasons.
335 template <class T>
336 using Handle = Local<T>;
337
338
414 /** 339 /**
415 * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether 340 * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
416 * the Local<> is empty before it can be used. 341 * the Local<> is empty before it can be used.
417 * 342 *
418 * If an API method returns a MaybeLocal<>, the API method can potentially fail 343 * If an API method returns a MaybeLocal<>, the API method can potentially fail
419 * either because an exception is thrown, or because an exception is pending, 344 * either because an exception is thrown, or because an exception is pending,
420 * e.g. because a previous API call threw an exception that hasn't been caught 345 * e.g. because a previous API call threw an exception that hasn't been caught
421 * yet, or because a TerminateExecution exception was thrown. In that case, an 346 * yet, or because a TerminateExecution exception was thrown. In that case, an
422 * empty MaybeLocal is returned. 347 * empty MaybeLocal is returned.
423 */ 348 */
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 612
688 /** 613 /**
689 * Returns the class ID previously assigned to this handle or 0 if no class ID 614 * Returns the class ID previously assigned to this handle or 0 if no class ID
690 * was previously assigned. 615 * was previously assigned.
691 */ 616 */
692 V8_INLINE uint16_t WrapperClassId() const; 617 V8_INLINE uint16_t WrapperClassId() const;
693 618
694 private: 619 private:
695 friend class Isolate; 620 friend class Isolate;
696 friend class Utils; 621 friend class Utils;
697 template<class F> friend class Handle;
698 template<class F> friend class Local; 622 template<class F> friend class Local;
699 template<class F1, class F2> friend class Persistent; 623 template<class F1, class F2> friend class Persistent;
700 template <class F> 624 template <class F>
701 friend class Global; 625 friend class Global;
702 template<class F> friend class PersistentBase; 626 template<class F> friend class PersistentBase;
703 template<class F> friend class ReturnValue; 627 template<class F> friend class ReturnValue;
704 template <class F1, class F2, class F3> 628 template <class F1, class F2, class F3>
705 friend class PersistentValueMapBase; 629 friend class PersistentValueMapBase;
706 template<class F1, class F2> friend class PersistentValueVector; 630 template<class F1, class F2> friend class PersistentValueVector;
707 friend class Object; 631 friend class Object;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 } 754 }
831 755
832 // TODO(dcarney): this is pretty useless, fix or remove 756 // TODO(dcarney): this is pretty useless, fix or remove
833 template <class S> V8_INLINE Persistent<S>& As() { // NOLINT 757 template <class S> V8_INLINE Persistent<S>& As() { // NOLINT
834 return Persistent<S>::Cast(*this); 758 return Persistent<S>::Cast(*this);
835 } 759 }
836 760
837 private: 761 private:
838 friend class Isolate; 762 friend class Isolate;
839 friend class Utils; 763 friend class Utils;
840 template<class F> friend class Handle;
841 template<class F> friend class Local; 764 template<class F> friend class Local;
842 template<class F1, class F2> friend class Persistent; 765 template<class F1, class F2> friend class Persistent;
843 template<class F> friend class ReturnValue; 766 template<class F> friend class ReturnValue;
844 767
845 template <class S> V8_INLINE Persistent(S* that) : PersistentBase<T>(that) { } 768 template <class S> V8_INLINE Persistent(S* that) : PersistentBase<T>(that) { }
846 V8_INLINE T* operator*() const { return this->val_; } 769 V8_INLINE T* operator*() const { return this->val_; }
847 template<class S, class M2> 770 template<class S, class M2>
848 V8_INLINE void Copy(const Persistent<S, M2>& that); 771 V8_INLINE void Copy(const Persistent<S, M2>& that);
849 }; 772 };
850 773
(...skipping 5229 matching lines...) Expand 10 before | Expand all | Expand 10 after
6080 WeakCallbackInfo<void>::Callback weak_callback); 6003 WeakCallbackInfo<void>::Callback weak_callback);
6081 static void* ClearWeak(internal::Object** global_handle); 6004 static void* ClearWeak(internal::Object** global_handle);
6082 static void Eternalize(Isolate* isolate, 6005 static void Eternalize(Isolate* isolate,
6083 Value* handle, 6006 Value* handle,
6084 int* index); 6007 int* index);
6085 static Local<Value> GetEternal(Isolate* isolate, int index); 6008 static Local<Value> GetEternal(Isolate* isolate, int index);
6086 6009
6087 static void FromJustIsNothing(); 6010 static void FromJustIsNothing();
6088 static void ToLocalEmpty(); 6011 static void ToLocalEmpty();
6089 static void InternalFieldOutOfBounds(int index); 6012 static void InternalFieldOutOfBounds(int index);
6090
6091 template <class T> friend class Handle;
6092 template <class T> friend class Local; 6013 template <class T> friend class Local;
6093 template <class T> 6014 template <class T>
6094 friend class MaybeLocal; 6015 friend class MaybeLocal;
6095 template <class T> 6016 template <class T>
6096 friend class Maybe; 6017 friend class Maybe;
6097 template <class T> 6018 template <class T>
6098 friend class WeakCallbackInfo; 6019 friend class WeakCallbackInfo;
6099 template <class T> friend class Eternal; 6020 template <class T> friend class Eternal;
6100 template <class T> friend class PersistentBase; 6021 template <class T> friend class PersistentBase;
6101 template <class T, class M> friend class Persistent; 6022 template <class T, class M> friend class Persistent;
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
6883 int value_offset = 6804 int value_offset =
6884 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index); 6805 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
6885 return I::ReadField<T>(embedder_data, value_offset); 6806 return I::ReadField<T>(embedder_data, value_offset);
6886 } 6807 }
6887 }; 6808 };
6888 6809
6889 } // namespace internal 6810 } // namespace internal
6890 6811
6891 6812
6892 template <class T> 6813 template <class T>
6893 Local<T>::Local() : Handle<T>() { } 6814 Local<T> Local<T>::New(Isolate* isolate, Local<T> that) {
6894
6895
6896 template <class T>
6897 Local<T> Local<T>::New(Isolate* isolate, Handle<T> that) {
6898 return New(isolate, that.val_); 6815 return New(isolate, that.val_);
6899 } 6816 }
6900 6817
6901 template <class T> 6818 template <class T>
6902 Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) { 6819 Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) {
6903 return New(isolate, that.val_); 6820 return New(isolate, that.val_);
6904 } 6821 }
6905 6822
6906 template <class T>
6907 Handle<T> Handle<T>::New(Isolate* isolate, T* that) {
6908 if (that == NULL) return Handle<T>();
6909 T* that_ptr = that;
6910 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
6911 return Handle<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
6912 reinterpret_cast<internal::Isolate*>(isolate), *p)));
6913 }
6914
6915 6823
6916 template <class T> 6824 template <class T>
6917 Local<T> Local<T>::New(Isolate* isolate, T* that) { 6825 Local<T> Local<T>::New(Isolate* isolate, T* that) {
6918 if (that == NULL) return Local<T>(); 6826 if (that == NULL) return Local<T>();
6919 T* that_ptr = that; 6827 T* that_ptr = that;
6920 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); 6828 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
6921 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( 6829 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
6922 reinterpret_cast<internal::Isolate*>(isolate), *p))); 6830 reinterpret_cast<internal::Isolate*>(isolate), *p)));
6923 } 6831 }
6924 6832
(...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after
8098 */ 8006 */
8099 8007
8100 8008
8101 } // namespace v8 8009 } // namespace v8
8102 8010
8103 8011
8104 #undef TYPE_CHECK 8012 #undef TYPE_CHECK
8105 8013
8106 8014
8107 #endif // V8_H_ 8015 #endif // V8_H_
OLDNEW
« no previous file with comments | « no previous file | src/checks.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698