Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 21 matching lines...) Expand all Loading... | |
| 32 | 32 |
| 33 namespace v8 { | 33 namespace v8 { |
| 34 namespace internal { | 34 namespace internal { |
| 35 | 35 |
| 36 // ---------------------------------------------------------------------------- | 36 // ---------------------------------------------------------------------------- |
| 37 // A Handle provides a reference to an object that survives relocation by | 37 // A Handle provides a reference to an object that survives relocation by |
| 38 // the garbage collector. | 38 // the garbage collector. |
| 39 // Handles are only valid within a HandleScope. | 39 // Handles are only valid within a HandleScope. |
| 40 // When a handle is created for an object a cell is allocated in the heap. | 40 // When a handle is created for an object a cell is allocated in the heap. |
| 41 | 41 |
| 42 template<class T> | 42 template<typename T> |
| 43 class Handle { | 43 class Handle { |
| 44 public: | 44 public: |
| 45 INLINE(explicit Handle(T** location)) { location_ = location; } | 45 INLINE(explicit Handle(T** location)) { location_ = location; } |
| 46 INLINE(explicit Handle(T* obj)); | 46 INLINE(explicit Handle(T* obj)); |
| 47 | 47 |
| 48 INLINE(Handle()) : location_(NULL) {} | 48 INLINE(Handle()) : location_(NULL) {} |
| 49 | 49 |
| 50 // Constructor for handling automatic up casting. | 50 // Constructor for handling automatic up casting. |
| 51 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 51 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
| 52 template <class S> Handle(Handle<S> handle) { | 52 template <class S> Handle(Handle<S> handle) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 | 86 |
| 87 // Closes the given scope, but lets this handle escape. See | 87 // Closes the given scope, but lets this handle escape. See |
| 88 // implementation in api.h. | 88 // implementation in api.h. |
| 89 inline Handle<T> EscapeFrom(v8::HandleScope* scope); | 89 inline Handle<T> EscapeFrom(v8::HandleScope* scope); |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 T** location_; | 92 T** location_; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 | 95 |
| 96 // A handle-scope based variable. The value stored in the variable can change | |
| 97 // over time. The value stored in the variable at any time is a root | |
| 98 // for garbage collection. | |
| 99 // The variable is backed by the current HandleScope. | |
| 100 template <typename T> | |
| 101 class RootVar { | |
|
Erik Corry
2011/02/24 10:36:57
I think I would prefer MutableHandle as a name wit
Lasse Reichstein
2011/02/24 13:44:41
Renamed to HandleCell.
The name MutableHandle seem
| |
| 102 public: | |
| 103 // Create a new RootVar holding the given value. | |
| 104 explicit RootVar(Handle<T> value); | |
| 105 explicit RootVar(T* value); | |
| 106 | |
| 107 // Create an alias of an existing RootVar. | |
| 108 explicit RootVar(const RootVar<T>& value) : location_(value.location_) { } | |
| 109 | |
| 110 INLINE(T* operator->() const) { return operator*(); } | |
| 111 INLINE(T* operator*() const) { | |
| 112 return *location_; | |
| 113 } | |
| 114 INLINE(void operator=(T* value)) { | |
| 115 *location_ = value; | |
| 116 } | |
| 117 INLINE(void operator=(Handle<T> value)) { | |
|
Erik Corry
2011/02/24 10:36:57
Would it be premature generalization to provide th
Lasse Reichstein
2011/02/24 13:44:41
I would prefer only having explicit ways to extrac
| |
| 118 *location_ = *value; | |
| 119 } | |
| 120 INLINE(void operator=(const RootVar<T>& value)) { | |
| 121 *location_ = *value.location_; | |
| 122 } | |
| 123 | |
| 124 // Extract the value of the variable and cast it to a give type. | |
| 125 // This is typically used for calling methods on a more specialized type. | |
| 126 template <typename S> | |
| 127 inline S* cast() { | |
| 128 S::cast(*location_); | |
| 129 return *reinterpret_cast<S**>(location_); | |
| 130 } | |
| 131 | |
| 132 Handle<T> ToHandle() const { | |
| 133 return Handle<T>(*location_); | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 // Prevent implicit constructor from being created. | |
| 138 RootVar(); | |
| 139 | |
| 140 T** location_; | |
| 141 }; | |
| 142 | |
| 143 | |
| 96 // A stack-allocated class that governs a number of local handles. | 144 // A stack-allocated class that governs a number of local handles. |
| 97 // After a handle scope has been created, all local handles will be | 145 // After a handle scope has been created, all local handles will be |
| 98 // allocated within that handle scope until either the handle scope is | 146 // allocated within that handle scope until either the handle scope is |
| 99 // deleted or another handle scope is created. If there is already a | 147 // deleted or another handle scope is created. If there is already a |
| 100 // handle scope and a new one is created, all allocations will take | 148 // handle scope and a new one is created, all allocations will take |
| 101 // place in the new handle scope until it is deleted. After that, | 149 // place in the new handle scope until it is deleted. After that, |
| 102 // new handles will again be allocated in the original handle scope. | 150 // new handles will again be allocated in the original handle scope. |
| 103 // | 151 // |
| 104 // After the handle scope of a local handle has been deleted the | 152 // After the handle scope of a local handle has been deleted the |
| 105 // garbage collector will no longer track the object stored in the | 153 // garbage collector will no longer track the object stored in the |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 private: | 432 private: |
| 385 bool has_been_transformed_; // Tells whether the object has been transformed. | 433 bool has_been_transformed_; // Tells whether the object has been transformed. |
| 386 int unused_property_fields_; // Captures the unused number of field. | 434 int unused_property_fields_; // Captures the unused number of field. |
| 387 Handle<JSObject> object_; // The object being optimized. | 435 Handle<JSObject> object_; // The object being optimized. |
| 388 }; | 436 }; |
| 389 | 437 |
| 390 | 438 |
| 391 } } // namespace v8::internal | 439 } } // namespace v8::internal |
| 392 | 440 |
| 393 #endif // V8_HANDLES_H_ | 441 #endif // V8_HANDLES_H_ |
| OLD | NEW |