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);
154 void DoInitializeAsserts(
Isolate* isolate);
156 void AssertScopeLevelsMatch();
164 void*
operator new(
size_t size);
165 void*
operator new[](
size_t size);
166 void operator delete(
void*, size_t);
167 void operator delete[](
void*, size_t);
172#ifdef V8_ENABLE_CHECKS
173 int scope_level_ = 0;
177 template <
typename T>
191 isolate_ = v8_isolate;
192 prev_next_ = current->
next;
193 prev_limit_ = current->
limit;
195#ifdef V8_ENABLE_CHECKS
196 DoInitializeAsserts(v8_isolate);
197 scope_level_ = current->
level;
203#ifdef V8_ENABLE_CHECKS
204 AssertScopeLevelsMatch();
210 std::swap(current->
next, prev_next_);
214 current->
limit = prev_limit_;
216 DeleteExtensions(isolate_);
218#ifdef V8_ENABLE_CHECKS
219 DoCloseScopeAsserts(handle_count_before, limit, current);
231 result = Extend(v8_isolate);
247#ifdef V8_ENABLE_DIRECT_HANDLE
250class LocalBase :
public api_internal::DirectHandleBase {
258#ifdef V8_ENABLE_CHECKS
259 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
263 template <
typename S>
267 return LocalBase<T>(
value);
270 V8_INLINE static LocalBase<T>
New(Isolate* isolate, T* that) {
276 if (
slot ==
nullptr)
return LocalBase<T>();
277 return LocalBase<T>(*
slot);
282 return LocalBase<T>(
repr);
298#ifdef V8_ENABLE_CHECKS
299 if (!
IsEmpty()) api_internal::TypeCheckLocal<T>(value<Value>());
303 template <
typename S>
359#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
377 requires std::is_base_of_v<T, S>
397 return internal::HandleHelper::EqualHandles(*
this, that);
402 return internal::HandleHelper::EqualHandles(*
this, that);
407 return !operator==(that);
412 return !operator==(that);
422#ifdef V8_ENABLE_CHECKS
426 T::Cast(that.template value<S>());
447 return New(isolate, that.template value<T, true>());
452 return New(isolate, that.template value<T, true>());
457 return New(isolate, that.template value<T, true>());
471 template <
class F,
class M>
491 template <
class F1,
class F2,
class F3>
497 friend class internal::SamplingHeapProfiler;
499 friend class debug::ConsoleCallArguments;
502 explicit Local(no_checking_tag do_not_check)
503 :
LocalBase<T>(), StackAllocated(do_not_check) {}
504 explicit Local(
const Local<T>& other, no_checking_tag do_not_check)
505 :
LocalBase<T>(other), StackAllocated(do_not_check) {}
507 V8_INLINE explicit Local(
const LocalBase<T>& other) : LocalBase<T>(other) {}
510 internal::ValueHelper::InternalRepresentationType repr) {
511 return Local<T>(LocalBase<T>::FromRepr(repr));
514 V8_INLINE static Local<T> FromSlot(internal::Address* slot) {
515 return Local<T>(LocalBase<T>::FromSlot(slot));
518#ifdef V8_ENABLE_DIRECT_HANDLE
519 friend class TypecheckWitness;
521 V8_INLINE static Local<T> FromAddress(internal::Address ptr) {
522 return Local<T>(LocalBase<T>(ptr));
526 V8_INLINE static Local<T> New(Isolate* isolate, internal::Address value) {
527 return Local<T>(LocalBase<T>::New(isolate, value));
530 V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
531 return Local<T>(LocalBase<T>::New(isolate, that));
537 return Local<S>(LocalBase<S>(*
this));
549#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
555 LocalUnchecked& operator=(
const LocalUnchecked&)
noexcept =
default;
563#ifdef V8_ENABLE_DIRECT_HANDLE
568class StrongRootAllocator<LocalUnchecked<T>> :
public StrongRootAllocatorBase {
570 using value_type = LocalUnchecked<T>;
571 static_assert(std::is_standard_layout_v<value_type>);
572 static_assert(
sizeof(value_type) ==
sizeof(Address));
574 template <
typename HeapOrIsolateT>
575 explicit StrongRootAllocator(HeapOrIsolateT* heap_or_isolate)
576 : StrongRootAllocatorBase(heap_or_isolate) {}
577 template <
typename U>
578 StrongRootAllocator(
const StrongRootAllocator<U>& other) noexcept
579 : StrongRootAllocatorBase(other) {}
581 value_type* allocate(
size_t n) {
582 return reinterpret_cast<value_type*
>(allocate_impl(n));
584 void deallocate(value_type* p,
size_t n)
noexcept {
585 return deallocate_impl(
reinterpret_cast<Address*
>(p), n);
596#ifdef V8_ENABLE_DIRECT_HANDLE
599 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
600 return allocator_type(isolate);
603 using allocator_type = std::allocator<element_type>;
605 static allocator_type make_allocator(
Isolate* isolate)
noexcept {
606 return allocator_type();
610 using vector_type = std::vector<element_type, allocator_type>;
626 : backing_(n, make_allocator(isolate)) {}
628 : backing_(make_allocator(isolate)) {
629 if (init.size() == 0)
return;
630 backing_.reserve(init.size());
631 backing_.insert(backing_.end(), init.begin(), init.end());
641 size_t size() const noexcept {
return backing_.size(); }
642 bool empty() const noexcept {
return backing_.empty(); }
643 void reserve(
size_t n) { backing_.reserve(n); }
650 const Local<T>&
at(
size_t n)
const {
return backing_.at(n); }
664 template <
typename InputIt>
666 return iterator(backing_.insert(pos.
base(), first, last));
670 return iterator(backing_.insert(pos.
base(), init.begin(), init.end()));
675 backing_.reserve(init.size());
676 backing_.insert(backing_.end(), init.begin(), init.end());
683 template <
typename... Args>
685 backing_.push_back(
value_type{std::forward<Args>(args)...});
688 void clear() noexcept { backing_.clear(); }
689 void resize(
size_t n) { backing_.resize(n); }
693 return x.backing_ == y.backing_;
696 return x.backing_ != y.backing_;
699 return x.backing_ < y.backing_;
702 return x.backing_ > y.backing_;
705 return x.backing_ <= y.backing_;
708 return x.backing_ >= y.backing_;
712 vector_type backing_;
715#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
742 requires std::is_base_of_v<T, S>
748 requires std::is_base_of_v<T, S>
804 template <
typename S>
819 void*
operator new(
size_t size) =
delete;
820 void*
operator new[](
size_t size) =
delete;
821 void operator delete(
void*, size_t) =
delete;
822 void operator delete[](
void*, size_t) =
delete;
843#ifdef V8_ENABLE_DIRECT_HANDLE
846 if (value.
IsEmpty())
return value;
869 void*
operator new(
size_t size) =
delete;
870 void*
operator new[](
size_t size) =
delete;
871 void operator delete(
void*, size_t) =
delete;
872 void operator delete[](
void*, size_t) =
delete;
875 internal::Isolate*
const i_isolate_;
877 int prev_sealed_level_;
Definition: v8-traced-handle.h:124
Definition: v8-context.h:48
Definition: v8-local-handle.h:812
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:836
~EscapableHandleScope()=default
Local< T > Escape(Local< T > value)
Definition: v8-local-handle.h:842
MaybeLocal< T > EscapeMaybe(MaybeLocal< T > value)
Definition: v8-local-handle.h:852
EscapableHandleScope(Isolate *isolate)
Definition: v8-local-handle.h:838
Definition: v8-local-handle.h:119
void operator=(const HandleScope &)=delete
void Initialize(Isolate *isolate)
Definition: v8-local-handle.h:188
HandleScope(const HandleScope &)=delete
Isolate * GetIsolate() const
Definition: v8-local-handle.h:130
static int NumberOfHandles(Isolate *isolate)
~HandleScope()
Definition: v8-local-handle.h:201
static internal::Address * CreateHandle(Isolate *i_isolate, internal::Address value)
Definition: v8-local-handle.h:225
static internal::Address * CreateHandleForCurrentIsolate(internal::Address value)
Definition: v8-isolate.h:290
Definition: v8-local-handle.h:289
static LocalBase< T > New(Isolate *isolate, T *that)
Definition: v8-local-handle.h:310
static LocalBase< T > FromSlot(internal::Address *slot)
Definition: v8-local-handle.h:316
LocalBase(const LocalBase< S > &other)
Definition: v8-local-handle.h:304
static LocalBase< T > FromRepr(internal::ValueHelper::InternalRepresentationType repr)
Definition: v8-local-handle.h:320
LocalBase(internal::Address *location)
Definition: v8-local-handle.h:296
static LocalBase< T > New(Isolate *isolate, internal::Address value)
Definition: v8-local-handle.h:306
Definition: v8-local-handle.h:592
LocalVector< T > & operator=(std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:673
const Local< T > & front() const
Definition: v8-local-handle.h:653
friend bool operator>=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:707
bool empty() const noexcept
Definition: v8-local-handle.h:642
const Local< T > * data() const noexcept
Definition: v8-local-handle.h:658
const_iterator end() const noexcept
Definition: v8-local-handle.h:639
const Local< T > & operator[](size_t n) const
Definition: v8-local-handle.h:647
friend bool operator<(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:698
const Local< T > & back() const
Definition: v8-local-handle.h:655
void shrink_to_fit()
Definition: v8-local-handle.h:644
Local< T > * data() noexcept
Definition: v8-local-handle.h:657
void reserve(size_t n)
Definition: v8-local-handle.h:643
Local< T > & front()
Definition: v8-local-handle.h:652
internal::WrappedIterator< typename vector_type::iterator, Local< T > > iterator
Definition: v8-local-handle.h:619
friend bool operator<=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:704
friend bool operator!=(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:695
Local< T > & operator[](size_t n)
Definition: v8-local-handle.h:646
size_t size_type
Definition: v8-local-handle.h:616
void emplace_back(Args &&... args)
Definition: v8-local-handle.h:684
const_iterator begin() const noexcept
Definition: v8-local-handle.h:635
iterator insert(const_iterator pos, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:669
Local< T > & at(size_t n)
Definition: v8-local-handle.h:649
ptrdiff_t difference_type
Definition: v8-local-handle.h:617
iterator insert(const_iterator pos, const Local< T > &value)
Definition: v8-local-handle.h:660
internal::WrappedIterator< typename vector_type::const_iterator, const Local< T > > const_iterator
Definition: v8-local-handle.h:622
void pop_back()
Definition: v8-local-handle.h:681
LocalVector(Isolate *isolate, std::initializer_list< Local< T > > init)
Definition: v8-local-handle.h:627
iterator end() noexcept
Definition: v8-local-handle.h:638
const Local< T > & at(size_t n) const
Definition: v8-local-handle.h:650
void clear() noexcept
Definition: v8-local-handle.h:688
void swap(LocalVector< T > &other)
Definition: v8-local-handle.h:690
iterator insert(const_iterator pos, InputIt first, InputIt last)
Definition: v8-local-handle.h:665
friend bool operator==(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:692
LocalVector(Isolate *isolate)
Definition: v8-local-handle.h:624
size_t size() const noexcept
Definition: v8-local-handle.h:641
void resize(size_t n)
Definition: v8-local-handle.h:689
Local< T > & back()
Definition: v8-local-handle.h:654
LocalVector(Isolate *isolate, size_t n)
Definition: v8-local-handle.h:625
void push_back(const Local< T > &x)
Definition: v8-local-handle.h:680
iterator begin() noexcept
Definition: v8-local-handle.h:634
friend bool operator>(const LocalVector< T > &x, const LocalVector< T > &y)
Definition: v8-local-handle.h:701
Definition: v8-local-handle.h:364
friend class TracedReferenceBase
Definition: v8-local-handle.h:461
friend class Object
Definition: v8-local-handle.h:478
friend class PersistentValueMapBase
Definition: v8-local-handle.h:492
static Local< T > New(Isolate *isolate, const PersistentBase< T > &that)
Definition: v8-local-handle.h:450
friend class PropertyCallbackInfo
Definition: v8-local-handle.h:476
friend class ReturnValue
Definition: v8-local-handle.h:494
friend class HandleScope
Definition: v8-local-handle.h:488
friend class Traced
Definition: v8-local-handle.h:496
T * operator*() const
Definition: v8-local-handle.h:382
static Local< T > Cast(Local< S > that)
Definition: v8-local-handle.h:421
friend class FunctionCallbackInfo
Definition: v8-local-handle.h:474
Local< S > As() const
Definition: v8-local-handle.h:437
friend class String
Definition: v8-local-handle.h:477
bool operator!=(const Local< S > &that) const
Definition: v8-local-handle.h:406
friend class MaybeLocal
Definition: v8-local-handle.h:470
static Local< T > New(Isolate *isolate, const BasicTracedReference< T > &that)
Definition: v8-local-handle.h:455
bool operator==(const PersistentBase< S > &that) const
Definition: v8-local-handle.h:401
bool operator!=(const Persistent< S > &that) const
Definition: v8-local-handle.h:411
friend class Isolate
Definition: v8-local-handle.h:480
friend class Utils
Definition: v8-local-handle.h:462
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:446
friend class Context
Definition: v8-local-handle.h:479
friend class Private
Definition: v8-local-handle.h:481
friend class Persistent
Definition: v8-local-handle.h:472
friend class Global
Definition: v8-local-handle.h:466
T * operator->() const
Definition: v8-local-handle.h:380
friend class EscapableHandleScope
Definition: v8-local-handle.h:489
friend class Eternal
Definition: v8-local-handle.h:464
bool operator==(const Local< S > &that) const
Definition: v8-local-handle.h:396
Local(Local< S > that)
Definition: v8-local-handle.h:378
friend class InternalEscapableScope
Definition: v8-local-handle.h:490
friend class Local
Definition: v8-local-handle.h:468
Definition: v8-local-handle.h:732
bool ToLocal(Local< S > *out) const
Definition: v8-local-handle.h:758
static MaybeLocal< T > Cast(MaybeLocal< S > that)
Definition: v8-local-handle.h:787
MaybeLocal< S > As() const
Definition: v8-local-handle.h:797
Local< T > ToLocalChecked()
Definition: v8-local-handle.h:767
Local< S > FromMaybe(Local< S > default_value) const
Definition: v8-local-handle.h:777
bool IsEmpty() const
Definition: v8-local-handle.h:751
MaybeLocal(Local< S > that)
Definition: v8-local-handle.h:743
MaybeLocal(MaybeLocal< S > that)
Definition: v8-local-handle.h:749
Definition: v8-object.h:235
Definition: v8-persistent-handle.h:93
Definition: v8-persistent-handle.h:250
Definition: v8-local-handle.h:862
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:545
LocalUnchecked()
Definition: v8-local-handle.h:547
LocalUnchecked(const Local< T > &other) noexcept
Definition: v8-local-handle.h:559
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