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 26 matching lines...) Expand all Loading... |
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<typename 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 INLINE(Handle(T* obj, Isolate* isolate)); |
47 | 48 |
48 INLINE(Handle()) : location_(NULL) {} | 49 INLINE(Handle()) : location_(NULL) {} |
49 | 50 |
50 // Constructor for handling automatic up casting. | 51 // Constructor for handling automatic up casting. |
51 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 52 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
52 template <class S> Handle(Handle<S> handle) { | 53 template <class S> Handle(Handle<S> handle) { |
53 #ifdef DEBUG | 54 #ifdef DEBUG |
54 T* a = NULL; | 55 T* a = NULL; |
55 S* b = NULL; | 56 S* b = NULL; |
56 a = b; // Fake assignment to enforce type checks. | 57 a = b; // Fake assignment to enforce type checks. |
(...skipping 18 matching lines...) Expand all Loading... |
75 reinterpret_cast<Address>(*location_) != kZapValue); | 76 reinterpret_cast<Address>(*location_) != kZapValue); |
76 return location_; | 77 return location_; |
77 } | 78 } |
78 | 79 |
79 template <class S> static Handle<T> cast(Handle<S> that) { | 80 template <class S> static Handle<T> cast(Handle<S> that) { |
80 T::cast(*that); | 81 T::cast(*that); |
81 return Handle<T>(reinterpret_cast<T**>(that.location())); | 82 return Handle<T>(reinterpret_cast<T**>(that.location())); |
82 } | 83 } |
83 | 84 |
84 static Handle<T> null() { return Handle<T>(); } | 85 static Handle<T> null() { return Handle<T>(); } |
85 bool is_null() { return location_ == NULL; } | 86 bool is_null() const { return location_ == NULL; } |
86 | 87 |
87 // Closes the given scope, but lets this handle escape. See | 88 // Closes the given scope, but lets this handle escape. See |
88 // implementation in api.h. | 89 // implementation in api.h. |
89 inline Handle<T> EscapeFrom(v8::HandleScope* scope); | 90 inline Handle<T> EscapeFrom(v8::HandleScope* scope); |
90 | 91 |
91 private: | 92 private: |
92 T** location_; | 93 T** location_; |
93 }; | 94 }; |
94 | 95 |
95 | 96 |
96 // A stack-allocated class that governs a number of local handles. | 97 // A stack-allocated class that governs a number of local handles. |
97 // After a handle scope has been created, all local handles will be | 98 // After a handle scope has been created, all local handles will be |
98 // allocated within that handle scope until either the handle scope is | 99 // allocated within that handle scope until either the handle scope is |
99 // deleted or another handle scope is created. If there is already a | 100 // 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 | 101 // 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, | 102 // place in the new handle scope until it is deleted. After that, |
102 // new handles will again be allocated in the original handle scope. | 103 // new handles will again be allocated in the original handle scope. |
103 // | 104 // |
104 // After the handle scope of a local handle has been deleted the | 105 // After the handle scope of a local handle has been deleted the |
105 // garbage collector will no longer track the object stored in the | 106 // garbage collector will no longer track the object stored in the |
106 // handle and may deallocate it. The behavior of accessing a handle | 107 // handle and may deallocate it. The behavior of accessing a handle |
107 // for which the handle scope has been deleted is undefined. | 108 // for which the handle scope has been deleted is undefined. |
108 class HandleScope { | 109 class HandleScope { |
109 public: | 110 public: |
110 HandleScope() : prev_next_(current_.next), prev_limit_(current_.limit) { | 111 inline HandleScope(); |
111 current_.level++; | 112 explicit inline HandleScope(Isolate* isolate); |
112 } | |
113 | 113 |
114 ~HandleScope() { | 114 inline ~HandleScope(); |
115 CloseScope(); | |
116 } | |
117 | 115 |
118 // Counts the number of allocated handles. | 116 // Counts the number of allocated handles. |
119 static int NumberOfHandles(); | 117 static int NumberOfHandles(); |
120 | 118 |
121 // Creates a new handle with the given value. | 119 // Creates a new handle with the given value. |
122 template <typename T> | 120 template <typename T> |
123 static inline T** CreateHandle(T* value) { | 121 static inline T** CreateHandle(T* value, Isolate* isolate); |
124 internal::Object** cur = current_.next; | |
125 if (cur == current_.limit) cur = Extend(); | |
126 // Update the current next field, set the value in the created | |
127 // handle, and return the result. | |
128 ASSERT(cur < current_.limit); | |
129 current_.next = cur + 1; | |
130 | |
131 T** result = reinterpret_cast<T**>(cur); | |
132 *result = value; | |
133 return result; | |
134 } | |
135 | 122 |
136 // Deallocates any extensions used by the current scope. | 123 // Deallocates any extensions used by the current scope. |
137 static void DeleteExtensions(); | 124 static void DeleteExtensions(Isolate* isolate); |
138 | 125 |
139 static Address current_next_address(); | 126 static Address current_next_address(); |
140 static Address current_limit_address(); | 127 static Address current_limit_address(); |
141 static Address current_level_address(); | 128 static Address current_level_address(); |
142 | 129 |
143 // Closes the HandleScope (invalidating all handles | 130 // Closes the HandleScope (invalidating all handles |
144 // created in the scope of the HandleScope) and returns | 131 // created in the scope of the HandleScope) and returns |
145 // a Handle backed by the parent scope holding the | 132 // a Handle backed by the parent scope holding the |
146 // value of the argument handle. | 133 // value of the argument handle. |
147 template <typename T> | 134 template <typename T> |
148 Handle<T> CloseAndEscape(Handle<T> handle_value) { | 135 Handle<T> CloseAndEscape(Handle<T> handle_value); |
149 T* value = *handle_value; | 136 |
150 // Throw away all handles in the current scope. | 137 Isolate* isolate() { return isolate_; } |
151 CloseScope(); | |
152 // Allocate one handle in the parent scope. | |
153 ASSERT(current_.level > 0); | |
154 Handle<T> result(CreateHandle<T>(value)); | |
155 // Reinitialize the current scope (so that it's ready | |
156 // to be used or closed again). | |
157 prev_next_ = current_.next; | |
158 prev_limit_ = current_.limit; | |
159 current_.level++; | |
160 return result; | |
161 } | |
162 | 138 |
163 private: | 139 private: |
164 // Prevent heap allocation or illegal handle scopes. | 140 // Prevent heap allocation or illegal handle scopes. |
165 HandleScope(const HandleScope&); | 141 HandleScope(const HandleScope&); |
166 void operator=(const HandleScope&); | 142 void operator=(const HandleScope&); |
167 void* operator new(size_t size); | 143 void* operator new(size_t size); |
168 void operator delete(void* size_t); | 144 void operator delete(void* size_t); |
169 | 145 |
170 inline void CloseScope() { | 146 inline void CloseScope(); |
171 current_.next = prev_next_; | |
172 current_.level--; | |
173 if (current_.limit != prev_limit_) { | |
174 current_.limit = prev_limit_; | |
175 DeleteExtensions(); | |
176 } | |
177 #ifdef DEBUG | |
178 ZapRange(prev_next_, prev_limit_); | |
179 #endif | |
180 } | |
181 | 147 |
182 static v8::ImplementationUtilities::HandleScopeData current_; | 148 Isolate* isolate_; |
183 // Holds values on entry. The prev_next_ value is never NULL | |
184 // on_entry, but is set to NULL when this scope is closed. | |
185 Object** prev_next_; | 149 Object** prev_next_; |
186 Object** prev_limit_; | 150 Object** prev_limit_; |
187 | 151 |
188 // Extend the handle scope making room for more handles. | 152 // Extend the handle scope making room for more handles. |
189 static internal::Object** Extend(); | 153 static internal::Object** Extend(); |
190 | 154 |
191 // Zaps the handles in the half-open interval [start, end). | 155 // Zaps the handles in the half-open interval [start, end). |
192 static void ZapRange(internal::Object** start, internal::Object** end); | 156 static void ZapRange(internal::Object** start, internal::Object** end); |
193 | 157 |
194 friend class v8::HandleScope; | 158 friend class v8::HandleScope; |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 private: | 382 private: |
419 bool has_been_transformed_; // Tells whether the object has been transformed. | 383 bool has_been_transformed_; // Tells whether the object has been transformed. |
420 int unused_property_fields_; // Captures the unused number of field. | 384 int unused_property_fields_; // Captures the unused number of field. |
421 Handle<JSObject> object_; // The object being optimized. | 385 Handle<JSObject> object_; // The object being optimized. |
422 }; | 386 }; |
423 | 387 |
424 | 388 |
425 } } // namespace v8::internal | 389 } } // namespace v8::internal |
426 | 390 |
427 #endif // V8_HANDLES_H_ | 391 #endif // V8_HANDLES_H_ |
OLD | NEW |