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;
48class EscapableHandleScope;
50class FunctionCallbackInfo;
53template <
class F1,
class F2,
class F3>
54class PersistentValueMapBase;
58class PropertyCallbackInfo;
70class ConsoleCallArguments;
78class SamplingHeapProfiler;
81namespace api_internal {
85#ifdef V8_ENABLE_CHECKS
86template <
typename T,
typename V = Value>
87void TypeCheckLocal(
V* value) {
89 if constexpr (
requires { T::Cast(value); }) {
91 if (std::is_same_v<Array, T> && value->IsArgumentsObject())
return;
92 if (std::is_same_v<ArrayBuffer, T> && value->IsSharedArrayBuffer())
return;
93 if (std::is_same_v<Object, T> && value->IsNull())
return;
94 if (std::is_same_v<Object, T> && value->IsString())
return;
95 if (std::is_same_v<Object, T> && value->IsUndefined())
return;
96 if (std::is_same_v<Uint32, T> && value->IsInt32())
return;
97 if (std::is_same_v<Object, T> && value->IsNumber())
return;
151 void DeleteExtensions(
Isolate* isolate);
153#ifdef V8_ENABLE_CHECKS
155 void DoInitializeAsserts(
Isolate* isolate);
157 void AssertScopeLevelsMatch();
166 void*
operator new(
size_t size);
167 void*
operator new[](
size_t size);
168 void operator delete(
void*, size_t);
169 void operator delete[](
void*, size_t);
174#ifdef V8_ENABLE_CHECKS
175 int scope_level_ = 0;
179 template <
typename T>
193 isolate_ = v8_isolate;
194 prev_next_ = current->
next;
195 prev_limit_ = current->
limit;
197#ifdef V8_ENABLE_CHECKS
198 DoInitializeAsserts(v8_isolate);
199 scope_level_ = current->
level;
205#ifdef V8_ENABLE_CHECKS
206 AssertScopeLevelsMatch();
212 std::swap(current->
next, prev_next_);
216 current->
limit = prev_limit_;
218 DeleteExtensions(isolate_);
220#ifdef V8_ENABLE_CHECKS
221 DoCloseScopeAsserts(handle_count_before, limit, current);
233 result = Extend(v8_isolate);
249#ifdef V8_ENABLE_DIRECT_HANDLE
252class LocalBase :
public api_internal::DirectHandleBase {
260#ifdef V8_ENABLE_CHECKS
261 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
265 template <
typename S>
269 return LocalBase<T>(
value);
272 V8_INLINE static LocalBase<T>
New(Isolate* isolate, T* that) {
278 if (
slot ==
nullptr)
return LocalBase<T>();
279 return LocalBase<T>(*
slot);
284 return LocalBase<T>(
repr);
300#ifdef V8_ENABLE_CHECKS
301 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
305 template <
typename S>
361#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
379 requires std::is_base_of_v<T, S>
399 return internal::HandleHelper::EqualHandles(*
this, that);
404 return internal::HandleHelper::EqualHandles(*
this, that);
409 return !operator==(that);
414 return !operator==(that);
424#ifdef V8_ENABLE_CHECKS
428 T::Cast(that.template value<S>());
449 return New(isolate, that.template value<T, true>());
454 return New(isolate, that.template value<T, true>());
459 return New(isolate, that.template value<T, true>());
473 template <
class F,
class M>
493 template <
class F1,
class F2,
class F3>
499 friend class internal::SamplingHeapProfiler;
501 friend class debug::ConsoleCallArguments;
504 explicit Local(no_checking_tag do_not_check)
505 :
LocalBase<T>(), StackAllocated(do_not_check) {}
506 explicit Local(
const Local<T>& other, no_checking_tag do_not_check)
507 :
LocalBase<T>(other), StackAllocated(do_not_check) {}
509 V8_INLINE explicit Local(
const LocalBase<T>& other) : LocalBase<T>(other) {}
512 internal::ValueHelper::InternalRepresentationType repr) {
513 return Local<T>(LocalBase<T>::FromRepr(repr));
516 V8_INLINE static Local<T> FromSlot(internal::Address* slot) {
517 return Local<T>(LocalBase<T>::FromSlot(slot));
520#ifdef V8_ENABLE_DIRECT_HANDLE
521 friend class TypecheckWitness;
523 V8_INLINE static Local<T> FromAddress(internal::Address ptr) {
524 return Local<T>(LocalBase<T>(ptr));
528 V8_INLINE static Local<T> New(Isolate* isolate, internal::Address value) {
529 return Local<T>(LocalBase<T>::New(isolate, value));
532 V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
533 return Local<T>(LocalBase<T>::New(isolate, that));
539 return Local<S>(LocalBase<S>(*
this));
551#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
557 LocalUnchecked& operator=(
const LocalUnchecked&)
noexcept =
default;
565#ifdef V8_ENABLE_DIRECT_HANDLE
570class StrongRootAllocator<LocalUnchecked<T>> :
public StrongRootAllocatorBase {
572 using value_type = LocalUnchecked<T>;
573 static_assert(std::is_standard_layout_v<value_type>);
574 static_assert(
sizeof(value_type) ==
sizeof(Address));
576 template <
typename HeapOrIsolateT>
577 explicit StrongRootAllocator(HeapOrIsolateT* heap_or_isolate)
578 : StrongRootAllocatorBase(heap_or_isolate) {}
579 template <
typename U>
580 StrongRootAllocator(
const StrongRootAllocator<U>& other) noexcept
581 : StrongRootAllocatorBase(other) {}
583 value_type* allocate(
size_t n) {
584 return reinterpret_cast<value_type*
>(allocate_impl(n));
586 void deallocate(value_type* p,
size_t n)
noexcept {
587 return deallocate_impl(
reinterpret_cast<Address*
>(p), n);
598#ifdef V8_ENABLE_DIRECT_HANDLE
601 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
602 return allocator_type(isolate);
605 using allocator_type = std::allocator<element_type>;
607 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
608 return allocator_type();
612 using vector_type = std::vector<element_type, allocator_type>;
628 : backing_(n, make_allocator(isolate)) {}
630 : backing_(make_allocator(isolate)) {
631 if (init.size() == 0)
return;
632 backing_.reserve(init.size());
633 backing_.insert(backing_.end(), init.begin(), init.end());
643 size_t size() const noexcept {
return backing_.size(); }
644 bool empty() const noexcept {
return backing_.empty(); }
645 void reserve(
size_t n) { backing_.reserve(n); }
652 const Local<T>&
at(
size_t n)
const {
return backing_.at(n); }
666 template <
typename InputIt>
668 return iterator(backing_.insert(pos.
base(), first, last));
672 return iterator(backing_.insert(pos.
base(), init.begin(), init.end()));
677 backing_.reserve(init.size());
678 backing_.insert(backing_.end(), init.begin(), init.end());
685 template <
typename... Args>
687 backing_.push_back(
value_type{std::forward<Args>(args)...});
690 void clear() noexcept { backing_.clear(); }
691 void resize(
size_t n) { backing_.resize(n); }
695 return x.backing_ == y.backing_;
698 return x.backing_ != y.backing_;
701 return x.backing_ < y.backing_;
704 return x.backing_ > y.backing_;
707 return x.backing_ <= y.backing_;
710 return x.backing_ >= y.backing_;
714 vector_type backing_;
717#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
744 requires std::is_base_of_v<T, S>
750 requires std::is_base_of_v<T, S>
806 template <
typename S>
821 void*
operator new(
size_t size) =
delete;
822 void*
operator new[](
size_t size) =
delete;
823 void operator delete(
void*, size_t) =
delete;
824 void operator delete[](
void*, size_t) =
delete;
845#ifdef V8_ENABLE_DIRECT_HANDLE
848 if (value.
IsEmpty())
return value;
871 void*
operator new(
size_t size) =
delete;
872 void*
operator new[](
size_t size) =
delete;
873 void operator delete(
void*, size_t) =
delete;
874 void operator delete[](
void*, size_t) =
delete;
877 internal::Isolate*
const i_isolate_;
879 int prev_sealed_level_;
Definition: v8-traced-handle.h:124
Definition: v8-context.h:48
Definition: v8-local-handle.h:814
EscapableHandleScopeBase(Isolate *isolate)
internal::Address * EscapeSlot(internal::Address *escape_value)
EscapableHandleScopeBase(const EscapableHandleScopeBase &)=delete
void operator=(const EscapableHandleScopeBase &)=delete
~EscapableHandleScopeBase()=default
Definition: v8-local-handle.h:838
~EscapableHandleScope()=default
Local< T > Escape(Local< T > value)
Definition: v8-local-handle.h:844
MaybeLocal< T > EscapeMaybe(MaybeLocal< T > value)
Definition: v8-local-handle.h:854
EscapableHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:840
Definition: v8-local-handle.h:119
void operator=(const HandleScope &)=delete
void Initialize(Isolate *isolate)
Definition: v8-local-handle.h:190
HandleScope(const HandleScope &)=delete
Isolate * GetIsolate() const
Definition: v8-local-handle.h:130
static int NumberOfHandles(Isolate *isolate)
~HandleScope()
Definition: v8-local-handle.h:203
static internal::Address * CreateHandle(Isolate *i_isolate, internal::Address value)
Definition: v8-local-handle.h:227
static internal::Address * CreateHandleForCurrentIsolate(internal::Address value)
Definition: v8-isolate.h:290
Definition: v8-local-handle.h:291
static LocalBase< T > New(Isolate *isolate, T *that)
Definition: v8-local-handle.h:312
static LocalBase< T > FromSlot(internal::Address *slot)
Definition: v8-local-handle.h:318
LocalBase(const LocalBase< S > &other)
Definition: v8-local-handle.h:306
static LocalBase< T > FromRepr(internal::ValueHelper::InternalRepresentationType repr)
Definition: v8-local-handle.h:322
LocalBase(internal::Address *location)
Definition: v8-local-handle.h:298
static LocalBase< T > New(Isolate *isolate, internal::Address value)
Definition: v8-local-handle.h:308
Definition: v8-local-handle.h:594
LocalVector< T > & operator=(std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:675
const Local< T > & front() const
Definition: v8-local-handle.h:655
friend bool operator>=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:709
bool empty() const noexcept
Definition: v8-local-handle.h:644
const Local< T > * data() const noexcept
Definition: v8-local-handle.h:660
const_iterator end() const noexcept
Definition: v8-local-handle.h:641
const Local< T > & operator[](size_t n) const
Definition: v8-local-handle.h:649
friend bool operator<(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:700
const Local< T > & back() const
Definition: v8-local-handle.h:657
void shrink_to_fit()
Definition: v8-local-handle.h:646
Local< T > * data() noexcept
Definition: v8-local-handle.h:659
void reserve(size_t n)
Definition: v8-local-handle.h:645
Local< T > & front()
Definition: v8-local-handle.h:654
internal::WrappedIterator< typename vector_type::iterator, Local< T > > iterator
Definition: v8-local-handle.h:621
friend bool operator<=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:706
friend bool operator!=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:697
Local< T > & operator[](size_t n)
Definition: v8-local-handle.h:648
size_t size_type
Definition: v8-local-handle.h:618
void emplace_back(Args &&... args)
Definition: v8-local-handle.h:686
const_iterator begin() const noexcept
Definition: v8-local-handle.h:637
iterator insert(const_iterator pos, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:671
Local< T > & at(size_t n)
Definition: v8-local-handle.h:651
ptrdiff_t difference_type
Definition: v8-local-handle.h:619
iterator insert(const_iterator pos, const Local< T > &value)
Definition: v8-local-handle.h:662
internal::WrappedIterator< typename vector_type::const_iterator, const Local< T > > const_iterator
Definition: v8-local-handle.h:624
void pop_back()
Definition: v8-local-handle.h:683
LocalVector(Isolate *isolate, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:629
iterator end() noexcept
Definition: v8-local-handle.h:640
const Local< T > & at(size_t n) const
Definition: v8-local-handle.h:652
void clear() noexcept
Definition: v8-local-handle.h:690
void swap(LocalVector< T > &other)
Definition: v8-local-handle.h:692
iterator insert(const_iterator pos, InputIt first, InputIt last)
Definition: v8-local-handle.h:667
friend bool operator==(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:694
LocalVector(Isolate *isolate)
Definition: v8-local-handle.h:626
size_t size() const noexcept
Definition: v8-local-handle.h:643
void resize(size_t n)
Definition: v8-local-handle.h:691
Local< T > & back()
Definition: v8-local-handle.h:656
LocalVector(Isolate *isolate, size_t n)
Definition: v8-local-handle.h:627
void push_back(const Local< T > &x)
Definition: v8-local-handle.h:682
iterator begin() noexcept
Definition: v8-local-handle.h:636
friend bool operator>(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:703
Definition: v8-local-handle.h:366
friend class TracedReferenceBase
Definition: v8-local-handle.h:463
friend class Object
Definition: v8-local-handle.h:480
friend class PersistentValueMapBase
Definition: v8-local-handle.h:494
static Local< T > New(Isolate *isolate, const PersistentBase< T > &that)
Definition: v8-local-handle.h:452
friend class PropertyCallbackInfo
Definition: v8-local-handle.h:478
friend class ReturnValue
Definition: v8-local-handle.h:496
friend class HandleScope
Definition: v8-local-handle.h:490
friend class Traced
Definition: v8-local-handle.h:498
T * operator*() const
Definition: v8-local-handle.h:384
static Local< T > Cast(Local< S > that)
Definition: v8-local-handle.h:423
friend class FunctionCallbackInfo
Definition: v8-local-handle.h:476
Local< S > As() const
Definition: v8-local-handle.h:439
friend class String
Definition: v8-local-handle.h:479
bool operator!=(const Local< S > &that) const
Definition: v8-local-handle.h:408
friend class MaybeLocal
Definition: v8-local-handle.h:472
static Local< T > New(Isolate *isolate, const BasicTracedReference< T > &that)
Definition: v8-local-handle.h:457
bool operator==(const PersistentBase< S > &that) const
Definition: v8-local-handle.h:403
bool operator!=(const Persistent< S > &that) const
Definition: v8-local-handle.h:413
friend class Isolate
Definition: v8-local-handle.h:482
friend class Utils
Definition: v8-local-handle.h:464
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:448
friend class Context
Definition: v8-local-handle.h:481
friend class Private
Definition: v8-local-handle.h:483
friend class Persistent
Definition: v8-local-handle.h:474
friend class Global
Definition: v8-local-handle.h:468
T * operator->() const
Definition: v8-local-handle.h:382
friend class EscapableHandleScope
Definition: v8-local-handle.h:491
friend class Eternal
Definition: v8-local-handle.h:466
bool operator==(const Local< S > &that) const
Definition: v8-local-handle.h:398
Local(Local< S > that)
Definition: v8-local-handle.h:380
friend class InternalEscapableScope
Definition: v8-local-handle.h:492
friend class Local
Definition: v8-local-handle.h:470
Definition: v8-local-handle.h:734
bool ToLocal(Local< S > *out) const
Definition: v8-local-handle.h:760
static MaybeLocal< T > Cast(MaybeLocal< S > that)
Definition: v8-local-handle.h:789
MaybeLocal< S > As() const
Definition: v8-local-handle.h:799
Local< T > ToLocalChecked()
Definition: v8-local-handle.h:769
Local< S > FromMaybe(Local< S > default_value) const
Definition: v8-local-handle.h:779
bool IsEmpty() const
Definition: v8-local-handle.h:753
MaybeLocal(Local< S > that)
Definition: v8-local-handle.h:745
MaybeLocal(MaybeLocal< S > that)
Definition: v8-local-handle.h:751
Definition: v8-object.h:235
Definition: v8-persistent-handle.h:93
Definition: v8-persistent-handle.h:250
Definition: v8-local-handle.h:864
SealHandleScope(const SealHandleScope &)=delete
void operator=(const SealHandleScope &)=delete
SealHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:63
Definition: v8-value.h:515
Definition: v8-primitive.h:920
Definition: v8-value.h:32
Definition: v8-handle-base.h:57
T * value() const
Definition: v8-handle-base.h:89
internal::Address ptr() const
Definition: v8-handle-base.h:80
IndirectHandleBase()=default
internal::Address *const & slot() const
Definition: v8-handle-base.h:83
bool IsEmpty() const
Definition: v8-handle-base.h:60
internal::ValueHelper::InternalRepresentationType repr() const
Definition: v8-handle-base.h:98
Definition: v8-handle-base.h:13
Definition: v8-local-handle.h:75
Definition: v8-internal.h:1774
Definition: v8-internal.h:874
Definition: v8-local-handle.h:547
LocalUnchecked()
Definition: v8-local-handle.h:549
LocalUnchecked(const Local< T > &other) noexcept
Definition: v8-local-handle.h:561
Definition: v8-internal.h:1456
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1748
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1702
static bool IsEmpty(T *value)
Definition: v8-internal.h:1707
Definition: v8-internal.h:1515
constexpr const Iterator & base() const noexcept
Definition: v8-internal.h:1679
internal::BasicPersistent< T, internal::StrongPersistentPolicy > Persistent
Definition: persistent.h:363
uintptr_t Address
Definition: v8-internal.h:52
Definition: libplatform.h:15
Local< Primitive > Null(Isolate *isolate)
Definition: v8-primitive.h:1078
Local< Primitive > Undefined(Isolate *isolate)
Definition: v8-primitive.h:1070
Local< Boolean > False(Isolate *isolate)
Definition: v8-primitive.h:1094
Local< Boolean > True(Isolate *isolate)
Definition: v8-primitive.h:1086
Definition: v8-internal.h:852
Address * next
Definition: v8-internal.h:856
int level
Definition: v8-internal.h:858
Address * limit
Definition: v8-internal.h:857
#define V8_EXPORT
Definition: v8config.h:849
#define V8_INLINE
Definition: v8config.h:513
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:684
#define V8_UNLIKELY(condition)
Definition: v8config.h:673
#define V8_TRIVIAL_ABI
Definition: v8config.h:802
#define V8_NODISCARD
Definition: v8config.h:706