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 |
| 38 // A Handle can be converted into a MaybeHandle. Converting a MaybeHandle |
| 39 // into a Handle requires checking that it does not point to NULL. |
| 40 |
| 41 template<typename T> |
| 42 class MaybeHandle { |
| 43 public: |
| 44 INLINE(MaybeHandle()) : location_(NULL) { } |
| 45 |
| 46 // Constructor for handling automatic up casting from Handle. |
| 47 // Ex. Handle<JSArray> can be passed when MaybeHandle<Object> is expected. |
| 48 template <class S> MaybeHandle(Handle<S> handle) { |
| 49 #ifdef DEBUG |
| 50 T* a = NULL; |
| 51 S* b = NULL; |
| 52 a = b; // Fake assignment to enforce type checks. |
| 53 USE(a); |
| 54 #endif |
| 55 this->location_ = reinterpret_cast<T**>(handle.location()); |
| 56 } |
| 57 |
| 58 // Constructor for handling automatic up casting. |
| 59 // Ex. MaybeHandle<JSArray> can be passed when Handle<Object> is expected. |
| 60 template <class S> MaybeHandle(MaybeHandle<S> maybe_handle) { |
| 61 #ifdef DEBUG |
| 62 T* a = NULL; |
| 63 S* b = NULL; |
| 64 a = b; // Fake assignment to enforce type checks. |
| 65 USE(a); |
| 66 #endif |
| 67 location_ = reinterpret_cast<T**>(maybe_handle.location_); |
| 68 } |
| 69 |
| 70 INLINE(Handle<T> ToHandleChecked()) { |
| 71 CHECK(location_ != NULL); |
| 72 return Handle<T>(location_); |
| 73 } |
| 74 |
| 75 INLINE(bool ToHandle(Handle<T>* out)) { |
| 76 if (location_ == NULL) { |
| 77 *out = Handle<T>::null(); |
| 78 return false; |
| 79 } else { |
| 80 *out = Handle<T>(location_); |
| 81 return true; |
| 82 } |
| 83 } |
| 84 |
| 85 protected: |
| 86 T** location_; |
| 87 |
| 88 // MaybeHandles of different classes are allowed to access each |
| 89 // other's location_. |
| 90 template<class S> friend class MaybeHandle; |
| 91 }; |
| 92 |
37 // ---------------------------------------------------------------------------- | 93 // ---------------------------------------------------------------------------- |
38 // A Handle provides a reference to an object that survives relocation by | 94 // A Handle provides a reference to an object that survives relocation by |
39 // the garbage collector. | 95 // the garbage collector. |
40 // Handles are only valid within a HandleScope. | 96 // Handles are only valid within a HandleScope. |
41 // When a handle is created for an object a cell is allocated in the heap. | 97 // When a handle is created for an object a cell is allocated in the heap. |
42 | 98 |
43 template<typename T> | 99 template<typename T> |
44 class Handle { | 100 class Handle { |
45 public: | 101 public: |
46 INLINE(explicit Handle(T** location)) { location_ = location; } | 102 INLINE(explicit Handle(T** location)) { location_ = location; } |
47 INLINE(explicit Handle(T* obj)); | 103 INLINE(explicit Handle(T* obj)); |
48 INLINE(Handle(T* obj, Isolate* isolate)); | 104 INLINE(Handle(T* obj, Isolate* isolate)); |
49 | 105 |
50 INLINE(Handle()) : location_(NULL) {} | 106 // TODO(yangguo): Values that contain empty handles should be declared as |
| 107 // MaybeHandle to force validation before being used as handles. |
| 108 INLINE(Handle()) : location_(NULL) { } |
51 | 109 |
52 // Constructor for handling automatic up casting. | 110 // Constructor for handling automatic up casting. |
53 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 111 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
54 template <class S> Handle(Handle<S> handle) { | 112 template <class S> Handle(Handle<S> handle) { |
55 #ifdef DEBUG | 113 #ifdef DEBUG |
56 T* a = NULL; | 114 T* a = NULL; |
57 S* b = NULL; | 115 S* b = NULL; |
58 a = b; // Fake assignment to enforce type checks. | 116 a = b; // Fake assignment to enforce type checks. |
59 USE(a); | 117 USE(a); |
60 #endif | 118 #endif |
61 location_ = reinterpret_cast<T**>(handle.location_); | 119 location_ = reinterpret_cast<T**>(handle.location_); |
62 } | 120 } |
63 | 121 |
64 INLINE(T* operator->() const) { return operator*(); } | 122 INLINE(T* operator->() const) { return operator*(); } |
65 | 123 |
66 // Check if this handle refers to the exact same object as the other handle. | 124 // 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); | 125 INLINE(bool is_identical_to(const Handle<T> other) const); |
68 | 126 |
69 // Provides the C++ dereference operator. | 127 // Provides the C++ dereference operator. |
70 INLINE(T* operator*() const); | 128 INLINE(T* operator*() const); |
71 | 129 |
72 // Returns the address to where the raw pointer is stored. | 130 // Returns the address to where the raw pointer is stored. |
73 INLINE(T** location() const); | 131 INLINE(T** location() const); |
74 | 132 |
75 template <class S> static Handle<T> cast(Handle<S> that) { | 133 template <class S> static Handle<T> cast(Handle<S> that) { |
76 T::cast(*reinterpret_cast<T**>(that.location_)); | 134 T::cast(*reinterpret_cast<T**>(that.location_)); |
77 return Handle<T>(reinterpret_cast<T**>(that.location_)); | 135 return Handle<T>(reinterpret_cast<T**>(that.location_)); |
78 } | 136 } |
79 | 137 |
| 138 // TODO(yangguo): Values that contain empty handles should be declared as |
| 139 // MaybeHandle to force validation before being used as handles. |
80 static Handle<T> null() { return Handle<T>(); } | 140 static Handle<T> null() { return Handle<T>(); } |
81 bool is_null() const { return location_ == NULL; } | 141 bool is_null() const { return location_ == NULL; } |
82 | 142 |
83 // Closes the given scope, but lets this handle escape. See | 143 // Closes the given scope, but lets this handle escape. See |
84 // implementation in api.h. | 144 // implementation in api.h. |
85 inline Handle<T> EscapeFrom(v8::EscapableHandleScope* scope); | 145 inline Handle<T> EscapeFrom(v8::EscapableHandleScope* scope); |
86 | 146 |
87 #ifdef DEBUG | 147 #ifdef DEBUG |
88 enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK }; | 148 enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK }; |
89 | 149 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 | 379 |
320 void Initialize() { | 380 void Initialize() { |
321 next = limit = NULL; | 381 next = limit = NULL; |
322 level = 0; | 382 level = 0; |
323 } | 383 } |
324 }; | 384 }; |
325 | 385 |
326 } } // namespace v8::internal | 386 } } // namespace v8::internal |
327 | 387 |
328 #endif // V8_HANDLES_H_ | 388 #endif // V8_HANDLES_H_ |
OLD | NEW |