OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_HANDLES_H_ | 5 #ifndef V8_HANDLES_H_ |
6 #define V8_HANDLES_H_ | 6 #define V8_HANDLES_H_ |
7 | 7 |
8 #include "include/v8.h" | 8 #include "include/v8.h" |
9 #include "src/base/functional.h" | 9 #include "src/base/functional.h" |
10 #include "src/base/macros.h" | 10 #include "src/base/macros.h" |
11 #include "src/checks.h" | 11 #include "src/checks.h" |
12 #include "src/globals.h" | 12 #include "src/globals.h" |
| 13 #include "src/zone.h" |
13 | 14 |
14 namespace v8 { | 15 namespace v8 { |
15 namespace internal { | 16 namespace internal { |
16 | 17 |
17 // Forward declarations. | 18 // Forward declarations. |
18 class DeferredHandles; | 19 class DeferredHandles; |
19 class HandleScopeImplementer; | 20 class HandleScopeImplementer; |
20 class Isolate; | 21 class Isolate; |
21 class Object; | 22 class Object; |
22 | 23 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 V8_INLINE explicit Handle(T** location = nullptr) | 86 V8_INLINE explicit Handle(T** location = nullptr) |
86 : HandleBase(reinterpret_cast<Object**>(location)) { | 87 : HandleBase(reinterpret_cast<Object**>(location)) { |
87 Object* a = nullptr; | 88 Object* a = nullptr; |
88 T* b = nullptr; | 89 T* b = nullptr; |
89 a = b; // Fake assignment to enforce type checks. | 90 a = b; // Fake assignment to enforce type checks. |
90 USE(a); | 91 USE(a); |
91 } | 92 } |
92 V8_INLINE explicit Handle(T* object) : Handle(object, object->GetIsolate()) {} | 93 V8_INLINE explicit Handle(T* object) : Handle(object, object->GetIsolate()) {} |
93 V8_INLINE Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {} | 94 V8_INLINE Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {} |
94 | 95 |
| 96 // Allocate a new handle for the object, do not canonicalize. |
| 97 V8_INLINE static Handle<T> New(T* object, Isolate* isolate); |
| 98 |
95 // Constructor for handling automatic up casting. | 99 // Constructor for handling automatic up casting. |
96 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 100 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
97 template <typename S> | 101 template <typename S> |
98 V8_INLINE Handle(Handle<S> handle) | 102 V8_INLINE Handle(Handle<S> handle) |
99 : HandleBase(handle) { | 103 : HandleBase(handle) { |
100 T* a = nullptr; | 104 T* a = nullptr; |
101 S* b = nullptr; | 105 S* b = nullptr; |
102 a = b; // Fake assignment to enforce type checks. | 106 a = b; // Fake assignment to enforce type checks. |
103 USE(a); | 107 USE(a); |
104 } | 108 } |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 // for which the handle scope has been deleted is undefined. | 251 // for which the handle scope has been deleted is undefined. |
248 class HandleScope { | 252 class HandleScope { |
249 public: | 253 public: |
250 explicit inline HandleScope(Isolate* isolate); | 254 explicit inline HandleScope(Isolate* isolate); |
251 | 255 |
252 inline ~HandleScope(); | 256 inline ~HandleScope(); |
253 | 257 |
254 // Counts the number of allocated handles. | 258 // Counts the number of allocated handles. |
255 static int NumberOfHandles(Isolate* isolate); | 259 static int NumberOfHandles(Isolate* isolate); |
256 | 260 |
| 261 // Create a new handle or lookup a canonical handle. |
| 262 V8_INLINE static Object** GetHandle(Isolate* isolate, Object* value); |
| 263 |
257 // Creates a new handle with the given value. | 264 // Creates a new handle with the given value. |
258 template <typename T> | 265 V8_INLINE static Object** CreateHandle(Isolate* isolate, Object* value); |
259 static inline T** CreateHandle(Isolate* isolate, T* value); | |
260 | 266 |
261 // Deallocates any extensions used by the current scope. | 267 // Deallocates any extensions used by the current scope. |
262 static void DeleteExtensions(Isolate* isolate); | 268 static void DeleteExtensions(Isolate* isolate); |
263 | 269 |
264 static Address current_next_address(Isolate* isolate); | 270 static Address current_next_address(Isolate* isolate); |
265 static Address current_limit_address(Isolate* isolate); | 271 static Address current_limit_address(Isolate* isolate); |
266 static Address current_level_address(Isolate* isolate); | 272 static Address current_level_address(Isolate* isolate); |
267 | 273 |
268 // Closes the HandleScope (invalidating all handles | 274 // Closes the HandleScope (invalidating all handles |
269 // created in the scope of the HandleScope) and returns | 275 // created in the scope of the HandleScope) and returns |
(...skipping 28 matching lines...) Expand all Loading... |
298 // Extend the handle scope making room for more handles. | 304 // Extend the handle scope making room for more handles. |
299 static Object** Extend(Isolate* isolate); | 305 static Object** Extend(Isolate* isolate); |
300 | 306 |
301 #ifdef ENABLE_HANDLE_ZAPPING | 307 #ifdef ENABLE_HANDLE_ZAPPING |
302 // Zaps the handles in the half-open interval [start, end). | 308 // Zaps the handles in the half-open interval [start, end). |
303 static void ZapRange(Object** start, Object** end); | 309 static void ZapRange(Object** start, Object** end); |
304 #endif | 310 #endif |
305 | 311 |
306 friend class v8::HandleScope; | 312 friend class v8::HandleScope; |
307 friend class DeferredHandles; | 313 friend class DeferredHandles; |
| 314 friend class DeferredHandleScope; |
308 friend class HandleScopeImplementer; | 315 friend class HandleScopeImplementer; |
309 friend class Isolate; | 316 friend class Isolate; |
310 }; | 317 }; |
311 | 318 |
312 | 319 |
| 320 // Forward declarations for CanonicalHandleScope. |
| 321 template <typename V> |
| 322 class IdentityMap; |
| 323 class RootIndexMap; |
| 324 |
| 325 |
| 326 // A CanonicalHandleScope does not open a new HandleScope. It changes the |
| 327 // existing HandleScope so that Handles created within are canonicalized. |
| 328 // This does not apply to nested inner HandleScopes unless a nested |
| 329 // CanonicalHandleScope is introduced. Handles are only canonicalized within |
| 330 // the same CanonicalHandleScope, but not across nested ones. |
| 331 class CanonicalHandleScope final { |
| 332 public: |
| 333 explicit CanonicalHandleScope(Isolate* isolate); |
| 334 ~CanonicalHandleScope(); |
| 335 |
| 336 private: |
| 337 Object** Lookup(Object* object); |
| 338 |
| 339 Isolate* isolate_; |
| 340 Zone zone_; |
| 341 RootIndexMap* root_index_map_; |
| 342 IdentityMap<Object**>* identity_map_; |
| 343 // Ordinary nested handle scopes within the current one are not canonical. |
| 344 int canonical_level_; |
| 345 // We may have nested canonical scopes. Handles are canonical within each one. |
| 346 CanonicalHandleScope* prev_canonical_scope_; |
| 347 |
| 348 friend class HandleScope; |
| 349 }; |
| 350 |
| 351 |
313 class DeferredHandleScope final { | 352 class DeferredHandleScope final { |
314 public: | 353 public: |
315 explicit DeferredHandleScope(Isolate* isolate); | 354 explicit DeferredHandleScope(Isolate* isolate); |
316 // The DeferredHandles object returned stores the Handles created | 355 // The DeferredHandles object returned stores the Handles created |
317 // since the creation of this DeferredHandleScope. The Handles are | 356 // since the creation of this DeferredHandleScope. The Handles are |
318 // alive as long as the DeferredHandles object is alive. | 357 // alive as long as the DeferredHandles object is alive. |
319 DeferredHandles* Detach(); | 358 DeferredHandles* Detach(); |
320 ~DeferredHandleScope(); | 359 ~DeferredHandleScope(); |
321 | 360 |
322 private: | 361 private: |
(...skipping 15 matching lines...) Expand all Loading... |
338 class SealHandleScope final { | 377 class SealHandleScope final { |
339 public: | 378 public: |
340 #ifndef DEBUG | 379 #ifndef DEBUG |
341 explicit SealHandleScope(Isolate* isolate) {} | 380 explicit SealHandleScope(Isolate* isolate) {} |
342 ~SealHandleScope() {} | 381 ~SealHandleScope() {} |
343 #else | 382 #else |
344 explicit inline SealHandleScope(Isolate* isolate); | 383 explicit inline SealHandleScope(Isolate* isolate); |
345 inline ~SealHandleScope(); | 384 inline ~SealHandleScope(); |
346 private: | 385 private: |
347 Isolate* isolate_; | 386 Isolate* isolate_; |
348 Object** limit_; | 387 Object** prev_limit_; |
349 int level_; | 388 int prev_sealed_level_; |
350 #endif | 389 #endif |
351 }; | 390 }; |
352 | 391 |
353 | 392 |
354 struct HandleScopeData final { | 393 struct HandleScopeData final { |
355 Object** next; | 394 Object** next; |
356 Object** limit; | 395 Object** limit; |
357 int level; | 396 int level; |
| 397 int sealed_level; |
| 398 CanonicalHandleScope* canonical_scope; |
358 | 399 |
359 void Initialize() { | 400 void Initialize() { |
360 next = limit = NULL; | 401 next = limit = NULL; |
361 level = 0; | 402 sealed_level = level = 0; |
| 403 canonical_scope = NULL; |
362 } | 404 } |
363 }; | 405 }; |
364 | 406 |
365 } // namespace internal | 407 } // namespace internal |
366 } // namespace v8 | 408 } // namespace v8 |
367 | 409 |
368 #endif // V8_HANDLES_H_ | 410 #endif // V8_HANDLES_H_ |
OLD | NEW |