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;
71class ConsoleCallArguments;
79class SamplingHeapProfiler;
82namespace api_internal {
86#ifdef V8_ENABLE_CHECKS
87template <
typename T,
typename V = Value>
88void TypeCheckLocal(
V* value) {
90 if constexpr (
requires { T::Cast(value); }) {
144 void DeleteExtensions(
Isolate* isolate);
146#ifdef V8_ENABLE_CHECKS
148 void DoInitializeAsserts(
Isolate* isolate);
150 void AssertScopeLevelsMatch();
159 void*
operator new(
size_t size);
160 void*
operator new[](
size_t size);
161 void operator delete(
void*, size_t);
162 void operator delete[](
void*, size_t);
167#ifdef V8_ENABLE_CHECKS
168 int scope_level_ = 0;
172 template <
typename T>
186 isolate_ = v8_isolate;
187 prev_next_ = current->
next;
188 prev_limit_ = current->
limit;
190#ifdef V8_ENABLE_CHECKS
191 DoInitializeAsserts(v8_isolate);
192 scope_level_ = current->
level;
198#ifdef V8_ENABLE_CHECKS
199 AssertScopeLevelsMatch();
205 std::swap(current->
next, prev_next_);
209 current->
limit = prev_limit_;
211 DeleteExtensions(isolate_);
213#ifdef V8_ENABLE_CHECKS
214 DoCloseScopeAsserts(handle_count_before, limit, current);
226 result = Extend(v8_isolate);
242#ifdef V8_ENABLE_DIRECT_HANDLE
245class LocalBase :
public api_internal::DirectHandleBase {
253#ifdef V8_ENABLE_CHECKS
254 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
258 template <
typename S>
262 return LocalBase<T>(
value);
265 V8_INLINE static LocalBase<T>
New(Isolate* isolate, T* that) {
271 if (
slot ==
nullptr)
return LocalBase<T>();
272 return LocalBase<T>(*
slot);
277 return LocalBase<T>(
repr);
293#ifdef V8_ENABLE_CHECKS
294 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
298 template <
typename S>
354#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
372 requires std::is_base_of_v<T, S>
392 return internal::HandleHelper::EqualHandles(*
this, that);
397 return internal::HandleHelper::EqualHandles(*
this, that);
402 return !operator==(that);
407 return !operator==(that);
417#ifdef V8_ENABLE_CHECKS
421 T::Cast(that.template value<S>());
442 return New(isolate, that.template value<T, true>());
447 return New(isolate, that.template value<T, true>());
452 return New(isolate, that.template value<T, true>());
466 template <
class F,
class M>
489 template <
class F1,
class F2,
class F3>
495 friend class internal::SamplingHeapProfiler;
497 friend class debug::ConsoleCallArguments;
500 explicit Local(no_checking_tag do_not_check)
501 :
LocalBase<T>(), StackAllocated(do_not_check) {}
502 explicit Local(
const Local<T>& other, no_checking_tag do_not_check)
503 :
LocalBase<T>(other), StackAllocated(do_not_check) {}
505 V8_INLINE explicit Local(
const LocalBase<T>& other) : LocalBase<T>(other) {}
508 internal::ValueHelper::InternalRepresentationType repr) {
509 return Local<T>(LocalBase<T>::FromRepr(repr));
512 V8_INLINE static Local<T> FromSlot(internal::Address* slot) {
513 return Local<T>(LocalBase<T>::FromSlot(slot));
516#ifdef V8_ENABLE_DIRECT_HANDLE
517 friend class TypecheckWitness;
519 V8_INLINE static Local<T> FromAddress(internal::Address ptr) {
520 return Local<T>(LocalBase<T>(ptr));
524 V8_INLINE static Local<T> New(Isolate* isolate, internal::Address value) {
525 return Local<T>(LocalBase<T>::New(isolate, value));
528 V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
529 return Local<T>(LocalBase<T>::New(isolate, that));
535 return Local<S>(LocalBase<S>(*
this));
547#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
553 LocalUnchecked& operator=(
const LocalUnchecked&)
noexcept =
default;
561#ifdef V8_ENABLE_DIRECT_HANDLE
566class StrongRootAllocator<LocalUnchecked<T>> :
public StrongRootAllocatorBase {
568 using value_type = LocalUnchecked<T>;
569 static_assert(std::is_standard_layout_v<value_type>);
570 static_assert(
sizeof(value_type) ==
sizeof(Address));
572 template <
typename HeapOrIsolateT>
573 explicit StrongRootAllocator(HeapOrIsolateT* heap_or_isolate)
574 : StrongRootAllocatorBase(heap_or_isolate) {}
575 template <
typename U>
576 StrongRootAllocator(
const StrongRootAllocator<U>& other) noexcept
577 : StrongRootAllocatorBase(other) {}
579 value_type* allocate(
size_t n) {
580 return reinterpret_cast<value_type*
>(allocate_impl(n));
582 void deallocate(value_type* p,
size_t n)
noexcept {
583 return deallocate_impl(
reinterpret_cast<Address*
>(p), n);
594#ifdef V8_ENABLE_DIRECT_HANDLE
597 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
598 return allocator_type(isolate);
601 using allocator_type = std::allocator<element_type>;
603 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
604 return allocator_type();
608 using vector_type = std::vector<element_type, allocator_type>;
624 : backing_(n, make_allocator(isolate)) {}
626 : backing_(make_allocator(isolate)) {
627 if (init.size() == 0)
return;
628 backing_.reserve(init.size());
629 backing_.insert(backing_.end(), init.begin(), init.end());
639 size_t size() const noexcept {
return backing_.size(); }
640 bool empty() const noexcept {
return backing_.empty(); }
641 void reserve(
size_t n) { backing_.reserve(n); }
648 const Local<T>&
at(
size_t n)
const {
return backing_.at(n); }
662 template <
typename InputIt>
664 return iterator(backing_.insert(pos.
base(), first, last));
668 return iterator(backing_.insert(pos.
base(), init.begin(), init.end()));
673 backing_.reserve(init.size());
674 backing_.insert(backing_.end(), init.begin(), init.end());
681 template <
typename... Args>
683 backing_.push_back(
value_type{std::forward<Args>(args)...});
686 void clear() noexcept { backing_.clear(); }
687 void resize(
size_t n) { backing_.resize(n); }
691 return x.backing_ == y.backing_;
694 return x.backing_ != y.backing_;
697 return x.backing_ < y.backing_;
700 return x.backing_ > y.backing_;
703 return x.backing_ <= y.backing_;
706 return x.backing_ >= y.backing_;
710 vector_type backing_;
713#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
740 requires std::is_base_of_v<T, S>
746 requires std::is_base_of_v<T, S>
802 template <
typename S>
817 void*
operator new(
size_t size) =
delete;
818 void*
operator new[](
size_t size) =
delete;
819 void operator delete(
void*, size_t) =
delete;
820 void operator delete[](
void*, size_t) =
delete;
841#ifdef V8_ENABLE_DIRECT_HANDLE
844 if (value.
IsEmpty())
return value;
867 void*
operator new(
size_t size) =
delete;
868 void*
operator new[](
size_t size) =
delete;
869 void operator delete(
void*, size_t) =
delete;
870 void operator delete[](
void*, size_t) =
delete;
873 internal::Isolate*
const i_isolate_;
875 int prev_sealed_level_;
Definition: v8-traced-handle.h:125
Definition: v8-context.h:55
Definition: v8-local-handle.h:810
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:834
~EscapableHandleScope()=default
Local< T > Escape(Local< T > value)
Definition: v8-local-handle.h:840
MaybeLocal< T > EscapeMaybe(MaybeLocal< T > value)
Definition: v8-local-handle.h:850
EscapableHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:836
Definition: v8-local-handle.h:112
void operator=(const HandleScope &)=delete
void Initialize(Isolate *isolate)
Definition: v8-local-handle.h:183
HandleScope(const HandleScope &)=delete
Isolate * GetIsolate() const
Definition: v8-local-handle.h:123
static int NumberOfHandles(Isolate *isolate)
~HandleScope()
Definition: v8-local-handle.h:196
static internal::Address * CreateHandle(Isolate *i_isolate, internal::Address value)
Definition: v8-local-handle.h:220
static internal::Address * CreateHandleForCurrentIsolate(internal::Address value)
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:284
static LocalBase< T > New(Isolate *isolate, T *that)
Definition: v8-local-handle.h:305
static LocalBase< T > FromSlot(internal::Address *slot)
Definition: v8-local-handle.h:311
LocalBase(const LocalBase< S > &other)
Definition: v8-local-handle.h:299
static LocalBase< T > FromRepr(internal::ValueHelper::InternalRepresentationType repr)
Definition: v8-local-handle.h:315
LocalBase(internal::Address *location)
Definition: v8-local-handle.h:291
static LocalBase< T > New(Isolate *isolate, internal::Address value)
Definition: v8-local-handle.h:301
Definition: v8-local-handle.h:590
LocalVector< T > & operator=(std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:671
const Local< T > & front() const
Definition: v8-local-handle.h:651
friend bool operator>=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:705
bool empty() const noexcept
Definition: v8-local-handle.h:640
const Local< T > * data() const noexcept
Definition: v8-local-handle.h:656
const_iterator end() const noexcept
Definition: v8-local-handle.h:637
const Local< T > & operator[](size_t n) const
Definition: v8-local-handle.h:645
friend bool operator<(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:696
const Local< T > & back() const
Definition: v8-local-handle.h:653
void shrink_to_fit()
Definition: v8-local-handle.h:642
Local< T > * data() noexcept
Definition: v8-local-handle.h:655
void reserve(size_t n)
Definition: v8-local-handle.h:641
Local< T > & front()
Definition: v8-local-handle.h:650
internal::WrappedIterator< typename vector_type::iterator, Local< T > > iterator
Definition: v8-local-handle.h:617
friend bool operator<=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:702
friend bool operator!=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:693
Local< T > & operator[](size_t n)
Definition: v8-local-handle.h:644
size_t size_type
Definition: v8-local-handle.h:614
void emplace_back(Args &&... args)
Definition: v8-local-handle.h:682
const_iterator begin() const noexcept
Definition: v8-local-handle.h:633
iterator insert(const_iterator pos, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:667
Local< T > & at(size_t n)
Definition: v8-local-handle.h:647
ptrdiff_t difference_type
Definition: v8-local-handle.h:615
iterator insert(const_iterator pos, const Local< T > &value)
Definition: v8-local-handle.h:658
internal::WrappedIterator< typename vector_type::const_iterator, const Local< T > > const_iterator
Definition: v8-local-handle.h:620
void pop_back()
Definition: v8-local-handle.h:679
LocalVector(Isolate *isolate, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:625
iterator end() noexcept
Definition: v8-local-handle.h:636
const Local< T > & at(size_t n) const
Definition: v8-local-handle.h:648
void clear() noexcept
Definition: v8-local-handle.h:686
void swap(LocalVector< T > &other)
Definition: v8-local-handle.h:688
iterator insert(const_iterator pos, InputIt first, InputIt last)
Definition: v8-local-handle.h:663
friend bool operator==(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:690
LocalVector(Isolate *isolate)
Definition: v8-local-handle.h:622
size_t size() const noexcept
Definition: v8-local-handle.h:639
void resize(size_t n)
Definition: v8-local-handle.h:687
Local< T > & back()
Definition: v8-local-handle.h:652
LocalVector(Isolate *isolate, size_t n)
Definition: v8-local-handle.h:623
void push_back(const Local< T > &x)
Definition: v8-local-handle.h:678
iterator begin() noexcept
Definition: v8-local-handle.h:632
friend bool operator>(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:699
Definition: v8-local-handle.h:359
friend class TracedReferenceBase
Definition: v8-local-handle.h:456
friend class Object
Definition: v8-local-handle.h:474
friend class PersistentValueMapBase
Definition: v8-local-handle.h:490
friend class WasmMemoryObject
Definition: v8-local-handle.h:479
friend class RegExp
Definition: v8-local-handle.h:478
friend class ArrayBufferView
Definition: v8-local-handle.h:472
static Local< T > New(Isolate *isolate, const PersistentBase< T > &that)
Definition: v8-local-handle.h:445
friend class PropertyCallbackInfo
Definition: v8-local-handle.h:471
friend class ReturnValue
Definition: v8-local-handle.h:492
friend class HandleScope
Definition: v8-local-handle.h:486
friend class Traced
Definition: v8-local-handle.h:494
T * operator*() const
Definition: v8-local-handle.h:377
static Local< T > Cast(Local< S > that)
Definition: v8-local-handle.h:416
friend class FunctionCallbackInfo
Definition: v8-local-handle.h:469
Local< S > As() const
Definition: v8-local-handle.h:432
friend class String
Definition: v8-local-handle.h:473
bool operator!=(const Local< S > &that) const
Definition: v8-local-handle.h:401
friend class MaybeLocal
Definition: v8-local-handle.h:465
static Local< T > New(Isolate *isolate, const BasicTracedReference< T > &that)
Definition: v8-local-handle.h:450
bool operator==(const PersistentBase< S > &that) const
Definition: v8-local-handle.h:396
bool operator!=(const Persistent< S > &that) const
Definition: v8-local-handle.h:406
friend class Isolate
Definition: v8-local-handle.h:476
friend class Utils
Definition: v8-local-handle.h:457
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:441
friend class Context
Definition: v8-local-handle.h:475
friend class Private
Definition: v8-local-handle.h:477
friend class Persistent
Definition: v8-local-handle.h:467
friend class Global
Definition: v8-local-handle.h:461
T * operator->() const
Definition: v8-local-handle.h:375
friend class EscapableHandleScope
Definition: v8-local-handle.h:487
friend class Eternal
Definition: v8-local-handle.h:459
bool operator==(const Local< S > &that) const
Definition: v8-local-handle.h:391
Local(Local< S > that)
Definition: v8-local-handle.h:373
friend class InternalEscapableScope
Definition: v8-local-handle.h:488
friend class Local
Definition: v8-local-handle.h:463
Definition: v8-local-handle.h:730
bool ToLocal(Local< S > *out) const
Definition: v8-local-handle.h:756
static MaybeLocal< T > Cast(MaybeLocal< S > that)
Definition: v8-local-handle.h:785
MaybeLocal< S > As() const
Definition: v8-local-handle.h:795
Local< T > ToLocalChecked()
Definition: v8-local-handle.h:765
Local< S > FromMaybe(Local< S > default_value) const
Definition: v8-local-handle.h:775
bool IsEmpty() const
Definition: v8-local-handle.h:749
MaybeLocal(Local< S > that)
Definition: v8-local-handle.h:741
MaybeLocal(MaybeLocal< S > that)
Definition: v8-local-handle.h:747
Definition: v8-object.h:262
Definition: v8-persistent-handle.h:93
Definition: v8-persistent-handle.h:250
Definition: v8-local-handle.h:860
SealHandleScope(const SealHandleScope &)=delete
void operator=(const SealHandleScope &)=delete
SealHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:64
Definition: v8-value.h:527
Definition: v8-value.h:32
Definition: v8-wasm.h:104
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:76
Definition: v8-internal.h:1863
Definition: v8-internal.h:969
Definition: v8-local-handle.h:543
LocalUnchecked()
Definition: v8-local-handle.h:545
LocalUnchecked(const Local< T > &other) noexcept
Definition: v8-local-handle.h:557
Definition: v8-internal.h:1594
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1837
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1791
static bool IsEmpty(T *value)
Definition: v8-internal.h:1796
Definition: v8-internal.h:1642
constexpr const Iterator & base() const noexcept
Definition: v8-internal.h:1768
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:1090
Local< Primitive > Undefined(Isolate *isolate)
Definition: v8-primitive.h:1082
Local< Boolean > False(Isolate *isolate)
Definition: v8-primitive.h:1106
Local< Boolean > True(Isolate *isolate)
Definition: v8-primitive.h:1098
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