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;
57class PropertyCallbackInfo;
68class ConsoleCallArguments;
76class SamplingHeapProfiler;
79namespace api_internal {
83#ifdef V8_ENABLE_CHECKS
84template <
typename T,
typename V = Value>
85void TypeCheckLocal(
V* value) {
87 if constexpr (
requires { T::Cast(value); }) {
141 void DeleteExtensions(
Isolate* isolate);
143#ifdef V8_ENABLE_CHECKS
145 void DoInitializeAsserts(
Isolate* isolate);
147 void AssertScopeLevelsMatch();
156 void*
operator new(
size_t size);
157 void*
operator new[](
size_t size);
158 void operator delete(
void*, size_t);
159 void operator delete[](
void*, size_t);
164#ifdef V8_ENABLE_CHECKS
165 int scope_level_ = 0;
169 template <
typename T>
183 isolate_ = v8_isolate;
184 prev_next_ = current->
next;
185 prev_limit_ = current->
limit;
187#ifdef V8_ENABLE_CHECKS
188 DoInitializeAsserts(v8_isolate);
189 scope_level_ = current->
level;
195#ifdef V8_ENABLE_CHECKS
196 AssertScopeLevelsMatch();
202 std::swap(current->
next, prev_next_);
206 current->
limit = prev_limit_;
208 DeleteExtensions(isolate_);
210#ifdef V8_ENABLE_CHECKS
211 DoCloseScopeAsserts(handle_count_before, limit, current);
223 result = Extend(v8_isolate);
239#ifdef V8_ENABLE_DIRECT_HANDLE
242class LocalBase :
public api_internal::DirectHandleBase {
250#ifdef V8_ENABLE_CHECKS
251 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
255 template <
typename S>
259 return LocalBase<T>(
value);
262 V8_INLINE static LocalBase<T>
New(Isolate* isolate, T* that) {
268 if (
slot ==
nullptr)
return LocalBase<T>();
269 return LocalBase<T>(*
slot);
274 return LocalBase<T>(
repr);
290#ifdef V8_ENABLE_CHECKS
291 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
295 template <
typename S>
351#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
369 requires std::is_base_of_v<T, S>
389 return internal::HandleHelper::EqualHandles(*
this, that);
394 return internal::HandleHelper::EqualHandles(*
this, that);
399 return !operator==(that);
404 return !operator==(that);
414#ifdef V8_ENABLE_CHECKS
418 T::Cast(that.template value<S>());
439 return New(isolate, that.template value<T, true>());
444 return New(isolate, that.template value<T, true>());
449 return New(isolate, that.template value<T, true>());
463 template <
class F,
class M>
483 template <
class F1,
class F2,
class F3>
489 friend class internal::SamplingHeapProfiler;
491 friend class debug::ConsoleCallArguments;
494 explicit Local(no_checking_tag do_not_check)
495 :
LocalBase<T>(), StackAllocated(do_not_check) {}
496 explicit Local(
const Local<T>& other, no_checking_tag do_not_check)
497 :
LocalBase<T>(other), StackAllocated(do_not_check) {}
499 V8_INLINE explicit Local(
const LocalBase<T>& other) : LocalBase<T>(other) {}
502 internal::ValueHelper::InternalRepresentationType repr) {
503 return Local<T>(LocalBase<T>::FromRepr(repr));
506 V8_INLINE static Local<T> FromSlot(internal::Address* slot) {
507 return Local<T>(LocalBase<T>::FromSlot(slot));
510#ifdef V8_ENABLE_DIRECT_HANDLE
511 friend class TypecheckWitness;
513 V8_INLINE static Local<T> FromAddress(internal::Address ptr) {
514 return Local<T>(LocalBase<T>(ptr));
518 V8_INLINE static Local<T> New(Isolate* isolate, internal::Address value) {
519 return Local<T>(LocalBase<T>::New(isolate, value));
522 V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
523 return Local<T>(LocalBase<T>::New(isolate, that));
529 return Local<S>(LocalBase<S>(*
this));
541#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
547 LocalUnchecked& operator=(
const LocalUnchecked&)
noexcept =
default;
555#ifdef V8_ENABLE_DIRECT_HANDLE
560class StrongRootAllocator<LocalUnchecked<T>> :
public StrongRootAllocatorBase {
562 using value_type = LocalUnchecked<T>;
563 static_assert(std::is_standard_layout_v<value_type>);
564 static_assert(
sizeof(value_type) ==
sizeof(Address));
566 template <
typename HeapOrIsolateT>
567 explicit StrongRootAllocator(HeapOrIsolateT* heap_or_isolate)
568 : StrongRootAllocatorBase(heap_or_isolate) {}
569 template <
typename U>
570 StrongRootAllocator(
const StrongRootAllocator<U>& other) noexcept
571 : StrongRootAllocatorBase(other) {}
573 value_type* allocate(
size_t n) {
574 return reinterpret_cast<value_type*
>(allocate_impl(n));
576 void deallocate(value_type* p,
size_t n)
noexcept {
577 return deallocate_impl(
reinterpret_cast<Address*
>(p), n);
588#ifdef V8_ENABLE_DIRECT_HANDLE
591 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
592 return allocator_type(isolate);
595 using allocator_type = std::allocator<element_type>;
597 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
598 return allocator_type();
602 using vector_type = std::vector<element_type, allocator_type>;
618 : backing_(n, make_allocator(isolate)) {}
620 : backing_(make_allocator(isolate)) {
621 if (init.size() == 0)
return;
622 backing_.reserve(init.size());
623 backing_.insert(backing_.end(), init.begin(), init.end());
633 size_t size() const noexcept {
return backing_.size(); }
634 bool empty() const noexcept {
return backing_.empty(); }
635 void reserve(
size_t n) { backing_.reserve(n); }
642 const Local<T>&
at(
size_t n)
const {
return backing_.at(n); }
656 template <
typename InputIt>
658 return iterator(backing_.insert(pos.
base(), first, last));
662 return iterator(backing_.insert(pos.
base(), init.begin(), init.end()));
667 backing_.reserve(init.size());
668 backing_.insert(backing_.end(), init.begin(), init.end());
675 template <
typename... Args>
677 backing_.push_back(
value_type{std::forward<Args>(args)...});
680 void clear() noexcept { backing_.clear(); }
681 void resize(
size_t n) { backing_.resize(n); }
685 return x.backing_ == y.backing_;
688 return x.backing_ != y.backing_;
691 return x.backing_ < y.backing_;
694 return x.backing_ > y.backing_;
697 return x.backing_ <= y.backing_;
700 return x.backing_ >= y.backing_;
704 vector_type backing_;
707#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
734 requires std::is_base_of_v<T, S>
740 requires std::is_base_of_v<T, S>
796 template <
typename S>
811 void*
operator new(
size_t size) =
delete;
812 void*
operator new[](
size_t size) =
delete;
813 void operator delete(
void*, size_t) =
delete;
814 void operator delete[](
void*, size_t) =
delete;
835#ifdef V8_ENABLE_DIRECT_HANDLE
838 if (value.
IsEmpty())
return value;
861 void*
operator new(
size_t size) =
delete;
862 void*
operator new[](
size_t size) =
delete;
863 void operator delete(
void*, size_t) =
delete;
864 void operator delete[](
void*, size_t) =
delete;
867 internal::Isolate*
const i_isolate_;
869 int prev_sealed_level_;
Definition: v8-traced-handle.h:125
Definition: v8-context.h:55
Definition: v8-local-handle.h:804
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:828
~EscapableHandleScope()=default
Local< T > Escape(Local< T > value)
Definition: v8-local-handle.h:834
MaybeLocal< T > EscapeMaybe(MaybeLocal< T > value)
Definition: v8-local-handle.h:844
EscapableHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:830
Definition: v8-local-handle.h:109
void operator=(const HandleScope &)=delete
void Initialize(Isolate *isolate)
Definition: v8-local-handle.h:180
HandleScope(const HandleScope &)=delete
Isolate * GetIsolate() const
Definition: v8-local-handle.h:120
static int NumberOfHandles(Isolate *isolate)
~HandleScope()
Definition: v8-local-handle.h:193
static internal::Address * CreateHandle(Isolate *i_isolate, internal::Address value)
Definition: v8-local-handle.h:217
static internal::Address * CreateHandleForCurrentIsolate(internal::Address value)
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:281
static LocalBase< T > New(Isolate *isolate, T *that)
Definition: v8-local-handle.h:302
static LocalBase< T > FromSlot(internal::Address *slot)
Definition: v8-local-handle.h:308
LocalBase(const LocalBase< S > &other)
Definition: v8-local-handle.h:296
static LocalBase< T > FromRepr(internal::ValueHelper::InternalRepresentationType repr)
Definition: v8-local-handle.h:312
LocalBase(internal::Address *location)
Definition: v8-local-handle.h:288
static LocalBase< T > New(Isolate *isolate, internal::Address value)
Definition: v8-local-handle.h:298
Definition: v8-local-handle.h:584
LocalVector< T > & operator=(std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:665
const Local< T > & front() const
Definition: v8-local-handle.h:645
friend bool operator>=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:699
bool empty() const noexcept
Definition: v8-local-handle.h:634
const Local< T > * data() const noexcept
Definition: v8-local-handle.h:650
const_iterator end() const noexcept
Definition: v8-local-handle.h:631
const Local< T > & operator[](size_t n) const
Definition: v8-local-handle.h:639
friend bool operator<(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:690
const Local< T > & back() const
Definition: v8-local-handle.h:647
void shrink_to_fit()
Definition: v8-local-handle.h:636
Local< T > * data() noexcept
Definition: v8-local-handle.h:649
void reserve(size_t n)
Definition: v8-local-handle.h:635
Local< T > & front()
Definition: v8-local-handle.h:644
internal::WrappedIterator< typename vector_type::iterator, Local< T > > iterator
Definition: v8-local-handle.h:611
friend bool operator<=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:696
friend bool operator!=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:687
Local< T > & operator[](size_t n)
Definition: v8-local-handle.h:638
size_t size_type
Definition: v8-local-handle.h:608
void emplace_back(Args &&... args)
Definition: v8-local-handle.h:676
const_iterator begin() const noexcept
Definition: v8-local-handle.h:627
iterator insert(const_iterator pos, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:661
Local< T > & at(size_t n)
Definition: v8-local-handle.h:641
ptrdiff_t difference_type
Definition: v8-local-handle.h:609
iterator insert(const_iterator pos, const Local< T > &value)
Definition: v8-local-handle.h:652
internal::WrappedIterator< typename vector_type::const_iterator, const Local< T > > const_iterator
Definition: v8-local-handle.h:614
void pop_back()
Definition: v8-local-handle.h:673
LocalVector(Isolate *isolate, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:619
iterator end() noexcept
Definition: v8-local-handle.h:630
const Local< T > & at(size_t n) const
Definition: v8-local-handle.h:642
void clear() noexcept
Definition: v8-local-handle.h:680
void swap(LocalVector< T > &other)
Definition: v8-local-handle.h:682
iterator insert(const_iterator pos, InputIt first, InputIt last)
Definition: v8-local-handle.h:657
friend bool operator==(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:684
LocalVector(Isolate *isolate)
Definition: v8-local-handle.h:616
size_t size() const noexcept
Definition: v8-local-handle.h:633
void resize(size_t n)
Definition: v8-local-handle.h:681
Local< T > & back()
Definition: v8-local-handle.h:646
LocalVector(Isolate *isolate, size_t n)
Definition: v8-local-handle.h:617
void push_back(const Local< T > &x)
Definition: v8-local-handle.h:672
iterator begin() noexcept
Definition: v8-local-handle.h:626
friend bool operator>(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:693
Definition: v8-local-handle.h:356
friend class TracedReferenceBase
Definition: v8-local-handle.h:453
friend class Object
Definition: v8-local-handle.h:470
friend class PersistentValueMapBase
Definition: v8-local-handle.h:484
static Local< T > New(Isolate *isolate, const PersistentBase< T > &that)
Definition: v8-local-handle.h:442
friend class PropertyCallbackInfo
Definition: v8-local-handle.h:468
friend class ReturnValue
Definition: v8-local-handle.h:486
friend class HandleScope
Definition: v8-local-handle.h:480
friend class Traced
Definition: v8-local-handle.h:488
T * operator*() const
Definition: v8-local-handle.h:374
static Local< T > Cast(Local< S > that)
Definition: v8-local-handle.h:413
friend class FunctionCallbackInfo
Definition: v8-local-handle.h:466
Local< S > As() const
Definition: v8-local-handle.h:429
friend class String
Definition: v8-local-handle.h:469
bool operator!=(const Local< S > &that) const
Definition: v8-local-handle.h:398
friend class MaybeLocal
Definition: v8-local-handle.h:462
static Local< T > New(Isolate *isolate, const BasicTracedReference< T > &that)
Definition: v8-local-handle.h:447
bool operator==(const PersistentBase< S > &that) const
Definition: v8-local-handle.h:393
bool operator!=(const Persistent< S > &that) const
Definition: v8-local-handle.h:403
friend class Isolate
Definition: v8-local-handle.h:472
friend class Utils
Definition: v8-local-handle.h:454
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:438
friend class Context
Definition: v8-local-handle.h:471
friend class Private
Definition: v8-local-handle.h:473
friend class Persistent
Definition: v8-local-handle.h:464
friend class Global
Definition: v8-local-handle.h:458
T * operator->() const
Definition: v8-local-handle.h:372
friend class EscapableHandleScope
Definition: v8-local-handle.h:481
friend class Eternal
Definition: v8-local-handle.h:456
bool operator==(const Local< S > &that) const
Definition: v8-local-handle.h:388
Local(Local< S > that)
Definition: v8-local-handle.h:370
friend class InternalEscapableScope
Definition: v8-local-handle.h:482
friend class Local
Definition: v8-local-handle.h:460
Definition: v8-local-handle.h:724
bool ToLocal(Local< S > *out) const
Definition: v8-local-handle.h:750
static MaybeLocal< T > Cast(MaybeLocal< S > that)
Definition: v8-local-handle.h:779
MaybeLocal< S > As() const
Definition: v8-local-handle.h:789
Local< T > ToLocalChecked()
Definition: v8-local-handle.h:759
Local< S > FromMaybe(Local< S > default_value) const
Definition: v8-local-handle.h:769
bool IsEmpty() const
Definition: v8-local-handle.h:743
MaybeLocal(Local< S > that)
Definition: v8-local-handle.h:735
MaybeLocal(MaybeLocal< S > that)
Definition: v8-local-handle.h:741
Definition: v8-object.h:269
Definition: v8-persistent-handle.h:93
Definition: v8-persistent-handle.h:250
Definition: v8-local-handle.h:854
SealHandleScope(const SealHandleScope &)=delete
void operator=(const SealHandleScope &)=delete
SealHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:62
Definition: v8-value.h:527
Definition: v8-value.h:32
Definition: v8-handle-base.h:57
T * value() const
Definition: v8-handle-base.h:99
internal::Address ptr() const
Definition: v8-handle-base.h:80
IndirectHandleBase()=default
internal::Address *const & slot() const
Definition: v8-handle-base.h:93
bool IsEmpty() const
Definition: v8-handle-base.h:60
internal::ValueHelper::InternalRepresentationType repr() const
Definition: v8-handle-base.h:108
Definition: v8-handle-base.h:13
Definition: v8-local-handle.h:73
Definition: v8-internal.h:1866
Definition: v8-internal.h:969
Definition: v8-local-handle.h:537
LocalUnchecked()
Definition: v8-local-handle.h:539
LocalUnchecked(const Local< T > &other) noexcept
Definition: v8-local-handle.h:551
Definition: v8-internal.h:1597
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1840
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1794
static bool IsEmpty(T *value)
Definition: v8-internal.h:1799
Definition: v8-internal.h:1645
constexpr const Iterator & base() const noexcept
Definition: v8-internal.h:1771
internal::BasicPersistent< T, internal::StrongPersistentPolicy > Persistent
Definition: persistent.h:362
uintptr_t Address
Definition: v8-internal.h:38
Definition: libplatform.h:15
Local< Primitive > Null(Isolate *isolate)
Definition: v8-primitive.h:1088
Local< Primitive > Undefined(Isolate *isolate)
Definition: v8-primitive.h:1080
Local< Boolean > False(Isolate *isolate)
Definition: v8-primitive.h:1104
Local< Boolean > True(Isolate *isolate)
Definition: v8-primitive.h:1096
Definition: v8-internal.h:947
Address * next
Definition: v8-internal.h:951
int level
Definition: v8-internal.h:953
Address * limit
Definition: v8-internal.h:952
#define V8_EXPORT
Definition: v8config.h:875
#define V8_INLINE
Definition: v8config.h:519
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:689
#define V8_UNLIKELY(condition)
Definition: v8config.h:678
#define V8_TRIVIAL_ABI
Definition: v8config.h:828
#define V8_NODISCARD
Definition: v8config.h:711