// 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 <stddef.h> #include <type_traits> #include <vector> #include "v8-handle-base.h" // NOLINT(build/include_directory) #include "v8-internal.h" // NOLINT(build/include_directory) namespace v8 { template <class T> class LocalBase; template <class T> class Local; template <class T> class LocalVector; template <class F> class MaybeLocal; template <class T> class Eternal; template <class T> class Global; template <class T> class NonCopyablePersistentTraits; template <class T> class PersistentBase; template <class T, class M = NonCopyablePersistentTraits<T>> class Persistent; class TracedReferenceBase; template <class T> class BasicTracedReference; template <class F> class TracedReference; class Boolean; class Context; class EscapableHandleScope; template <class F> class FunctionCallbackInfo; class Isolate; class Object; template <class F1, class F2, class F3> class PersistentValueMapBase; class Primitive; class Private; template <class F> class PropertyCallbackInfo; template <class F> class ReturnValue; class String; template <class F> class Traced; class TypecheckWitness; class Utils; namespace debug { class ConsoleCallArguments; } namespace internal { template <typename T> class CustomArguments; template <typename T> 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 { … }; /** * 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 <typename T> class LocalBase : public api_internal::DirectHandleBase { protected: template <class F> friend class Local; V8_INLINE LocalBase() = default; V8_INLINE explicit LocalBase(internal::Address ptr) : DirectHandleBase(ptr) {} template <typename S> V8_INLINE LocalBase(const LocalBase<S>& other) : DirectHandleBase(other) {} V8_INLINE static LocalBase<T> New(Isolate* isolate, internal::Address value) { return LocalBase<T>(value); } V8_INLINE static LocalBase<T> New(Isolate* isolate, T* that) { return LocalBase<T>::New(isolate, internal::ValueHelper::ValueAsAddress(that)); } V8_INLINE static LocalBase<T> FromSlot(internal::Address* slot) { return LocalBase<T>(*slot); } }; #else // !V8_ENABLE_DIRECT_HANDLE template <typename T> class LocalBase : public api_internal::IndirectHandleBase { … }; #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<Object>); 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 T> class V8_TRIVIAL_ABI Local : public LocalBase<T>, #ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK public api_internal::StackAllocated<true> #else public api_internal::StackAllocated<false> #endif { … }; namespace internal { // A local variant that is suitable for off-stack allocation. // Used internally by LocalVector<T>. Not to be used directly! template <typename T> class V8_TRIVIAL_ABI LocalUnchecked : public Local<T> { … }; #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 <typename T> class StrongRootAllocator<LocalUnchecked<T>> : public StrongRootAllocatorBase { public: using value_type = LocalUnchecked<T>; static_assert(std::is_standard_layout_v<value_type>); 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*>(isolate)) {} template <typename U> StrongRootAllocator(const StrongRootAllocator<U>& other) noexcept : StrongRootAllocatorBase(other) {} value_type* allocate(size_t n) { return reinterpret_cast<value_type*>(allocate_impl(n)); } void deallocate(value_type* p, size_t n) noexcept { return deallocate_impl(reinterpret_cast<Address*>(p), n); } }; #endif // V8_ENABLE_DIRECT_HANDLE } // namespace internal template <typename T> class LocalVector { … }; #if !defined(V8_IMMINENT_DEPRECATION_WARNINGS) // Handle is an alias for Local for historical reasons. Handle; #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 T> 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 { … }; class V8_EXPORT V8_NODISCARD EscapableHandleScope : public EscapableHandleScopeBase { … }; /** * 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 { … }; } // namespace v8 #endif // INCLUDE_V8_LOCAL_HANDLE_H_