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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // handle scope and a new one is created, all allocations will take | 100 // 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, | 101 // place in the new handle scope until it is deleted. After that, |
102 // new handles will again be allocated in the original handle scope. | 102 // new handles will again be allocated in the original handle scope. |
103 // | 103 // |
104 // After the handle scope of a local handle has been deleted the | 104 // After the handle scope of a local handle has been deleted the |
105 // garbage collector will no longer track the object stored in the | 105 // garbage collector will no longer track the object stored in the |
106 // handle and may deallocate it. The behavior of accessing a handle | 106 // handle and may deallocate it. The behavior of accessing a handle |
107 // for which the handle scope has been deleted is undefined. | 107 // for which the handle scope has been deleted is undefined. |
108 class HandleScope { | 108 class HandleScope { |
109 public: | 109 public: |
110 HandleScope() : previous_(current_) { | 110 HandleScope() : prev_next_(current_.next), prev_limit_(current_.limit) { |
111 current_.extensions = 0; | 111 current_.level++; |
112 } | 112 } |
113 | 113 |
114 ~HandleScope() { | 114 ~HandleScope() { |
115 Leave(&previous_); | 115 current_.next = prev_next_; |
| 116 current_.level--; |
| 117 if (current_.limit != prev_limit_) { |
| 118 current_.limit = prev_limit_; |
| 119 DeleteExtensions(); |
| 120 } |
| 121 #ifdef DEBUG |
| 122 ZapRange(prev_next_, prev_limit_); |
| 123 #endif |
116 } | 124 } |
117 | 125 |
118 // Counts the number of allocated handles. | 126 // Counts the number of allocated handles. |
119 static int NumberOfHandles(); | 127 static int NumberOfHandles(); |
120 | 128 |
121 // Creates a new handle with the given value. | 129 // Creates a new handle with the given value. |
122 template <typename T> | 130 template <typename T> |
123 static inline T** CreateHandle(T* value) { | 131 static inline T** CreateHandle(T* value) { |
124 internal::Object** cur = current_.next; | 132 internal::Object** cur = current_.next; |
125 if (cur == current_.limit) cur = Extend(); | 133 if (cur == current_.limit) cur = Extend(); |
126 // Update the current next field, set the value in the created | 134 // Update the current next field, set the value in the created |
127 // handle, and return the result. | 135 // handle, and return the result. |
128 ASSERT(cur < current_.limit); | 136 ASSERT(cur < current_.limit); |
129 current_.next = cur + 1; | 137 current_.next = cur + 1; |
130 | 138 |
131 T** result = reinterpret_cast<T**>(cur); | 139 T** result = reinterpret_cast<T**>(cur); |
132 *result = value; | 140 *result = value; |
133 return result; | 141 return result; |
134 } | 142 } |
135 | 143 |
136 // Deallocates any extensions used by the current scope. | 144 // Deallocates any extensions used by the current scope. |
137 static void DeleteExtensions(); | 145 static void DeleteExtensions(); |
138 | 146 |
139 static Address current_extensions_address(); | |
140 static Address current_next_address(); | 147 static Address current_next_address(); |
141 static Address current_limit_address(); | 148 static Address current_limit_address(); |
| 149 static Address current_level_address(); |
142 | 150 |
143 private: | 151 private: |
144 // Prevent heap allocation or illegal handle scopes. | 152 // Prevent heap allocation or illegal handle scopes. |
145 HandleScope(const HandleScope&); | 153 HandleScope(const HandleScope&); |
146 void operator=(const HandleScope&); | 154 void operator=(const HandleScope&); |
147 void* operator new(size_t size); | 155 void* operator new(size_t size); |
148 void operator delete(void* size_t); | 156 void operator delete(void* size_t); |
149 | 157 |
150 static v8::ImplementationUtilities::HandleScopeData current_; | 158 static v8::ImplementationUtilities::HandleScopeData current_; |
151 const v8::ImplementationUtilities::HandleScopeData previous_; | 159 Object** const prev_next_; |
152 | 160 Object** const prev_limit_; |
153 // Pushes a fresh handle scope to be used when allocating new handles. | |
154 static void Enter( | |
155 v8::ImplementationUtilities::HandleScopeData* previous) { | |
156 *previous = current_; | |
157 current_.extensions = 0; | |
158 } | |
159 | |
160 // Re-establishes the previous scope state. Should be called only | |
161 // once, and only for the current scope. | |
162 static void Leave( | |
163 const v8::ImplementationUtilities::HandleScopeData* previous) { | |
164 if (current_.extensions > 0) { | |
165 DeleteExtensions(); | |
166 } | |
167 current_ = *previous; | |
168 #ifdef DEBUG | |
169 ZapRange(current_.next, current_.limit); | |
170 #endif | |
171 } | |
172 | 161 |
173 // Extend the handle scope making room for more handles. | 162 // Extend the handle scope making room for more handles. |
174 static internal::Object** Extend(); | 163 static internal::Object** Extend(); |
175 | 164 |
176 // Zaps the handles in the half-open interval [start, end). | 165 // Zaps the handles in the half-open interval [start, end). |
177 static void ZapRange(internal::Object** start, internal::Object** end); | 166 static void ZapRange(internal::Object** start, internal::Object** end); |
178 | 167 |
179 friend class v8::HandleScope; | 168 friend class v8::HandleScope; |
180 friend class v8::ImplementationUtilities; | 169 friend class v8::ImplementationUtilities; |
181 }; | 170 }; |
182 | 171 |
183 | 172 |
184 // ---------------------------------------------------------------------------- | 173 // ---------------------------------------------------------------------------- |
185 // Handle operations. | 174 // Handle operations. |
186 // They might invoke garbage collection. The result is an handle to | 175 // They might invoke garbage collection. The result is an handle to |
187 // an object of expected type, or the handle is an error if running out | 176 // an object of expected type, or the handle is an error if running out |
188 // of space or encountering an internal error. | 177 // of space or encountering an internal error. |
189 | 178 |
190 void NormalizeProperties(Handle<JSObject> object, | 179 void NormalizeProperties(Handle<JSObject> object, |
191 PropertyNormalizationMode mode, | 180 PropertyNormalizationMode mode, |
192 int expected_additional_properties); | 181 int expected_additional_properties); |
193 void NormalizeElements(Handle<JSObject> object); | 182 void NormalizeElements(Handle<JSObject> object); |
194 void TransformToFastProperties(Handle<JSObject> object, | 183 void TransformToFastProperties(Handle<JSObject> object, |
195 int unused_property_fields); | 184 int unused_property_fields); |
| 185 void NumberDictionarySet(Handle<NumberDictionary> dictionary, |
| 186 uint32_t index, |
| 187 Handle<Object> value, |
| 188 PropertyDetails details); |
196 | 189 |
197 // Flattens a string. | 190 // Flattens a string. |
198 void FlattenString(Handle<String> str); | 191 void FlattenString(Handle<String> str); |
199 | 192 |
200 // Flattens a string and returns the underlying external or sequential | 193 // Flattens a string and returns the underlying external or sequential |
201 // string. | 194 // string. |
202 Handle<String> FlattenGetString(Handle<String> str); | 195 Handle<String> FlattenGetString(Handle<String> str); |
203 | 196 |
204 Handle<Object> SetProperty(Handle<JSObject> object, | 197 Handle<Object> SetProperty(Handle<JSObject> object, |
205 Handle<String> key, | 198 Handle<String> key, |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 344 |
352 class NoHandleAllocation BASE_EMBEDDED { | 345 class NoHandleAllocation BASE_EMBEDDED { |
353 public: | 346 public: |
354 #ifndef DEBUG | 347 #ifndef DEBUG |
355 NoHandleAllocation() {} | 348 NoHandleAllocation() {} |
356 ~NoHandleAllocation() {} | 349 ~NoHandleAllocation() {} |
357 #else | 350 #else |
358 inline NoHandleAllocation(); | 351 inline NoHandleAllocation(); |
359 inline ~NoHandleAllocation(); | 352 inline ~NoHandleAllocation(); |
360 private: | 353 private: |
361 int extensions_; | 354 int level_; |
362 #endif | 355 #endif |
363 }; | 356 }; |
364 | 357 |
365 | 358 |
366 // ---------------------------------------------------------------------------- | 359 // ---------------------------------------------------------------------------- |
367 | 360 |
368 | 361 |
369 // Stack allocated wrapper call for optimizing adding multiple | 362 // Stack allocated wrapper call for optimizing adding multiple |
370 // properties to an object. | 363 // properties to an object. |
371 class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED { | 364 class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED { |
372 public: | 365 public: |
373 OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object, | 366 OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object, |
374 int expected_property_count, | 367 int expected_property_count, |
375 bool condition = true); | 368 bool condition = true); |
376 ~OptimizedObjectForAddingMultipleProperties(); | 369 ~OptimizedObjectForAddingMultipleProperties(); |
377 private: | 370 private: |
378 bool has_been_transformed_; // Tells whether the object has been transformed. | 371 bool has_been_transformed_; // Tells whether the object has been transformed. |
379 int unused_property_fields_; // Captures the unused number of field. | 372 int unused_property_fields_; // Captures the unused number of field. |
380 Handle<JSObject> object_; // The object being optimized. | 373 Handle<JSObject> object_; // The object being optimized. |
381 }; | 374 }; |
382 | 375 |
383 | 376 |
384 } } // namespace v8::internal | 377 } } // namespace v8::internal |
385 | 378 |
386 #endif // V8_HANDLES_H_ | 379 #endif // V8_HANDLES_H_ |
OLD | NEW |