// Copyright 2021 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 INCLUDE_V8_LOCAL_HANDLE_H_ #define INCLUDE_V8_LOCAL_HANDLE_H_ #include #include #include #include "v8-handle-base.h" // NOLINT(build/include_directory) #include "v8-internal.h" // NOLINT(build/include_directory) namespace v8 { template class LocalBase; template class Local; template class LocalVector; template class MaybeLocal; template class Eternal; template class Global; template class NonCopyablePersistentTraits; template class PersistentBase; template > class Persistent; class TracedReferenceBase; template class BasicTracedReference; template class TracedReference; class Boolean; class Context; class EscapableHandleScope; template class FunctionCallbackInfo; class Isolate; class Object; template class PersistentValueMapBase; class Primitive; class Private; template class PropertyCallbackInfo; template class ReturnValue; class String; template class Traced; class TypecheckWitness; class Utils; namespace debug { class ConsoleCallArguments; } namespace internal { template class CustomArguments; template class LocalUnchecked; class SamplingHeapProfiler; } // namespace internal namespace api_internal { // Called when ToLocalChecked is called on an empty Local. V8_EXPORT void ToLocalEmpty(); } // namespace api_internal /** * 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 V8_EXPORT V8_NODISCARD HandleScope { public: explicit HandleScope(Isolate* isolate); ~HandleScope(); /** * Counts the number of allocated handles. */ static int NumberOfHandles(Isolate* isolate); V8_INLINE Isolate* GetIsolate() const { return reinterpret_cast(i_isolate_); } HandleScope(const HandleScope&) = delete; void operator=(const HandleScope&) = delete; static internal::Address* CreateHandleForCurrentIsolate( internal::Address value); protected: V8_INLINE HandleScope() = default; void Initialize(Isolate* isolate); static internal::Address* CreateHandle(internal::Isolate* i_isolate, internal::Address value); private: // Declaring operator new and delete as deleted is not spec compliant. // Therefore declare them private instead to disable dynamic alloc void* operator new(size_t size); void* operator new[](size_t size); void operator delete(void*, size_t); void operator delete[](void*, size_t); internal::Isolate* i_isolate_; internal::Address* prev_next_; internal::Address* prev_limit_; #ifdef V8_ENABLE_CHECKS int scope_level_ = 0; #endif // LocalBase::New uses CreateHandle with an Isolate* parameter. template friend class LocalBase; // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with // a HeapObject in their shortcuts. friend class Object; friend class Context; }; /** * A base class for local handles. * Its implementation depends on whether direct handle support is enabled. * When it is, a local handle contains a direct pointer to the referenced * object, otherwise it contains an indirect pointer. */ #ifdef V8_ENABLE_DIRECT_HANDLE template class LocalBase : public api_internal::DirectHandleBase { protected: template friend class Local; V8_INLINE LocalBase() = default; V8_INLINE explicit LocalBase(internal::Address ptr) : DirectHandleBase(ptr) {} template V8_INLINE LocalBase(const LocalBase& other) : DirectHandleBase(other) {} V8_INLINE static LocalBase New(Isolate* isolate, internal::Address value) { return LocalBase(value); } V8_INLINE static LocalBase New(Isolate* isolate, T* that) { return LocalBase::New(isolate, internal::ValueHelper::ValueAsAddress(that)); } V8_INLINE static LocalBase FromSlot(internal::Address* slot) { return LocalBase(*slot); } }; #else // !V8_ENABLE_DIRECT_HANDLE template class LocalBase : public api_internal::IndirectHandleBase { protected: template friend class Local; V8_INLINE LocalBase() = default; V8_INLINE explicit LocalBase(internal::Address* location) : IndirectHandleBase(location) {} template V8_INLINE LocalBase(const LocalBase& other) : IndirectHandleBase(other) {} V8_INLINE static LocalBase New(Isolate* isolate, internal::Address value) { return LocalBase(HandleScope::CreateHandle( reinterpret_cast(isolate), value)); } V8_INLINE static LocalBase New(Isolate* isolate, T* that) { if (internal::ValueHelper::IsEmpty(that)) return LocalBase(); return LocalBase::New(isolate, internal::ValueHelper::ValueAsAddress(that)); } V8_INLINE static LocalBase FromSlot(internal::Address* slot) { return LocalBase(slot); } }; #endif // V8_ENABLE_DIRECT_HANDLE /** * An object reference managed by the v8 garbage collector. * * All objects returned from v8 have to be tracked by the garbage collector so * that it knows that the objects are still alive. Also, because the garbage * collector may move objects, it is unsafe to point directly to an object. * Instead, all objects are stored in handles which are known by the garbage * collector and updated whenever an object moves. Handles should always be * passed by value (except in cases like out-parameters) and they should never * be allocated on the heap. * * There are two types of handles: local and persistent handles. * * Local handles are light-weight and transient and typically used in local * operations. They are managed by HandleScopes. That means that a HandleScope * must exist on the stack when they are created and that they are only valid * inside of the HandleScope active during their creation. For passing a local * handle to an outer HandleScope, an EscapableHandleScope and its Escape() * method must be used. * * Persistent handles can be used when storing objects across several * independent operations and have to be explicitly deallocated when they're no * longer used. * * It is safe to extract the object stored in the handle by dereferencing the * handle (for instance, to extract the Object* from a Local); the value * will still be governed by a handle behind the scenes and the same rules apply * to these values as to their handles. */ template class V8_TRIVIAL_ABI Local : public LocalBase, #ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK public api_internal::StackAllocated #else public api_internal::StackAllocated #endif { public: V8_INLINE Local() = default; template V8_INLINE Local(Local that) : LocalBase(that) { /** * This check fails when trying to convert between incompatible * handles. For example, converting from a Local to a * Local. */ static_assert(std::is_base_of::value, "type check"); } V8_INLINE T* operator->() const { return this->template value(); } V8_INLINE T* operator*() const { return this->operator->(); } /** * Checks whether two handles are equal or different. * They are equal iff they are both empty or they are both non-empty and the * objects to which they refer are physically equal. * * If both handles refer to JS objects, this is the same as strict * non-equality. For primitives, such as numbers or strings, a `true` return * value does not indicate that the values aren't equal in the JavaScript * sense. Use `Value::StrictEquals()` to check primitives for equality. */ template V8_INLINE bool operator==(const Local& that) const { return internal::HandleHelper::EqualHandles(*this, that); } template V8_INLINE bool operator==(const PersistentBase& that) const { return internal::HandleHelper::EqualHandles(*this, that); } template V8_INLINE bool operator!=(const Local& that) const { return !operator==(that); } template V8_INLINE bool operator!=(const Persistent& that) const { return !operator==(that); } /** * Cast a handle to a subclass, e.g. Local to Local. * This is only valid if the handle actually refers to a value of the * target type. */ template V8_INLINE static Local Cast(Local that) { #ifdef V8_ENABLE_CHECKS // If we're going to perform the type check then we have to check // that the handle isn't empty before doing the checked cast. if (that.IsEmpty()) return Local(); T::Cast(that.template value()); #endif return Local(LocalBase(that)); } /** * Calling this is equivalent to Local::Cast(). * In particular, this is only valid if the handle actually refers to a value * of the target type. */ template V8_INLINE Local As() const { return Local::Cast(*this); } /** * Create a local handle for the content of another handle. * The referee is kept alive by the local handle even when * the original handle is destroyed/disposed. */ V8_INLINE static Local New(Isolate* isolate, Local that) { return New(isolate, that.template value()); } V8_INLINE static Local New(Isolate* isolate, const PersistentBase& that) { return New(isolate, that.template value()); } V8_INLINE static Local New(Isolate* isolate, const BasicTracedReference& that) { return New(isolate, that.template value()); } private: friend class TracedReferenceBase; friend class Utils; template friend class Eternal; template friend class Global; template friend class Local; template friend class MaybeLocal; template friend class Persistent; template friend class FunctionCallbackInfo; template friend class PropertyCallbackInfo; friend class String; friend class Object; friend class Context; friend class Isolate; friend class Private; template friend class internal::CustomArguments; friend Local Undefined(Isolate* isolate); friend Local Null(Isolate* isolate); friend Local True(Isolate* isolate); friend Local False(Isolate* isolate); friend class HandleScope; friend class EscapableHandleScope; friend class InternalEscapableScope; template friend class PersistentValueMapBase; template friend class ReturnValue; template friend class Traced; friend class internal::SamplingHeapProfiler; friend class internal::HandleHelper; friend class debug::ConsoleCallArguments; friend class internal::LocalUnchecked; explicit Local(no_checking_tag do_not_check) : LocalBase(), StackAllocated(do_not_check) {} explicit Local(const Local& other, no_checking_tag do_not_check) : LocalBase(other), StackAllocated(do_not_check) {} V8_INLINE explicit Local(const LocalBase& other) : LocalBase(other) {} V8_INLINE static Local FromSlot(internal::Address* slot) { return Local(LocalBase::FromSlot(slot)); } #ifdef V8_ENABLE_DIRECT_HANDLE friend class TypecheckWitness; V8_INLINE static Local FromAddress(internal::Address ptr) { return Local(LocalBase(ptr)); } #endif // V8_ENABLE_DIRECT_HANDLE V8_INLINE static Local New(Isolate* isolate, internal::Address value) { return Local(LocalBase::New(isolate, value)); } V8_INLINE static Local New(Isolate* isolate, T* that) { return Local(LocalBase::New(isolate, that)); } // Unsafe cast, should be avoided. template V8_INLINE Local UnsafeAs() const { return Local(LocalBase(*this)); } }; namespace internal { // A local variant that is suitable for off-stack allocation. // Used internally by LocalVector. Not to be used directly! template class V8_TRIVIAL_ABI LocalUnchecked : public Local { public: LocalUnchecked() : Local(Local::do_not_check) {} #if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI // In this case, the check is also enforced in the copy constructor and we // need to suppress it. LocalUnchecked(const LocalUnchecked& other) : Local(other, Local::do_not_check) noexcept {} LocalUnchecked& operator=(const LocalUnchecked&) noexcept = default; #endif // Implicit conversion from Local. LocalUnchecked(const Local& other) noexcept // NOLINT(runtime/explicit) : Local(other, Local::do_not_check) {} }; #ifdef V8_ENABLE_DIRECT_HANDLE // Off-stack allocated direct locals must be registered as strong roots. // For off-stack indirect locals, this is not necessary. template class StrongRootAllocator> : public StrongRootAllocatorBase { public: using value_type = LocalUnchecked; static_assert(std::is_standard_layout_v); static_assert(sizeof(value_type) == sizeof(Address)); explicit StrongRootAllocator(Heap* heap) : StrongRootAllocatorBase(heap) {} explicit StrongRootAllocator(Isolate* isolate) : StrongRootAllocatorBase(isolate) {} explicit StrongRootAllocator(v8::Isolate* isolate) : StrongRootAllocatorBase(reinterpret_cast(isolate)) {} template StrongRootAllocator(const StrongRootAllocator& other) noexcept : StrongRootAllocatorBase(other) {} value_type* allocate(size_t n) { return reinterpret_cast(allocate_impl(n)); } void deallocate(value_type* p, size_t n) noexcept { return deallocate_impl(reinterpret_cast(p), n); } }; #endif // V8_ENABLE_DIRECT_HANDLE } // namespace internal template class LocalVector { private: using element_type = internal::LocalUnchecked; #ifdef V8_ENABLE_DIRECT_HANDLE using allocator_type = internal::StrongRootAllocator; static allocator_type make_allocator(Isolate* isolate) noexcept { return allocator_type(isolate); } #else using allocator_type = std::allocator; static allocator_type make_allocator(Isolate* isolate) noexcept { return allocator_type(); } #endif // V8_ENABLE_DIRECT_HANDLE using vector_type = std::vector; public: using value_type = Local; using reference = value_type&; using const_reference = const value_type&; using size_type = size_t; using difference_type = ptrdiff_t; using iterator = internal::WrappedIterator>; using const_iterator = internal::WrappedIterator>; explicit LocalVector(Isolate* isolate) : backing_(make_allocator(isolate)) {} LocalVector(Isolate* isolate, size_t n) : backing_(n, make_allocator(isolate)) {} explicit LocalVector(Isolate* isolate, std::initializer_list> init) : backing_(make_allocator(isolate)) { if (init.size() == 0) return; backing_.reserve(init.size()); backing_.insert(backing_.end(), init.begin(), init.end()); } iterator begin() noexcept { return iterator(backing_.begin()); } const_iterator begin() const noexcept { return const_iterator(backing_.begin()); } iterator end() noexcept { return iterator(backing_.end()); } const_iterator end() const noexcept { return const_iterator(backing_.end()); } size_t size() const noexcept { return backing_.size(); } bool empty() const noexcept { return backing_.empty(); } void reserve(size_t n) { backing_.reserve(n); } void shrink_to_fit() { backing_.shrink_to_fit(); } Local& operator[](size_t n) { return backing_[n]; } const Local& operator[](size_t n) const { return backing_[n]; } Local& at(size_t n) { return backing_.at(n); } const Local& at(size_t n) const { return backing_.at(n); } Local& front() { return backing_.front(); } const Local& front() const { return backing_.front(); } Local& back() { return backing_.back(); } const Local& back() const { return backing_.back(); } Local* data() noexcept { return backing_.data(); } const Local* data() const noexcept { return backing_.data(); } iterator insert(const_iterator pos, const Local& value) { return iterator(backing_.insert(pos.base(), value)); } template iterator insert(const_iterator pos, InputIt first, InputIt last) { return iterator(backing_.insert(pos.base(), first, last)); } iterator insert(const_iterator pos, std::initializer_list> init) { return iterator(backing_.insert(pos.base(), init.begin(), init.end())); } LocalVector& operator=(std::initializer_list> init) { backing_.clear(); backing_.reserve(init.size()); backing_.insert(backing_.end(), init.begin(), init.end()); return *this; } void push_back(const Local& x) { backing_.push_back(x); } void pop_back() { backing_.pop_back(); } void emplace_back(const Local& x) { backing_.emplace_back(x); } void clear() noexcept { backing_.clear(); } void resize(size_t n) { backing_.resize(n); } void swap(LocalVector& other) { backing_.swap(other.backing_); } friend bool operator==(const LocalVector& x, const LocalVector& y) { return x.backing_ == y.backing_; } friend bool operator!=(const LocalVector& x, const LocalVector& y) { return x.backing_ != y.backing_; } friend bool operator<(const LocalVector& x, const LocalVector& y) { return x.backing_ < y.backing_; } friend bool operator>(const LocalVector& x, const LocalVector& y) { return x.backing_ > y.backing_; } friend bool operator<=(const LocalVector& x, const LocalVector& y) { return x.backing_ <= y.backing_; } friend bool operator>=(const LocalVector& x, const LocalVector& y) { return x.backing_ >= y.backing_; } private: vector_type backing_; }; #if !defined(V8_IMMINENT_DEPRECATION_WARNINGS) // Handle is an alias for Local for historical reasons. template using Handle = Local; #endif /** * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether * the Local<> is empty before it can be used. * * If an API method returns a MaybeLocal<>, the API method can potentially fail * either because an exception is thrown, or because an exception is pending, * e.g. because a previous API call threw an exception that hasn't been caught * yet, or because a TerminateExecution exception was thrown. In that case, an * empty MaybeLocal is returned. */ template class MaybeLocal { public: V8_INLINE MaybeLocal() : local_() {} template V8_INLINE MaybeLocal(Local that) : local_(that) {} V8_INLINE bool IsEmpty() const { return local_.IsEmpty(); } /** * Converts this MaybeLocal<> to a Local<>. If this MaybeLocal<> is empty, * |false| is returned and |out| is assigned with nullptr. */ template V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local* out) const { *out = local_; return !IsEmpty(); } /** * Converts this MaybeLocal<> to a Local<>. If this MaybeLocal<> is empty, * V8 will crash the process. */ V8_INLINE Local ToLocalChecked() { if (V8_UNLIKELY(IsEmpty())) api_internal::ToLocalEmpty(); return local_; } /** * Converts this MaybeLocal<> to a Local<>, using a default value if this * MaybeLocal<> is empty. */ template V8_INLINE Local FromMaybe(Local default_value) const { return IsEmpty() ? default_value : Local(local_); } /** * Cast a handle to a subclass, e.g. MaybeLocal to MaybeLocal. * This is only valid if the handle actually refers to a value of the target * type. */ template V8_INLINE static MaybeLocal Cast(MaybeLocal that) { #ifdef V8_ENABLE_CHECKS // If we're going to perform the type check then we have to check // that the handle isn't empty before doing the checked cast. if (that.IsEmpty()) return MaybeLocal(); T::Cast(that.local_.template value()); #endif return MaybeLocal(that.local_); } /** * Calling this is equivalent to MaybeLocal::Cast(). * In particular, this is only valid if the handle actually refers to a value * of the target type. */ template V8_INLINE MaybeLocal As() const { return MaybeLocal::Cast(*this); } private: Local local_; template friend class MaybeLocal; }; /** * A HandleScope which first allocates a handle in the current scope * which will be later filled with the escape value. */ class V8_EXPORT V8_NODISCARD EscapableHandleScopeBase : public HandleScope { public: explicit EscapableHandleScopeBase(Isolate* isolate); V8_INLINE ~EscapableHandleScopeBase() = default; EscapableHandleScopeBase(const EscapableHandleScopeBase&) = delete; void operator=(const EscapableHandleScopeBase&) = delete; void* operator new(size_t size) = delete; void* operator new[](size_t size) = delete; void operator delete(void*, size_t) = delete; void operator delete[](void*, size_t) = delete; protected: /** * Pushes the value into the previous scope and returns a handle to it. * Cannot be called twice. */ internal::Address* EscapeSlot(internal::Address* escape_value); private: internal::Address* escape_slot_; }; class V8_EXPORT V8_NODISCARD EscapableHandleScope : public EscapableHandleScopeBase { public: explicit EscapableHandleScope(Isolate* isolate) : EscapableHandleScopeBase(isolate) {} V8_INLINE ~EscapableHandleScope() = default; template V8_INLINE Local Escape(Local value) { #ifdef V8_ENABLE_DIRECT_HANDLE return value; #else if (value.IsEmpty()) return value; return Local::FromSlot(EscapeSlot(value.slot())); #endif } template V8_INLINE MaybeLocal EscapeMaybe(MaybeLocal value) { return Escape(value.FromMaybe(Local())); } }; /** * A SealHandleScope acts like a handle scope in which no handle allocations * are allowed. It can be useful for debugging handle leaks. * Handles can be allocated within inner normal HandleScopes. */ class V8_EXPORT V8_NODISCARD SealHandleScope { public: explicit SealHandleScope(Isolate* isolate); ~SealHandleScope(); SealHandleScope(const SealHandleScope&) = delete; void operator=(const SealHandleScope&) = delete; void* operator new(size_t size) = delete; void* operator new[](size_t size) = delete; void operator delete(void*, size_t) = delete; void operator delete[](void*, size_t) = delete; private: internal::Isolate* const i_isolate_; internal::Address* prev_limit_; int prev_sealed_level_; }; } // namespace v8 #endif // INCLUDE_V8_LOCAL_HANDLE_H_