// Copyright 2011 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_HANDLES_H_ #define V8_HANDLES_H_ #include "include/v8.h" #include "src/base/macros.h" #include "src/checks.h" #include "src/globals.h" namespace v8 { namespace internal { // Forward declarations. class DeferredHandles; class HandleScopeImplementer; class Isolate; class Object; // ---------------------------------------------------------------------------- // Base class for Handle instantiations. Don't use directly. class HandleBase { public: V8_INLINE explicit HandleBase(Object** location) : location_(location) {} V8_INLINE explicit HandleBase(Object* object, Isolate* isolate); // Check if this handle refers to the exact same object as the other handle. V8_INLINE bool is_identical_to(const HandleBase that) const { // Dereferencing deferred handles to check object equality is safe. SLOW_DCHECK((this->location_ == nullptr || this->IsDereferenceAllowed(NO_DEFERRED_CHECK)) && (that.location_ == nullptr || that.IsDereferenceAllowed(NO_DEFERRED_CHECK))); if (this->location_ == that.location_) return true; if (this->location_ == NULL || that.location_ == NULL) return false; return *this->location_ == *that.location_; } V8_INLINE bool is_null() const { return location_ == nullptr; } protected: // Provides the C++ dereference operator. V8_INLINE Object* operator*() const { SLOW_DCHECK(IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK)); return *location_; } // Returns the address to where the raw pointer is stored. V8_INLINE Object** location() const { SLOW_DCHECK(location_ == nullptr || IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK)); return location_; } enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK }; #ifdef DEBUG bool IsDereferenceAllowed(DereferenceCheckMode mode) const; #else V8_INLINE bool IsDereferenceAllowed(DereferenceCheckMode mode) const { return true; } #endif // DEBUG Object** location_; }; // ---------------------------------------------------------------------------- // A Handle provides a reference to an object that survives relocation by // the garbage collector. // Handles are only valid within a HandleScope. // When a handle is created for an object a cell is allocated in the heap. template class Handle final : public HandleBase { public: V8_INLINE explicit Handle(T** location = nullptr) : HandleBase(reinterpret_cast(location)) { Object* a = nullptr; T* b = nullptr; a = b; // Fake assignment to enforce type checks. USE(a); } V8_INLINE explicit Handle(T* object) : Handle(object, object->GetIsolate()) {} V8_INLINE Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {} // Constructor for handling automatic up casting. // Ex. Handle can be passed when Handle is expected. template V8_INLINE Handle(Handle handle) : HandleBase(handle) { T* a = nullptr; S* b = nullptr; a = b; // Fake assignment to enforce type checks. USE(a); } V8_INLINE T* operator->() const { return operator*(); } // Provides the C++ dereference operator. V8_INLINE T* operator*() const { return reinterpret_cast(HandleBase::operator*()); } // Returns the address to where the raw pointer is stored. V8_INLINE T** location() const { return reinterpret_cast(HandleBase::location()); } template static const Handle cast(Handle that) { T::cast(*reinterpret_cast(that.location_)); return Handle(reinterpret_cast(that.location_)); } // TODO(yangguo): Values that contain empty handles should be declared as // MaybeHandle to force validation before being used as handles. static const Handle null() { return Handle(); } private: // Handles of different classes are allowed to access each other's location_. template friend class Handle; // MaybeHandle is allowed to access location_. template friend class MaybeHandle; }; template V8_INLINE Handle handle(T* object, Isolate* isolate) { return Handle(object, isolate); } template V8_INLINE Handle handle(T* object) { return Handle(object); } // ---------------------------------------------------------------------------- // A Handle can be converted into a MaybeHandle. Converting a MaybeHandle // into a Handle requires checking that it does not point to NULL. This // ensures NULL checks before use. // Do not use MaybeHandle as argument type. template class MaybeHandle final { public: V8_INLINE MaybeHandle() {} V8_INLINE ~MaybeHandle() {} // Constructor for handling automatic up casting from Handle. // Ex. Handle can be passed when MaybeHandle is expected. template V8_INLINE MaybeHandle(Handle handle) : location_(reinterpret_cast(handle.location_)) { T* a = nullptr; S* b = nullptr; a = b; // Fake assignment to enforce type checks. USE(a); } // Constructor for handling automatic up casting. // Ex. MaybeHandle can be passed when Handle is expected. template V8_INLINE MaybeHandle(MaybeHandle maybe_handle) : location_(reinterpret_cast(maybe_handle.location_)) { T* a = nullptr; S* b = nullptr; a = b; // Fake assignment to enforce type checks. USE(a); } V8_INLINE void Assert() const { DCHECK_NOT_NULL(location_); } V8_INLINE void Check() const { CHECK_NOT_NULL(location_); } V8_INLINE Handle ToHandleChecked() const { Check(); return Handle(location_); } // Convert to a Handle with a type that can be upcasted to. template V8_INLINE bool ToHandle(Handle* out) const { if (location_ == nullptr) { *out = Handle::null(); return false; } else { *out = Handle(location_); return true; } } bool is_null() const { return location_ == nullptr; } template V8_INLINE bool operator==(MaybeHandle that) const { return this->location_ == that.location_; } template V8_INLINE bool operator!=(MaybeHandle that) const { return this->location_ != that.location_; } protected: T** location_ = nullptr; // MaybeHandles of different classes are allowed to access each // other's location_. template friend class MaybeHandle; // Utility functions are allowed to access location_. template friend size_t hash_value(MaybeHandle); }; template V8_INLINE size_t hash_value(MaybeHandle maybe_handle) { uintptr_t v = bit_cast(maybe_handle.location_); DCHECK_EQ(0u, v & ((1u << kPointerSizeLog2) - 1)); return v >> kPointerSizeLog2; } // A stack-allocated class that governs a number of local handles. // After a handle scope has been created, all local handles will be // allocated within that handle scope until either the handle scope is // deleted or another handle scope is created. If there is already a // handle scope and a new one is created, all allocations will take // place in the new handle scope until it is deleted. After that, // new handles will again be allocated in the original handle scope. // // After the handle scope of a local handle has been deleted the // garbage collector will no longer track the object stored in the // handle and may deallocate it. The behavior of accessing a handle // for which the handle scope has been deleted is undefined. class HandleScope { public: explicit inline HandleScope(Isolate* isolate); inline ~HandleScope(); // Counts the number of allocated handles. static int NumberOfHandles(Isolate* isolate); // Creates a new handle with the given value. template static inline T** CreateHandle(Isolate* isolate, T* value); // Deallocates any extensions used by the current scope. static void DeleteExtensions(Isolate* isolate); static Address current_next_address(Isolate* isolate); static Address current_limit_address(Isolate* isolate); static Address current_level_address(Isolate* isolate); // Closes the HandleScope (invalidating all handles // created in the scope of the HandleScope) and returns // a Handle backed by the parent scope holding the // value of the argument handle. template Handle CloseAndEscape(Handle handle_value); Isolate* isolate() { return isolate_; } // Limit for number of handles with --check-handle-count. This is // large enough to compile natives and pass unit tests with some // slack for future changes to natives. static const int kCheckHandleThreshold = 30 * 1024; private: // Prevent heap allocation or illegal handle scopes. HandleScope(const HandleScope&); void operator=(const HandleScope&); void* operator new(size_t size); void operator delete(void* size_t); Isolate* isolate_; Object** prev_next_; Object** prev_limit_; // Close the handle scope resetting limits to a previous state. static inline void CloseScope(Isolate* isolate, Object** prev_next, Object** prev_limit); // Extend the handle scope making room for more handles. static Object** Extend(Isolate* isolate); #ifdef ENABLE_HANDLE_ZAPPING // Zaps the handles in the half-open interval [start, end). static void ZapRange(Object** start, Object** end); #endif friend class v8::HandleScope; friend class DeferredHandles; friend class HandleScopeImplementer; friend class Isolate; }; class DeferredHandleScope final { public: explicit DeferredHandleScope(Isolate* isolate); // The DeferredHandles object returned stores the Handles created // since the creation of this DeferredHandleScope. The Handles are // alive as long as the DeferredHandles object is alive. DeferredHandles* Detach(); ~DeferredHandleScope(); private: Object** prev_limit_; Object** prev_next_; HandleScopeImplementer* impl_; #ifdef DEBUG bool handles_detached_; int prev_level_; #endif friend class HandleScopeImplementer; }; // Seal off the current HandleScope so that new handles can only be created // if a new HandleScope is entered. class SealHandleScope final { public: #ifndef DEBUG explicit SealHandleScope(Isolate* isolate) {} ~SealHandleScope() {} #else explicit inline SealHandleScope(Isolate* isolate); inline ~SealHandleScope(); private: Isolate* isolate_; Object** limit_; int level_; #endif }; struct HandleScopeData final { Object** next; Object** limit; int level; void Initialize() { next = limit = NULL; level = 0; } }; } // namespace internal } // namespace v8 #endif // V8_HANDLES_H_