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" | |
14 | 13 |
15 namespace v8 { | 14 namespace v8 { |
16 namespace internal { | 15 namespace internal { |
17 | 16 |
18 // Forward declarations. | 17 // Forward declarations. |
19 class DeferredHandles; | 18 class DeferredHandles; |
20 class HandleScopeImplementer; | 19 class HandleScopeImplementer; |
21 class Isolate; | 20 class Isolate; |
22 class Object; | 21 class Object; |
23 | 22 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 V8_INLINE explicit Handle(T** location = nullptr) | 85 V8_INLINE explicit Handle(T** location = nullptr) |
87 : HandleBase(reinterpret_cast<Object**>(location)) { | 86 : HandleBase(reinterpret_cast<Object**>(location)) { |
88 Object* a = nullptr; | 87 Object* a = nullptr; |
89 T* b = nullptr; | 88 T* b = nullptr; |
90 a = b; // Fake assignment to enforce type checks. | 89 a = b; // Fake assignment to enforce type checks. |
91 USE(a); | 90 USE(a); |
92 } | 91 } |
93 V8_INLINE explicit Handle(T* object) : Handle(object, object->GetIsolate()) {} | 92 V8_INLINE explicit Handle(T* object) : Handle(object, object->GetIsolate()) {} |
94 V8_INLINE Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {} | 93 V8_INLINE Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {} |
95 | 94 |
96 // Allocate a new handle for the object, do not canonicalize. | |
97 V8_INLINE static Handle<T> New(T* object, Isolate* isolate); | |
98 | |
99 // Constructor for handling automatic up casting. | 95 // Constructor for handling automatic up casting. |
100 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. | 96 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
101 template <typename S> | 97 template <typename S> |
102 V8_INLINE Handle(Handle<S> handle) | 98 V8_INLINE Handle(Handle<S> handle) |
103 : HandleBase(handle) { | 99 : HandleBase(handle) { |
104 T* a = nullptr; | 100 T* a = nullptr; |
105 S* b = nullptr; | 101 S* b = nullptr; |
106 a = b; // Fake assignment to enforce type checks. | 102 a = b; // Fake assignment to enforce type checks. |
107 USE(a); | 103 USE(a); |
108 } | 104 } |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 // for which the handle scope has been deleted is undefined. | 247 // for which the handle scope has been deleted is undefined. |
252 class HandleScope { | 248 class HandleScope { |
253 public: | 249 public: |
254 explicit inline HandleScope(Isolate* isolate); | 250 explicit inline HandleScope(Isolate* isolate); |
255 | 251 |
256 inline ~HandleScope(); | 252 inline ~HandleScope(); |
257 | 253 |
258 // Counts the number of allocated handles. | 254 // Counts the number of allocated handles. |
259 static int NumberOfHandles(Isolate* isolate); | 255 static int NumberOfHandles(Isolate* isolate); |
260 | 256 |
261 // Create a new handle or lookup a canonical handle. | |
262 static inline Object** GetHandle(Isolate* isolate, Object* value); | |
263 | |
264 // Creates a new handle with the given value. | 257 // Creates a new handle with the given value. |
265 static inline Object** CreateHandle(Isolate* isolate, Object* value); | 258 template <typename T> |
| 259 static inline T** CreateHandle(Isolate* isolate, T* value); |
266 | 260 |
267 // Deallocates any extensions used by the current scope. | 261 // Deallocates any extensions used by the current scope. |
268 static void DeleteExtensions(Isolate* isolate); | 262 static void DeleteExtensions(Isolate* isolate); |
269 | 263 |
270 static Address current_next_address(Isolate* isolate); | 264 static Address current_next_address(Isolate* isolate); |
271 static Address current_limit_address(Isolate* isolate); | 265 static Address current_limit_address(Isolate* isolate); |
272 static Address current_level_address(Isolate* isolate); | 266 static Address current_level_address(Isolate* isolate); |
273 | 267 |
274 // Closes the HandleScope (invalidating all handles | 268 // Closes the HandleScope (invalidating all handles |
275 // created in the scope of the HandleScope) and returns | 269 // created in the scope of the HandleScope) and returns |
(...skipping 28 matching lines...) Expand all Loading... |
304 // Extend the handle scope making room for more handles. | 298 // Extend the handle scope making room for more handles. |
305 static Object** Extend(Isolate* isolate); | 299 static Object** Extend(Isolate* isolate); |
306 | 300 |
307 #ifdef ENABLE_HANDLE_ZAPPING | 301 #ifdef ENABLE_HANDLE_ZAPPING |
308 // Zaps the handles in the half-open interval [start, end). | 302 // Zaps the handles in the half-open interval [start, end). |
309 static void ZapRange(Object** start, Object** end); | 303 static void ZapRange(Object** start, Object** end); |
310 #endif | 304 #endif |
311 | 305 |
312 friend class v8::HandleScope; | 306 friend class v8::HandleScope; |
313 friend class DeferredHandles; | 307 friend class DeferredHandles; |
314 friend class DeferredHandleScope; | |
315 friend class HandleScopeImplementer; | 308 friend class HandleScopeImplementer; |
316 friend class Isolate; | 309 friend class Isolate; |
317 }; | 310 }; |
318 | 311 |
319 | 312 |
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 | |
352 class DeferredHandleScope final { | 313 class DeferredHandleScope final { |
353 public: | 314 public: |
354 explicit DeferredHandleScope(Isolate* isolate); | 315 explicit DeferredHandleScope(Isolate* isolate); |
355 // The DeferredHandles object returned stores the Handles created | 316 // The DeferredHandles object returned stores the Handles created |
356 // since the creation of this DeferredHandleScope. The Handles are | 317 // since the creation of this DeferredHandleScope. The Handles are |
357 // alive as long as the DeferredHandles object is alive. | 318 // alive as long as the DeferredHandles object is alive. |
358 DeferredHandles* Detach(); | 319 DeferredHandles* Detach(); |
359 ~DeferredHandleScope(); | 320 ~DeferredHandleScope(); |
360 | 321 |
361 private: | 322 private: |
(...skipping 15 matching lines...) Expand all Loading... |
377 class SealHandleScope final { | 338 class SealHandleScope final { |
378 public: | 339 public: |
379 #ifndef DEBUG | 340 #ifndef DEBUG |
380 explicit SealHandleScope(Isolate* isolate) {} | 341 explicit SealHandleScope(Isolate* isolate) {} |
381 ~SealHandleScope() {} | 342 ~SealHandleScope() {} |
382 #else | 343 #else |
383 explicit inline SealHandleScope(Isolate* isolate); | 344 explicit inline SealHandleScope(Isolate* isolate); |
384 inline ~SealHandleScope(); | 345 inline ~SealHandleScope(); |
385 private: | 346 private: |
386 Isolate* isolate_; | 347 Isolate* isolate_; |
387 Object** prev_limit_; | 348 Object** limit_; |
388 int prev_sealed_level_; | 349 int level_; |
389 #endif | 350 #endif |
390 }; | 351 }; |
391 | 352 |
392 | 353 |
393 struct HandleScopeData final { | 354 struct HandleScopeData final { |
394 Object** next; | 355 Object** next; |
395 Object** limit; | 356 Object** limit; |
396 int level; | 357 int level; |
397 int sealed_level; | |
398 CanonicalHandleScope* canonical_scope; | |
399 | 358 |
400 void Initialize() { | 359 void Initialize() { |
401 next = limit = NULL; | 360 next = limit = NULL; |
402 sealed_level = level = 0; | 361 level = 0; |
403 canonical_scope = NULL; | |
404 } | 362 } |
405 }; | 363 }; |
406 | 364 |
407 } // namespace internal | 365 } // namespace internal |
408 } // namespace v8 | 366 } // namespace v8 |
409 | 367 |
410 #endif // V8_HANDLES_H_ | 368 #endif // V8_HANDLES_H_ |
OLD | NEW |