V8: include/v8-local-handle.h Source File (original) (raw)
1
2
3
4
5#ifndef INCLUDE_V8_LOCAL_HANDLE_H_
6#define INCLUDE_V8_LOCAL_HANDLE_H_
7
8#include <stddef.h>
9
10#include <type_traits>
11#include
12
14#include "v8-internal.h"
15
16namespace v8 {
17
18template
19class LocalBase;
20template
21class Local;
22template
23class LocalVector;
24template
25class MaybeLocal;
26
27template
28class Eternal;
29template
30class Global;
31
32template
33class NonCopyablePersistentTraits;
34template
35class PersistentBase;
36template <class T, class M = NonCopyablePersistentTraits>
38
39class TracedReferenceBase;
40template
41class BasicTracedReference;
42template
43class TracedReference;
44
45class ArrayBuffer;
46class Boolean;
47class Context;
48class EscapableHandleScope;
49template
50class FunctionCallbackInfo;
51class Isolate;
52class Object;
53template <class F1, class F2, class F3>
54class PersistentValueMapBase;
55class Primitive;
56class Private;
57template
58class PropertyCallbackInfo;
59template
60class ReturnValue;
61class String;
62template
65class Utils;
68
69namespace debug {
70class ConsoleCallArguments;
71}
72
73namespace internal {
74template
76template
78class SamplingHeapProfiler;
79}
80
81namespace api_internal {
82
84
85#ifdef V8_ENABLE_CHECKS
86template <typename T, typename V = Value>
87void TypeCheckLocal(V* value) {
88
89 if constexpr (requires { T::Cast(value); }) {
90
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;
98
99 T::Cast(value);
100 }
101}
102#endif
103}
104
120 public:
122
124
129
131
134
137
138 protected:
140
142
145
146 private:
147
149
150
151 void DeleteExtensions(Isolate* isolate);
152
153#ifdef V8_ENABLE_CHECKS
154
155 void DoInitializeAsserts(Isolate* isolate);
156
157 void AssertScopeLevelsMatch();
158
159
162#endif
163
164
165
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);
170
174#ifdef V8_ENABLE_CHECKS
175 int scope_level_ = 0;
176#endif
177
178
179 template
181
182
183
186};
187
189
193 isolate_ = v8_isolate;
194 prev_next_ = current->next;
195 prev_limit_ = current->limit;
196 current->level++;
197#ifdef V8_ENABLE_CHECKS
198 DoInitializeAsserts(v8_isolate);
199 scope_level_ = current->level;
200#endif
201}
202
204 if (V8_UNLIKELY(isolate_ == nullptr)) return;
205#ifdef V8_ENABLE_CHECKS
206 AssertScopeLevelsMatch();
208#endif
209
212 std::swap(current->next, prev_next_);
213 current->level--;
216 current->limit = prev_limit_;
217 limit = prev_limit_;
218 DeleteExtensions(isolate_);
219 }
220#ifdef V8_ENABLE_CHECKS
221 DoCloseScopeAsserts(handle_count_before, limit, current);
222#else
223 (void)limit;
224#endif
225}
226
233 result = Extend(v8_isolate);
234 }
235
236
239 *result = value;
240 return result;
241}
242
249#ifdef V8_ENABLE_DIRECT_HANDLE
250
251template
252class LocalBase : public api_internal::DirectHandleBase {
253 protected:
254 template
255 friend class Local;
256
258
260#ifdef V8_ENABLE_CHECKS
261 if (()) api_internal::TypeCheckLocal(value());
262#endif
263 }
264
265 template
266 V8_INLINE LocalBase(const LocalBase& other) : DirectHandleBase(other) {}
267
269 return LocalBase(value);
270 }
271
272 V8_INLINE static LocalBase New(Isolate* isolate, T* that) {
275 }
276
278 if (slot == nullptr) return LocalBase();
279 return LocalBase(*slot);
280 }
281
284 return LocalBase(repr);
285 }
286};
287
288#else
289
290template
292 protected:
293 template
295
297
300#ifdef V8_ENABLE_CHECKS
301 if (()) api_internal::TypeCheckLocal(value());
302#endif
303 }
304
305 template
307
310 }
311
316 }
317
320 }
321
325 }
326};
327
328#endif
329
359template
361#ifdef V8_ENABLE_LOCAL_OFF_STACK_CHECK
363#else
365#endif
366{
367 public:
372
378 template
379 requires std::is_base_of_v<T, S>
381
383
385
397 template
399 return internal::HandleHelper::EqualHandles(*this, that);
400 }
401
402 template
404 return internal::HandleHelper::EqualHandles(*this, that);
405 }
406
407 template
409 return !operator==(that);
410 }
411
412 template
414 return !operator==(that);
415 }
416
422 template
424#ifdef V8_ENABLE_CHECKS
425
426
428 T::Cast(that.template value());
429#endif
431 }
432
438 template
441 }
442
449 return New(isolate, that.template value<T, true>());
450 }
451
454 return New(isolate, that.template value<T, true>());
455 }
456
459 return New(isolate, that.template value<T, true>());
460 }
461
462 private:
465 template
467 template
469 template
471 template
473 template <class F, class M>
475 template
477 template
484 template
493 template <class F1, class F2, class F3>
495 template
497 template
499 friend class internal::SamplingHeapProfiler;
501 friend class debug::ConsoleCallArguments;
503
504 explicit Local(no_checking_tag do_not_check)
505 : LocalBase(), StackAllocated(do_not_check) {}
506 explicit Local(const Local& other, no_checking_tag do_not_check)
507 : LocalBase(other), StackAllocated(do_not_check) {}
508
509 V8_INLINE explicit Local(const LocalBase& other) : LocalBase(other) {}
510
511 V8_INLINE static Local FromRepr(
512 internal::ValueHelper::InternalRepresentationType repr) {
513 return Local(LocalBase::FromRepr(repr));
514 }
515
516 V8_INLINE static Local FromSlot(internal::Address* slot) {
517 return Local(LocalBase::FromSlot(slot));
518 }
519
520#ifdef V8_ENABLE_DIRECT_HANDLE
521 friend class TypecheckWitness;
522
523 V8_INLINE static Local FromAddress(internal::Address ptr) {
524 return Local(LocalBase(ptr));
525 }
526#endif
527
528 V8_INLINE static Local New(Isolate* isolate, internal::Address value) {
529 return Local(LocalBase::New(isolate, value));
530 }
531
532 V8_INLINE static Local New(Isolate* isolate, T* that) {
533 return Local(LocalBase::New(isolate, that));
534 }
535
536
537 template
538 V8_INLINE Local UnsafeAs() const {
539 return Local(LocalBase(*this));
540 }
541};
542
543namespace internal {
544
545
546template
548 public:
550
551#if defined(V8_ENABLE_LOCAL_OFF_STACK_CHECK) && V8_HAS_ATTRIBUTE_TRIVIAL_ABI
552
553
555 const LocalUnchecked& other) noexcept
557 LocalUnchecked& operator=(const LocalUnchecked&) noexcept = default;
558#endif
559
560
563};
564
565#ifdef V8_ENABLE_DIRECT_HANDLE
566
567
568
569template
570class StrongRootAllocator<LocalUnchecked> : public StrongRootAllocatorBase {
571 public:
572 using value_type = LocalUnchecked;
573 static_assert(std::is_standard_layout_v<value_type>);
574 static_assert(sizeof(value_type) == sizeof(Address));
575
576 template
577 explicit StrongRootAllocator(HeapOrIsolateT* heap_or_isolate)
578 : StrongRootAllocatorBase(heap_or_isolate) {}
579 template
580 StrongRootAllocator(const StrongRootAllocator& other) noexcept
581 : StrongRootAllocatorBase(other) {}
582
583 value_type* allocate(size_t n) {
584 return reinterpret_cast<value_type*>(allocate_impl(n));
585 }
586 void deallocate(value_type* p, size_t n) noexcept {
587 return deallocate_impl(reinterpret_cast<Address*>(p), n);
588 }
589};
590#endif
591}
592
593template
595 private:
597
598#ifdef V8_ENABLE_DIRECT_HANDLE
600
601 static allocator_type make_allocator(Isolate* isolate) noexcept {
602 return allocator_type(isolate);
603 }
604#else
605 using allocator_type = std::allocator<element_type>;
606
607 static allocator_type make_allocator(Isolate* isolate) noexcept {
608 return allocator_type();
609 }
610#endif
611
612 using vector_type = std::vector<element_type, allocator_type>;
613
614 public:
625
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());
634 }
635
639 }
642
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); }
647
650
651 Local& at(size_t n) { return backing_.at(n); }
652 const Local& at(size_t n) const { return backing_.at(n); }
653
658
660 const Local* data() const noexcept { return backing_.data(); }
661
663 return iterator(backing_.insert(pos.base(), value));
664 }
665
666 template
668 return iterator(backing_.insert(pos.base(), first, last));
669 }
670
672 return iterator(backing_.insert(pos.base(), init.begin(), init.end()));
673 }
674
676 backing_.clear();
677 backing_.reserve(init.size());
678 backing_.insert(backing_.end(), init.begin(), init.end());
679 return *this;
680 }
681
684
685 template <typename... Args>
687 backing_.push_back(value_type{std::forward(args)...});
688 }
689
690 void clear() noexcept { backing_.clear(); }
691 void resize(size_t n) { backing_.resize(n); }
693
695 return x.backing_ == y.backing_;
696 }
698 return x.backing_ != y.backing_;
699 }
701 return x.backing_ < y.backing_;
702 }
704 return x.backing_ > y.backing_;
705 }
707 return x.backing_ <= y.backing_;
708 }
710 return x.backing_ >= y.backing_;
711 }
712
713 private:
714 vector_type backing_;
715};
716
717#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
718
719template
721#endif
722
733template
735 public:
743 template
744 requires std::is_base_of_v<T, S>
749 template
750 requires std::is_base_of_v<T, S>
752
754
759 template
761 *out = local_;
763 }
764
771 return local_;
772 }
773
778 template
781 }
782
788 template
791 }
792
798 template
801 }
802
803 private:
805
806 template
808};
809
815 public:
818
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;
825
826 protected:
832
833 private:
835};
836
839 public:
843 template
845#ifdef V8_ENABLE_DIRECT_HANDLE
846 return value;
847#else
848 if (value.IsEmpty()) return value;
850#endif
851 }
852
853 template
856 }
857};
858
865 public:
868
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;
875
876 private:
877 internal::Isolate* const i_isolate_;
879 int prev_sealed_level_;
880};
881
882}
883
884#endif
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:291
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:262
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:868
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:1763
Definition: v8-internal.h:885
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:1499
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1737
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1691
static bool IsEmpty(T *value)
Definition: v8-internal.h:1696
Definition: v8-internal.h:1542
constexpr const Iterator & base() const noexcept
Definition: v8-internal.h:1668
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:1026
Local< Primitive > Undefined(Isolate *isolate)
Definition: v8-primitive.h:1018
Local< Boolean > False(Isolate *isolate)
Definition: v8-primitive.h:1042
Local< Boolean > True(Isolate *isolate)
Definition: v8-primitive.h:1034
Definition: v8-internal.h:863
Address * next
Definition: v8-internal.h:867
int level
Definition: v8-internal.h:869
Address * limit
Definition: v8-internal.h:868
#define V8_EXPORT
Definition: v8config.h:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_WARN_UNUSED_RESULT
Definition: v8config.h:679
#define V8_UNLIKELY(condition)
Definition: v8config.h:668
#define V8_TRIVIAL_ABI
Definition: v8config.h:808
#define V8_NODISCARD
Definition: v8config.h:701