5#ifndef INCLUDE_V8_LOCAL_HANDLE_H_
6#define INCLUDE_V8_LOCAL_HANDLE_H_
33class NonCopyablePersistentTraits;
36template <
class T,
class M = NonCopyablePersistentTraits<T>>
39class TracedReferenceBase;
41class BasicTracedReference;
47class EscapableHandleScope;
49class FunctionCallbackInfo;
52template <
class F1,
class F2,
class F3>
53class PersistentValueMapBase;
54template <
class F1,
class F2>
69class ConsoleCallArguments;
77class SamplingHeapProfiler;
80namespace api_internal {
111 return reinterpret_cast<Isolate*
>(i_isolate_);
131 void*
operator new(
size_t size);
132 void*
operator new[](
size_t size);
133 void operator delete(
void*, size_t);
134 void operator delete[](
void*, size_t);
136 internal::Isolate* i_isolate_;
139#ifdef V8_ENABLE_CHECKS
140 int scope_level_ = 0;
144 template <
typename T>
159#ifdef V8_ENABLE_DIRECT_LOCAL
162class LocalBase :
public api_internal::DirectHandleBase {
171 template <
typename S>
175 return LocalBase<T>(
value);
178 V8_INLINE static LocalBase<T>
New(Isolate* isolate, T* that) {
184 return LocalBase<T>(*
slot);
201 template <
typename S>
206 reinterpret_cast<internal::Isolate*
>(isolate),
value));
253#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
269 static_assert(std::is_base_of<T, S>::value,
"type check");
289 return internal::HandleHelper::EqualHandles(*
this, that);
294 return internal::HandleHelper::EqualHandles(*
this, that);
299 return !operator==(that);
304 return !operator==(that);
314#ifdef V8_ENABLE_CHECKS
318 T::Cast(that.template value<S>());
339 return New(isolate, that.template value<T, true>());
344 return New(isolate, that.template value<T, true>());
349 return New(isolate, that.template value<T, true>());
363 template <
class F,
class M>
383 template <
class F1,
class F2,
class F3>
385 template <
class F1,
class F2>
391 friend class internal::SamplingHeapProfiler;
393 friend class debug::ConsoleCallArguments;
396 explicit Local(no_checking_tag do_not_check)
397 :
LocalBase<T>(), StackAllocated(do_not_check) {}
398 explicit Local(
const Local<T>& other, no_checking_tag do_not_check)
399 :
LocalBase<T>(other), StackAllocated(do_not_check) {}
401 V8_INLINE explicit Local<T>(
const LocalBase<T>& other)
402 : LocalBase<T>(other) {}
404 V8_INLINE static Local<T> FromSlot(internal::Address* slot) {
405 return Local<T>(LocalBase<T>::FromSlot(slot));
408#ifdef V8_ENABLE_DIRECT_LOCAL
409 friend class TypecheckWitness;
411 V8_INLINE static Local<T> FromAddress(internal::Address ptr) {
412 return Local<T>(LocalBase<T>(ptr));
416 V8_INLINE static Local<T> New(Isolate* isolate, internal::Address value) {
417 return Local<T>(LocalBase<T>::New(isolate, value));
420 V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
421 return Local<T>(LocalBase<T>::New(isolate, that));
427 return Local<S>(LocalBase<S>(*
this));
439#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
444 LocalUnchecked& operator=(
const LocalUnchecked&) =
default;
452#ifdef V8_ENABLE_DIRECT_LOCAL
457class StrongRootAllocator<LocalUnchecked<T>> :
public StrongRootAllocatorBase {
459 using value_type = LocalUnchecked<T>;
460 static_assert(std::is_standard_layout_v<value_type>);
461 static_assert(
sizeof(value_type) ==
sizeof(Address));
463 explicit StrongRootAllocator(Heap* heap) : StrongRootAllocatorBase(heap) {}
465 : StrongRootAllocatorBase(isolate) {}
466 template <
typename U>
467 StrongRootAllocator(
const StrongRootAllocator<U>& other) noexcept
468 : StrongRootAllocatorBase(other) {}
470 value_type* allocate(
size_t n) {
471 return reinterpret_cast<value_type*
>(allocate_impl(n));
473 void deallocate(value_type* p,
size_t n)
noexcept {
474 return deallocate_impl(
reinterpret_cast<Address*
>(p), n);
485#ifdef V8_ENABLE_DIRECT_LOCAL
488 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
489 return allocator_type(isolate);
492 using allocator_type = std::allocator<element_type>;
494 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
495 return allocator_type();
499 using vector_type = std::vector<element_type, allocator_type>;
515 : backing_(n, make_allocator(isolate)) {}
517 : backing_(make_allocator(isolate)) {
518 if (init.size() == 0)
return;
519 backing_.reserve(init.size());
520 backing_.insert(backing_.end(), init.begin(), init.end());
530 size_t size() const noexcept {
return backing_.size(); }
531 bool empty() const noexcept {
return backing_.empty(); }
532 void reserve(
size_t n) { backing_.reserve(n); }
539 const Local<T>&
at(
size_t n)
const {
return backing_.at(n); }
553 template <
typename InputIt>
555 return iterator(backing_.insert(pos.
base(), first, last));
559 return iterator(backing_.insert(pos.
base(), init.begin(), init.end()));
564 backing_.insert(backing_.end(), init.begin(), init.end());
572 void clear() noexcept { backing_.clear(); }
573 void resize(
size_t n) { backing_.resize(n); }
577 return x.backing_ == y.backing_;
580 return x.backing_ != y.backing_;
583 return x.backing_ < y.backing_;
586 return x.backing_ > y.backing_;
589 return x.backing_ <= y.backing_;
592 return x.backing_ >= y.backing_;
596 vector_type backing_;
599#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
659#ifdef V8_ENABLE_CHECKS
663 T::Cast(that.local_.template value<S>());
681 template <
typename S>
696 void*
operator new(
size_t size) =
delete;
697 void*
operator new[](
size_t size) =
delete;
698 void operator delete(
void*, size_t) =
delete;
699 void operator delete[](
void*, size_t) =
delete;
720#ifdef V8_ENABLE_DIRECT_LOCAL
723 if (value.
IsEmpty())
return value;
746 void*
operator new(
size_t size) =
delete;
747 void*
operator new[](
size_t size) =
delete;
748 void operator delete(
void*, size_t) =
delete;
749 void operator delete[](
void*, size_t) =
delete;
752 internal::Isolate*
const i_isolate_;
754 int prev_sealed_level_;
EscapableHandleScopeBase(const EscapableHandleScopeBase &)=delete
internal::Address * EscapeSlot(internal::Address *escape_value)
EscapableHandleScopeBase(Isolate *isolate)
void operator=(const EscapableHandleScopeBase &)=delete
V8_INLINE ~EscapableHandleScopeBase()=default
V8_INLINE Local< T > Escape(Local< T > value)
EscapableHandleScope(Isolate *isolate)
V8_INLINE MaybeLocal< T > EscapeMaybe(MaybeLocal< T > value)
V8_INLINE ~EscapableHandleScope()=default
void Initialize(Isolate *isolate)
V8_INLINE HandleScope()=default
static int NumberOfHandles(Isolate *isolate)
V8_INLINE Isolate * GetIsolate() const
HandleScope(const HandleScope &)=delete
static internal::Address * CreateHandleForCurrentIsolate(internal::Address value)
HandleScope(Isolate *isolate)
void operator=(const HandleScope &)=delete
static internal::Address * CreateHandle(internal::Isolate *i_isolate, internal::Address value)
V8_INLINE LocalBase(internal::Address *location)
V8_INLINE LocalBase(const LocalBase< S > &other)
V8_INLINE LocalBase()=default
static V8_INLINE LocalBase< T > New(Isolate *isolate, T *that)
static V8_INLINE LocalBase< T > New(Isolate *isolate, internal::Address value)
static V8_INLINE LocalBase< T > FromSlot(internal::Address *slot)
friend class TracedReferenceBase
friend class PersistentValueMapBase
V8_INLINE T * operator->() const
V8_INLINE Local(Local< S > that)
V8_INLINE bool operator!=(const Local< S > &that) const
static V8_INLINE Local< T > New(Isolate *isolate, const PersistentBase< T > &that)
static V8_INLINE Local< T > Cast(Local< S > that)
V8_INLINE bool operator==(const Local< S > &that) const
friend class PropertyCallbackInfo
static V8_INLINE Local< T > New(Isolate *isolate, const BasicTracedReference< T > &that)
friend class FunctionCallbackInfo
V8_INLINE Local< S > As() const
V8_INLINE bool operator==(const PersistentBase< S > &that) const
friend class PersistentValueVector
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
V8_INLINE T * operator*() const
V8_INLINE bool operator!=(const Persistent< S > &that) const
V8_INLINE Local()=default
friend class EscapableHandleScope
friend class InternalEscapableScope
friend bool operator>=(const LocalVector< T > &x, const LocalVector< T > &y)
const_iterator begin() const noexcept
internal::WrappedIterator< typename vector_type::const_iterator, const Local< T > > const_iterator
friend bool operator<(const LocalVector< T > &x, const LocalVector< T > &y)
size_t size() const noexcept
friend bool operator<=(const LocalVector< T > &x, const LocalVector< T > &y)
bool empty() const noexcept
LocalVector(Isolate *isolate, std::initializer_list< Local< T > > init)
const Local< T > * data() const noexcept
friend bool operator!=(const LocalVector< T > &x, const LocalVector< T > &y)
iterator insert(const_iterator pos, std::initializer_list< Local< T > > init)
internal::WrappedIterator< typename vector_type::iterator, Local< T > > iterator
const_iterator end() const noexcept
LocalVector(Isolate *isolate, size_t n)
const Local< T > & back() const
Local< T > * data() noexcept
void swap(LocalVector< T > &other)
LocalVector(Isolate *isolate)
iterator begin() noexcept
iterator insert(const_iterator pos, const Local< T > &value)
LocalVector< T > & operator=(std::initializer_list< Local< T > > init)
Local< T > & operator[](size_t n)
void emplace_back(const Local< T > &x)
const Local< T > & operator[](size_t n) const
iterator insert(const_iterator pos, InputIt first, InputIt last)
friend bool operator==(const LocalVector< T > &x, const LocalVector< T > &y)
const Local< T > & front() const
void push_back(const Local< T > &x)
const Local< T > & at(size_t n) const
Local< T > & at(size_t n)
ptrdiff_t difference_type
friend bool operator>(const LocalVector< T > &x, const LocalVector< T > &y)
V8_INLINE Local< T > ToLocalChecked()
V8_INLINE bool IsEmpty() const
V8_INLINE MaybeLocal(Local< S > that)
static V8_INLINE MaybeLocal< T > Cast(MaybeLocal< S > that)
V8_INLINE MaybeLocal< S > As() const
V8_INLINE Local< S > FromMaybe(Local< S > default_value) const
V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local< S > *out) const
void operator=(const SealHandleScope &)=delete
SealHandleScope(Isolate *isolate)
SealHandleScope(const SealHandleScope &)=delete
V8_INLINE internal::Address ptr() const
V8_INLINE T * value() const
V8_INLINE bool IsEmpty() const
V8_INLINE IndirectHandleBase()=default
V8_INLINE internal::Address *const & slot() const
LocalUnchecked(const Local< T > &other)
static V8_INLINE Address ValueAsAddress(const T *value)
static V8_INLINE bool IsEmpty(T *value)
constexpr Iterator base() const noexcept
internal::BasicPersistent< T, internal::StrongPersistentPolicy > Persistent
V8_EXPORT void ToLocalEmpty()
V8_INLINE Local< Primitive > Null(Isolate *isolate)
V8_INLINE Local< Boolean > True(Isolate *isolate)
V8_INLINE Local< Boolean > False(Isolate *isolate)
V8_INLINE Local< Primitive > Undefined(Isolate *isolate)
#define V8_WARN_UNUSED_RESULT
#define V8_UNLIKELY(condition)