Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/node/v8-traced-handle.h
$ cat -n /usr/include/node/v8-traced-handle.h 1 // Copyright 2021 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef INCLUDE_V8_TRACED_HANDLE_H_ 6 #define INCLUDE_V8_TRACED_HANDLE_H_ 7 8 #include
9 #include
10 #include
11 12 #include
13 #include
14 #include
15 #include
16 17 #include "v8-internal.h" // NOLINT(build/include_directory) 18 #include "v8-local-handle.h" // NOLINT(build/include_directory) 19 #include "v8-weak-callback-info.h" // NOLINT(build/include_directory) 20 #include "v8config.h" // NOLINT(build/include_directory) 21 22 namespace v8 { 23 24 class Value; 25 26 namespace internal { 27 28 class BasicTracedReferenceExtractor; 29 30 enum class TracedReferenceStoreMode { 31 kInitializingStore, 32 kAssigningStore, 33 }; 34 35 enum class TracedReferenceHandling { 36 kDefault, // See EmbedderRootsHandler::IsRoot(). 37 kDroppable 38 }; 39 40 V8_EXPORT internal::Address* GlobalizeTracedReference( 41 internal::Isolate* isolate, internal::Address value, 42 internal::Address* slot, TracedReferenceStoreMode store_mode, 43 internal::TracedReferenceHandling reference_handling); 44 V8_EXPORT void MoveTracedReference(internal::Address** from, 45 internal::Address** to); 46 V8_EXPORT void CopyTracedReference(const internal::Address* const* from, 47 internal::Address** to); 48 V8_EXPORT void DisposeTracedReference(internal::Address* global_handle); 49 50 } // namespace internal 51 52 /** 53 * An indirect handle, where the indirect pointer points to a GlobalHandles 54 * node. 55 */ 56 class TracedReferenceBase : public api_internal::IndirectHandleBase { 57 public: 58 /** 59 * If non-empty, destroy the underlying storage cell. |IsEmpty| will return 60 * true after this call. 61 */ 62 V8_INLINE void Reset(); 63 64 /** 65 * Construct a Local
from this handle. 66 */ 67 V8_INLINE Local
Get(Isolate* isolate) const { 68 if (IsEmpty()) return Local
(); 69 return Local
::New(isolate, this->value
()); 70 } 71 72 /** 73 * Returns true if this TracedReference is empty, i.e., has not been 74 * assigned an object. This version of IsEmpty is thread-safe. 75 */ 76 bool IsEmptyThreadSafe() const { 77 return this->GetSlotThreadSafe() == nullptr; 78 } 79 80 protected: 81 V8_INLINE TracedReferenceBase() = default; 82 83 /** 84 * Update this reference in a thread-safe way. 85 */ 86 void SetSlotThreadSafe(void* new_val) { 87 reinterpret_cast
*>(&slot())->store( 88 new_val, std::memory_order_relaxed); 89 } 90 91 /** 92 * Get this reference in a thread-safe way 93 */ 94 const void* GetSlotThreadSafe() const { 95 return reinterpret_cast
const*>(&slot())->load( 96 std::memory_order_relaxed); 97 } 98 99 V8_EXPORT void CheckValue() const; 100 101 friend class internal::BasicTracedReferenceExtractor; 102 template
103 friend class Local; 104 template
105 friend bool operator==(const TracedReferenceBase&, const Local
&); 106 friend bool operator==(const TracedReferenceBase&, 107 const TracedReferenceBase&); 108 }; 109 110 /** 111 * A traced handle with copy and move semantics. The handle is to be used 112 * together as part of GarbageCollected objects (see v8-cppgc.h) or from stack 113 * and specifies edges from C++ objects to JavaScript. 114 * 115 * The exact semantics are: 116 * - Tracing garbage collections using CppHeap. 117 * - Non-tracing garbage collections refer to 118 * |v8::EmbedderRootsHandler::IsRoot()| whether the handle should 119 * be treated as root or not. 120 * 121 * Note that the base class cannot be instantiated itself, use |TracedReference| 122 * instead. 123 */ 124 template
125 class BasicTracedReference : public TracedReferenceBase { 126 public: 127 /** 128 * Construct a Local
from this handle. 129 */ 130 Local
Get(Isolate* isolate) const { return Local
::New(isolate, *this); } 131 132 template
133 V8_INLINE BasicTracedReference
& As() const { 134 return reinterpret_cast
&>( 135 const_cast
&>(*this)); 136 } 137 138 V8_DEPRECATE_SOON("Use Get to convert to Local instead") 139 V8_INLINE T* operator->() const { 140 #ifdef V8_ENABLE_CHECKS 141 CheckValue(); 142 #endif // V8_ENABLE_CHECKS 143 return this->template value
(); 144 } 145 146 V8_DEPRECATE_SOON("Use Get to convert to Local instead") 147 V8_INLINE T* operator*() const { return this->operator->(); } 148 149 private: 150 /** 151 * An empty BasicTracedReference without storage cell. 152 */ 153 BasicTracedReference() = default; 154 155 V8_INLINE static internal::Address* NewFromNonEmptyValue( 156 Isolate* isolate, T* that, internal::Address** slot, 157 internal::TracedReferenceStoreMode store_mode, 158 internal::TracedReferenceHandling reference_handling); 159 160 template
161 friend class Local; 162 friend class Object; 163 template
164 friend class TracedReference; 165 template
166 friend class BasicTracedReference; 167 template
168 friend class ReturnValue; 169 }; 170 171 /** 172 * A traced handle without destructor that clears the handle. The embedder needs 173 * to ensure that the handle is not accessed once the V8 object has been 174 * reclaimed. For more details see BasicTracedReference. 175 */ 176 template
177 class TracedReference : public BasicTracedReference
{ 178 public: 179 struct IsDroppable {}; 180 181 using BasicTracedReference
::Reset; 182 183 /** 184 * An empty TracedReference without storage cell. 185 */ 186 V8_INLINE TracedReference() = default; 187 188 /** 189 * Construct a TracedReference from a Local. 190 * 191 * When the Local is non-empty, a new storage cell is created 192 * pointing to the same object. 193 */ 194 template
195 TracedReference(Isolate* isolate, Local
that) : BasicTracedReference
() { 196 static_assert(std::is_base_of
::value, "type check"); 197 if (V8_UNLIKELY(that.IsEmpty())) { 198 return; 199 } 200 this->slot() = this->NewFromNonEmptyValue( 201 isolate, *that, &this->slot(), 202 internal::TracedReferenceStoreMode::kInitializingStore, 203 internal::TracedReferenceHandling::kDefault); 204 } 205 206 /** 207 * Construct a droppable TracedReference from a Local. Droppable means that V8 208 * is free to reclaim the pointee if it is unmodified and otherwise 209 * unreachable 210 * 211 * When the Local is non-empty, a new storage cell is created 212 * pointing to the same object. 213 */ 214 template
215 TracedReference(Isolate* isolate, Local
that, IsDroppable) 216 : BasicTracedReference
() { 217 static_assert(std::is_base_of
::value, "type check"); 218 if (V8_UNLIKELY(that.IsEmpty())) { 219 return; 220 } 221 this->slot() = this->NewFromNonEmptyValue( 222 isolate, *that, &this->slot(), 223 internal::TracedReferenceStoreMode::kInitializingStore, 224 internal::TracedReferenceHandling::kDroppable); 225 } 226 227 /** 228 * Move constructor initializing TracedReference from an 229 * existing one. 230 */ 231 V8_INLINE TracedReference(TracedReference&& other) noexcept { 232 // Forward to operator=. 233 *this = std::move(other); 234 } 235 236 /** 237 * Move constructor initializing TracedReference from an 238 * existing one. 239 */ 240 template
241 V8_INLINE TracedReference(TracedReference
&& other) noexcept { 242 // Forward to operator=. 243 *this = std::move(other); 244 } 245 246 /** 247 * Copy constructor initializing TracedReference from an 248 * existing one. 249 */ 250 V8_INLINE TracedReference(const TracedReference& other) { 251 // Forward to operator=; 252 *this = other; 253 } 254 255 /** 256 * Copy constructor initializing TracedReference from an 257 * existing one. 258 */ 259 template
260 V8_INLINE TracedReference(const TracedReference
& other) { 261 // Forward to operator=; 262 *this = other; 263 } 264 265 /** 266 * Move assignment operator initializing TracedReference from an existing one. 267 */ 268 V8_INLINE TracedReference& operator=(TracedReference&& rhs) noexcept; 269 270 /** 271 * Move assignment operator initializing TracedReference from an existing one. 272 */ 273 template
274 V8_INLINE TracedReference& operator=(TracedReference
&& rhs) noexcept; 275 276 /** 277 * Copy assignment operator initializing TracedReference from an existing one. 278 */ 279 V8_INLINE TracedReference& operator=(const TracedReference& rhs); 280 281 /** 282 * Copy assignment operator initializing TracedReference from an existing one. 283 */ 284 template
285 V8_INLINE TracedReference& operator=(const TracedReference
& rhs); 286 287 /** 288 * Always resets the reference. Creates a new reference from `other` if it is 289 * non-empty. 290 */ 291 template
292 V8_INLINE void Reset(Isolate* isolate, const Local
& other); 293 294 /** 295 * Always resets the reference. Creates a new reference from `other` if it is 296 * non-empty. The new reference is droppable, see constructor. 297 */ 298 template
299 V8_INLINE void Reset(Isolate* isolate, const Local
& other, IsDroppable); 300 301 template
302 V8_INLINE TracedReference
& As() const { 303 return reinterpret_cast
&>( 304 const_cast
&>(*this)); 305 } 306 }; 307 308 // --- Implementation --- 309 template
310 internal::Address* BasicTracedReference
::NewFromNonEmptyValue( 311 Isolate* isolate, T* that, internal::Address** slot, 312 internal::TracedReferenceStoreMode store_mode, 313 internal::TracedReferenceHandling reference_handling) { 314 return internal::GlobalizeTracedReference( 315 reinterpret_cast
(isolate), 316 internal::ValueHelper::ValueAsAddress(that), 317 reinterpret_cast
(slot), store_mode, 318 reference_handling); 319 } 320 321 void TracedReferenceBase::Reset() { 322 if (V8_UNLIKELY(IsEmpty())) { 323 return; 324 } 325 internal::DisposeTracedReference(slot()); 326 SetSlotThreadSafe(nullptr); 327 } 328 329 V8_INLINE bool operator==(const TracedReferenceBase& lhs, 330 const TracedReferenceBase& rhs) { 331 return internal::HandleHelper::EqualHandles(lhs, rhs); 332 } 333 334 template
335 V8_INLINE bool operator==(const TracedReferenceBase& lhs, 336 const v8::Local
& rhs) { 337 return internal::HandleHelper::EqualHandles(lhs, rhs); 338 } 339 340 template
341 V8_INLINE bool operator==(const v8::Local
& lhs, 342 const TracedReferenceBase& rhs) { 343 return rhs == lhs; 344 } 345 346 V8_INLINE bool operator!=(const TracedReferenceBase& lhs, 347 const TracedReferenceBase& rhs) { 348 return !(lhs == rhs); 349 } 350 351 template
352 V8_INLINE bool operator!=(const TracedReferenceBase& lhs, 353 const v8::Local
& rhs) { 354 return !(lhs == rhs); 355 } 356 357 template
358 V8_INLINE bool operator!=(const v8::Local
& lhs, 359 const TracedReferenceBase& rhs) { 360 return !(rhs == lhs); 361 } 362 363 template
364 template
365 void TracedReference
::Reset(Isolate* isolate, const Local
& other) { 366 static_assert(std::is_base_of
::value, "type check"); 367 this->Reset(); 368 if (V8_UNLIKELY(other.IsEmpty())) { 369 return; 370 } 371 this->SetSlotThreadSafe(this->NewFromNonEmptyValue( 372 isolate, *other, &this->slot(), 373 internal::TracedReferenceStoreMode::kAssigningStore, 374 internal::TracedReferenceHandling::kDefault)); 375 } 376 377 template
378 template
379 void TracedReference
::Reset(Isolate* isolate, const Local
& other, 380 IsDroppable) { 381 static_assert(std::is_base_of
::value, "type check"); 382 this->Reset(); 383 if (V8_UNLIKELY(other.IsEmpty())) { 384 return; 385 } 386 this->SetSlotThreadSafe(this->NewFromNonEmptyValue( 387 isolate, *other, &this->slot(), 388 internal::TracedReferenceStoreMode::kAssigningStore, 389 internal::TracedReferenceHandling::kDroppable)); 390 } 391 392 template
393 template
394 TracedReference
& TracedReference
::operator=( 395 TracedReference
&& rhs) noexcept { 396 static_assert(std::is_base_of
::value, "type check"); 397 *this = std::move(rhs.template As
()); 398 return *this; 399 } 400 401 template
402 template
403 TracedReference
& TracedReference
::operator=( 404 const TracedReference
& rhs) { 405 static_assert(std::is_base_of
::value, "type check"); 406 *this = rhs.template As
(); 407 return *this; 408 } 409 410 template
411 TracedReference
& TracedReference
::operator=( 412 TracedReference&& rhs) noexcept { 413 if (this != &rhs) { 414 internal::MoveTracedReference(&rhs.slot(), &this->slot()); 415 } 416 return *this; 417 } 418 419 template
420 TracedReference
& TracedReference
::operator=(const TracedReference& rhs) { 421 if (this != &rhs) { 422 this->Reset(); 423 if (!rhs.IsEmpty()) { 424 internal::CopyTracedReference(&rhs.slot(), &this->slot()); 425 } 426 } 427 return *this; 428 } 429 430 } // namespace v8 431 432 #endif // INCLUDE_V8_TRACED_HANDLE_H_
Contact us
|
About us
|
Term of use
|
Copyright © 2000-2025 MyWebUniversity.com ™