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

Side by Side Diff: include/v8.h

Issue 1022803002: Clarify what APIs return Maybe and MaybeLocal values (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « no previous file | no next file » | 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 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 friend class EscapableHandleScope; 404 friend class EscapableHandleScope;
405 template <class F1, class F2, class F3> 405 template <class F1, class F2, class F3>
406 friend class PersistentValueMapBase; 406 friend class PersistentValueMapBase;
407 template<class F1, class F2> friend class PersistentValueVector; 407 template<class F1, class F2> friend class PersistentValueVector;
408 408
409 template <class S> V8_INLINE Local(S* that) : Handle<T>(that) { } 409 template <class S> V8_INLINE Local(S* that) : Handle<T>(that) { }
410 V8_INLINE static Local<T> New(Isolate* isolate, T* that); 410 V8_INLINE static Local<T> New(Isolate* isolate, T* that);
411 }; 411 };
412 412
413 413
414 /**
415 * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
416 * the Local<> is empty before it can be used.
417 *
418 * 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,
420 * 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
422 * empty MaybeLocal is returned.
423 */
414 template <class T> 424 template <class T>
415 class MaybeLocal { 425 class MaybeLocal {
416 public: 426 public:
417 V8_INLINE MaybeLocal() : val_(nullptr) {} 427 V8_INLINE MaybeLocal() : val_(nullptr) {}
418 template <class S> 428 template <class S>
419 V8_INLINE MaybeLocal(Local<S> that) 429 V8_INLINE MaybeLocal(Local<S> that)
420 : val_(reinterpret_cast<T*>(*that)) { 430 : val_(reinterpret_cast<T*>(*that)) {
421 TYPE_CHECK(T, S); 431 TYPE_CHECK(T, S);
422 } 432 }
423 433
424 V8_INLINE bool IsEmpty() const { return val_ == nullptr; } 434 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
425 435
426 template <class S> 436 template <class S>
427 V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const { 437 V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const {
428 out->val_ = IsEmpty() ? nullptr : this->val_; 438 out->val_ = IsEmpty() ? nullptr : this->val_;
429 return !IsEmpty(); 439 return !IsEmpty();
430 } 440 }
431 441
442 // Will crash when checks are enabled if the MaybeLocal<> is empty.
432 V8_INLINE Local<T> ToLocalChecked(); 443 V8_INLINE Local<T> ToLocalChecked();
433 444
434 template <class S> 445 template <class S>
435 V8_INLINE Local<S> FromMaybe(Local<S> default_value) const { 446 V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
436 return IsEmpty() ? default_value : Local<S>(val_); 447 return IsEmpty() ? default_value : Local<S>(val_);
437 } 448 }
438 449
439 private: 450 private:
440 T* val_; 451 T* val_;
441 }; 452 };
(...skipping 5530 matching lines...) Expand 10 before | Expand all | Expand 10 after
5972 template <class T> friend class Eternal; 5983 template <class T> friend class Eternal;
5973 template <class T> friend class PersistentBase; 5984 template <class T> friend class PersistentBase;
5974 template <class T, class M> friend class Persistent; 5985 template <class T, class M> friend class Persistent;
5975 friend class Context; 5986 friend class Context;
5976 }; 5987 };
5977 5988
5978 5989
5979 /** 5990 /**
5980 * A simple Maybe type, representing an object which may or may not have a 5991 * A simple Maybe type, representing an object which may or may not have a
5981 * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html. 5992 * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
5993 *
5994 * If an API method returns a Maybe<>, the API method can potentially fail
5995 * either because an exception is thrown, or because an exception is pending,
5996 * e.g. because a previous API call threw an exception that hasn't been caught
5997 * yet, or because a TerminateExecution exception was thrown. In that case, a
5998 * "Nothing" value is returned.
5982 */ 5999 */
5983 template <class T> 6000 template <class T>
5984 class Maybe { 6001 class Maybe {
5985 public: 6002 public:
5986 V8_INLINE bool IsNothing() const { return !has_value; } 6003 V8_INLINE bool IsNothing() const { return !has_value; }
5987 V8_INLINE bool IsJust() const { return has_value; } 6004 V8_INLINE bool IsJust() const { return has_value; }
5988 6005
6006 // Will crash when checks are enabled if the Maybe<> is nothing.
5989 V8_INLINE T FromJust() const { 6007 V8_INLINE T FromJust() const {
5990 #ifdef V8_ENABLE_CHECKS 6008 #ifdef V8_ENABLE_CHECKS
5991 V8::CheckIsJust(IsJust()); 6009 V8::CheckIsJust(IsJust());
5992 #endif 6010 #endif
5993 return value; 6011 return value;
5994 } 6012 }
5995 6013
5996 V8_INLINE T FromMaybe(const T& default_value) const { 6014 V8_INLINE T FromMaybe(const T& default_value) const {
5997 return has_value ? value : default_value; 6015 return has_value ? value : default_value;
5998 } 6016 }
(...skipping 1957 matching lines...) Expand 10 before | Expand all | Expand 10 after
7956 */ 7974 */
7957 7975
7958 7976
7959 } // namespace v8 7977 } // namespace v8
7960 7978
7961 7979
7962 #undef TYPE_CHECK 7980 #undef TYPE_CHECK
7963 7981
7964 7982
7965 #endif // V8_H_ 7983 #endif // V8_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698