 Chromium Code Reviews
 Chromium Code Reviews Issue 222163002:
  Introduce MaybeHandle to police exception checking in handlified code.  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
    
  
    Issue 222163002:
  Introduce MaybeHandle to police exception checking in handlified code.  (Closed) 
  Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge| 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 16 matching lines...) Expand all Loading... | |
| 27 | 27 | 
| 28 #ifndef V8_HANDLES_H_ | 28 #ifndef V8_HANDLES_H_ | 
| 29 #define V8_HANDLES_H_ | 29 #define V8_HANDLES_H_ | 
| 30 | 30 | 
| 31 #include "allocation.h" | 31 #include "allocation.h" | 
| 32 #include "objects.h" | 32 #include "objects.h" | 
| 33 | 33 | 
| 34 namespace v8 { | 34 namespace v8 { | 
| 35 namespace internal { | 35 namespace internal { | 
| 36 | 36 | 
| 37 template<typename T> | |
| 38 class MaybeHandle { | |
| 
Michael Starzinger
2014/04/02 14:24:46
Can we get a few comments above this class about w
 | |
| 39 public: | |
| 40 MaybeHandle() : location_(NULL) { } | |
| 41 | |
| 42 // Constructor for handling automatic up casting. | |
| 43 // Ex. Handle<JSArray> can be passed when MaybeHandle<Object> is expected. | |
| 44 template <class S> MaybeHandle(Handle<S> handle) { | |
| 45 #ifdef DEBUG | |
| 46 T* a = NULL; | |
| 47 S* b = NULL; | |
| 48 a = b; // Fake assignment to enforce type checks. | |
| 49 USE(a); | |
| 50 #endif | |
| 51 this->location_ = reinterpret_cast<T**>(handle.location_); | |
| 52 } | |
| 53 | |
| 54 static MaybeHandle<T> Exception() { | |
| 55 return Handle<T>::null(); | |
| 56 } | |
| 57 | |
| 58 INLINE(Handle<T> ToHandleChecked()) { | |
| 59 CHECK(!is_null()); | |
| 60 return Handle<T>(location_); | |
| 61 } | |
| 62 | |
| 63 INLINE(bool ToHandle(Handle<T>* out)) { | |
| 64 if (is_null()) { | |
| 65 *out = Handle<T>::null(); | |
| 66 return false; | |
| 67 } else { | |
| 68 *out = Handle<T>(location_); | |
| 69 return true; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 INLINE(bool is_null()) const { return this->location_ == NULL; } | |
| 74 | |
| 75 protected: | |
| 76 // To access location_ from derived Handle<T> classes, we need to explicitly | |
| 77 // use this->location_, since the compiler does not resolve it correctly. | |
| 78 T** location_; | |
| 79 }; | |
| 80 | |
| 37 // ---------------------------------------------------------------------------- | 81 // ---------------------------------------------------------------------------- | 
| 38 // A Handle provides a reference to an object that survives relocation by | 82 // A Handle provides a reference to an object that survives relocation by | 
| 39 // the garbage collector. | 83 // the garbage collector. | 
| 40 // Handles are only valid within a HandleScope. | 84 // Handles are only valid within a HandleScope. | 
| 41 // When a handle is created for an object a cell is allocated in the heap. | 85 // When a handle is created for an object a cell is allocated in the heap. | 
| 42 | 86 | 
| 43 template<typename T> | 87 template<typename T> | 
| 44 class Handle { | 88 class Handle : public MaybeHandle<T> { | 
| 
Toon Verwaest
2014/04/02 14:40:24
I find it pretty confusing that a "checked handle"
 | |
| 45 public: | 89 public: | 
| 46 INLINE(explicit Handle(T** location)) { location_ = location; } | 90 INLINE(explicit Handle(T** location)) { this->location_ = location; } | 
| 47 INLINE(explicit Handle(T* obj)); | 91 INLINE(explicit Handle(T* obj)); | 
| 48 INLINE(Handle(T* obj, Isolate* isolate)); | 92 INLINE(Handle(T* obj, Isolate* isolate)); | 
| 49 | 93 | 
| 50 INLINE(Handle()) : location_(NULL) {} | 94 INLINE(Handle()) {} | 
| 
Michael Starzinger
2014/04/02 14:24:46
As discussed offline: The ultimate goal of this en
 | |
| 51 | 95 | 
| 52 // Constructor for handling automatic up casting. | 96 // Constructor for handling automatic up casting. | 
| 53 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 97 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 
| 54 template <class S> Handle(Handle<S> handle) { | 98 template <class S> Handle(Handle<S> handle) { | 
| 55 #ifdef DEBUG | 99 #ifdef DEBUG | 
| 56 T* a = NULL; | 100 T* a = NULL; | 
| 57 S* b = NULL; | 101 S* b = NULL; | 
| 58 a = b; // Fake assignment to enforce type checks. | 102 a = b; // Fake assignment to enforce type checks. | 
| 59 USE(a); | 103 USE(a); | 
| 60 #endif | 104 #endif | 
| 61 location_ = reinterpret_cast<T**>(handle.location_); | 105 this->location_ = reinterpret_cast<T**>(handle.location_); | 
| 62 } | 106 } | 
| 63 | 107 | 
| 64 INLINE(T* operator->() const) { return operator*(); } | 108 INLINE(T* operator->() const) { return operator*(); } | 
| 65 | 109 | 
| 66 // Check if this handle refers to the exact same object as the other handle. | 110 // Check if this handle refers to the exact same object as the other handle. | 
| 67 INLINE(bool is_identical_to(const Handle<T> other) const); | 111 INLINE(bool is_identical_to(const Handle<T> other) const); | 
| 68 | 112 | 
| 69 // Provides the C++ dereference operator. | 113 // Provides the C++ dereference operator. | 
| 70 INLINE(T* operator*() const); | 114 INLINE(T* operator*() const); | 
| 71 | 115 | 
| 72 // Returns the address to where the raw pointer is stored. | 116 // Returns the address to where the raw pointer is stored. | 
| 73 INLINE(T** location() const); | 117 INLINE(T** location() const); | 
| 74 | 118 | 
| 75 template <class S> static Handle<T> cast(Handle<S> that) { | 119 template <class S> static Handle<T> cast(Handle<S> that) { | 
| 76 T::cast(*reinterpret_cast<T**>(that.location_)); | 120 T::cast(*reinterpret_cast<T**>(that.location_)); | 
| 77 return Handle<T>(reinterpret_cast<T**>(that.location_)); | 121 return Handle<T>(reinterpret_cast<T**>(that.location_)); | 
| 78 } | 122 } | 
| 79 | 123 | 
| 80 static Handle<T> null() { return Handle<T>(); } | 124 static Handle<T> null() { return Handle<T>(); } | 
| 
Michael Starzinger
2014/04/02 14:24:46
Likewise.
 | |
| 81 bool is_null() const { return location_ == NULL; } | |
| 82 | 125 | 
| 83 // Closes the given scope, but lets this handle escape. See | 126 // Closes the given scope, but lets this handle escape. See | 
| 84 // implementation in api.h. | 127 // implementation in api.h. | 
| 85 inline Handle<T> EscapeFrom(v8::EscapableHandleScope* scope); | 128 inline Handle<T> EscapeFrom(v8::EscapableHandleScope* scope); | 
| 86 | 129 | 
| 87 #ifdef DEBUG | 130 #ifdef DEBUG | 
| 88 enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK }; | 131 enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK }; | 
| 89 | 132 | 
| 90 bool IsDereferenceAllowed(DereferenceCheckMode mode) const; | 133 bool IsDereferenceAllowed(DereferenceCheckMode mode) const; | 
| 91 #endif // DEBUG | 134 #endif // DEBUG | 
| 92 | 135 | 
| 93 private: | 136 private: | 
| 94 T** location_; | |
| 95 | |
| 96 // Handles of different classes are allowed to access each other's location_. | 137 // Handles of different classes are allowed to access each other's location_. | 
| 97 template<class S> friend class Handle; | 138 template<class S> friend class Handle; | 
| 139 template<class S> friend class MaybeHandle; | |
| 98 }; | 140 }; | 
| 99 | 141 | 
| 100 | 142 | 
| 101 // Convenience wrapper. | 143 // Convenience wrapper. | 
| 102 template<class T> | 144 template<class T> | 
| 103 inline Handle<T> handle(T* t, Isolate* isolate) { | 145 inline Handle<T> handle(T* t, Isolate* isolate) { | 
| 104 return Handle<T>(t, isolate); | 146 return Handle<T>(t, isolate); | 
| 105 } | 147 } | 
| 106 | 148 | 
| 107 | 149 | 
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 | 361 | 
| 320 void Initialize() { | 362 void Initialize() { | 
| 321 next = limit = NULL; | 363 next = limit = NULL; | 
| 322 level = 0; | 364 level = 0; | 
| 323 } | 365 } | 
| 324 }; | 366 }; | 
| 325 | 367 | 
| 326 } } // namespace v8::internal | 368 } } // namespace v8::internal | 
| 327 | 369 | 
| 328 #endif // V8_HANDLES_H_ | 370 #endif // V8_HANDLES_H_ | 
| OLD | NEW |