164 V8_INLINE void SetWeak(P* parameter,
165 typename WeakCallbackInfo::Callback callback,
166 WeakCallbackType type);
167
168 /**
169 * Turns this handle into a weak phantom handle without finalization callback.
170 * The handle will be reset automatically when the garbage collector detects
171 * that the object is no longer reachable.
172 * A related function Isolate::NumberOfPhantomHandleResetsSinceLastCall
173 * returns how many phantom handles were reset by the garbage collector.
174 */
175 V8_INLINE void SetWeak();
176
177 template
178 V8_INLINE P* ClearWeak();
179
180 // TODO(dcarney): remove this.
181 V8_INLINE void ClearWeak() { ClearWeak(); }
182
183 /**
184 * Annotates the strong handle with the given label, which is then used by the
185 * heap snapshot generator as a name of the edge from the root to the handle.
186 * The function does not take ownership of the label and assumes that the
187 * label is valid as long as the handle is valid.
188 */
189 V8_INLINE void AnnotateStrongRetainer(const char* label);
190
191 /** Returns true if the handle's reference is weak. */
192 V8_INLINE bool IsWeak() const;
193
194 /**
195 * Assigns a wrapper class ID to the handle.
196 */
197 V8_INLINE void SetWrapperClassId(uint16_t class_id);
198
199 /**
200 * Returns the class ID previously assigned to this handle or 0 if no class ID
201 * was previously assigned.
202 */
203 V8_INLINE uint16_t WrapperClassId() const;
204
205 PersistentBase(const PersistentBase& other) = delete;
206 void operator=(const PersistentBase&) = delete;
207
208 private:
209 friend class Isolate;
210 friend class Utils;
211 template
212 friend class Local;
213 template
214 friend class Persistent;
215 template
216 friend class Global;
217 template
218 friend class PersistentBase;
219 template
220 friend class ReturnValue;
221 template
222 friend class PersistentValueMapBase;
223 template
224 friend class PersistentValueVector;
225 friend class Object;
226
227 explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
228 V8_INLINE static T* New(Isolate* isolate, T* that);
229
230 T* val_;
231 };
232
233 /**
234 * Default traits for Persistent. This class does not allow
235 * use of the copy constructor or assignment operator.
236 * At present kResetInDestructor is not set, but that will change in a future
237 * version.
238 */
239 template
240 class NonCopyablePersistentTraits {
241 public:
242 using NonCopyablePersistent = Persistent>;
243 static const bool kResetInDestructor = false;
244 template
245 V8_INLINE static void Copy(const Persistent& source,
246 NonCopyablePersistent* dest) {
247 static_assert(sizeof(S) < 0,
248 "NonCopyablePersistentTraits::Copy is not instantiable");
249 }
250 };
251
252 /**
253 * Helper class traits to allow copying and assignment of Persistent.
254 * This will clone the contents of storage cell, but not any of the flags, etc.
255 */
256 template
257 struct CopyablePersistentTraits {
258 using CopyablePersistent = Persistent>;
259 static const bool kResetInDestructor = true;
260 template
261 static V8_INLINE void Copy(const Persistent& source,
262 CopyablePersistent* dest) {
263 // do nothing, just allow copy
264 }
265 };
266
267 /**
268 * A PersistentBase which allows copy and assignment.
269 *
270 * Copy, assignment and destructor behavior is controlled by the traits
271 * class M.
272 *
273 * Note: Persistent class hierarchy is subject to future changes.
274 */
275 template
276 class Persistent : public PersistentBase {
277 public:
278 /**
279 * A Persistent with no storage cell.
280 */
281 V8_INLINE Persistent() : PersistentBase(nullptr) {}
282 /**
283 * Construct a Persistent from a Local.
284 * When the Local is non-empty, a new storage cell is created
285 * pointing to the same object, and no flags are set.
286 */
287 template
288 V8_INLINE Persistent(Isolate* isolate, Local that)
289 : PersistentBase(PersistentBase::New(isolate, *that)) {
290 static_assert(std::is_base_of::value, "type check");
291 }
292 /**
293 * Construct a Persistent from a Persistent.
294 * When the Persistent is non-empty, a new storage cell is created
295 * pointing to the same object, and no flags are set.
296 */
297 template
298 V8_INLINE Persistent(Isolate* isolate, const Persistent& that)
299 : PersistentBase(PersistentBase::New(isolate, *that)) {
300 static_assert(std::is_base_of::value, "type check");
301 }
302 /**
303 * The copy constructors and assignment operator create a Persistent
304 * exactly as the Persistent constructor, but the Copy function from the
305 * traits class is called, allowing the setting of flags based on the
306 * copied Persistent.
307 */
308 V8_INLINE Persistent(const Persistent& that) : PersistentBase(nullptr) {
309 Copy(that);
310 }
311 template
312 V8_INLINE Persistent(const Persistent& that) : PersistentBase(0) {
313 Copy(that);
314 }
315 V8_INLINE Persistent& operator=(const Persistent& that) {
316 Copy(that);
317 return *this;
318 }
319 template
320 V8_INLINE Persistent& operator=(const Persistent& that) {
321 Copy(that);
322 return *this;
323 }
324 /**
325 * The destructor will dispose the Persistent based on the
326 * kResetInDestructor flags in the traits class. Since not calling dispose
327 * can result in a memory leak, it is recommended to always set this flag.
328 */
329 V8_INLINE ~Persistent() {
330 if (M::kResetInDestructor) this->Reset();
331 }
332
333 // TODO(dcarney): this is pretty useless, fix or remove
334 template
335 V8_INLINE static Persistent& Cast(const Persistent& that) {
336 #ifdef V8_ENABLE_CHECKS
337 // If we're going to perform the type check then we have to check
338 // that the handle isn't empty before doing the checked cast.
339 if (!that.IsEmpty()) T::Cast(*that);
340 #endif
341 return reinterpret_cast&>(const_cast&>(that));
342 }
343
344 // TODO(dcarney): this is pretty useless, fix or remove
345 template
346 V8_INLINE Persistent& As() const {
347 return Persistent::Cast(*this);
348 }
349
350 private:
351 friend class Isolate;
352 friend class Utils;
353 template
354 friend class Local;
355 template
356 friend class Persistent;
357 template
358 friend class ReturnValue;
359
360 explicit V8_INLINE Persistent(T* that) : PersistentBase(that) {}
361 V8_INLINE T* operator*() const { return this->val_; }
362 template
363 V8_INLINE void Copy(const Persistent& that);
364 };
365
366 /**
367 * A PersistentBase which has move semantics.
368 *
369 * Note: Persistent class hierarchy is subject to future changes.
370 */
371 template
372 class Global : public PersistentBase {
373 public:
374 /**
375 * A Global with no storage cell.
376 */
377 V8_INLINE Global() : PersistentBase(nullptr) {}
378
379 /**
380 * Construct a Global from a Local.
381 * When the Local is non-empty, a new storage cell is created
382 * pointing to the same object, and no flags are set.
383 */
384 template
385 V8_INLINE Global(Isolate* isolate, Local that)
386 : PersistentBase(PersistentBase::New(isolate, *that)) {
387 static_assert(std::is_base_of::value, "type check");
388 }
389
390 /**
391 * Construct a Global from a PersistentBase.
392 * When the Persistent is non-empty, a new storage cell is created
393 * pointing to the same object, and no flags are set.
394 */
395 template
396 V8_INLINE Global(Isolate* isolate, const PersistentBase& that)
397 : PersistentBase(PersistentBase::New(isolate, that.val_)) {
398 static_assert(std::is_base_of::value, "type check");
399 }
400
401 /**
402 * Move constructor.
403 */
404 V8_INLINE Global(Global&& other);
405
406 V8_INLINE ~Global() { this->Reset(); }
407
408 /**
409 * Move via assignment.
410 */
411 template
412 V8_INLINE Global& operator=(Global&& rhs);
413
414 /**
415 * Pass allows returning uniques from functions, etc.
416 */
417 Global Pass() { return static_cast(*this); }
418
419 /*
420 * For compatibility with Chromium's base::Bind (base::Passed).
421 */
422 using MoveOnlyTypeForCPP03 = void;
423
424 Global(const Global&) = delete;
425 void operator=(const Global&) = delete;
426
427 private:
428 template
429 friend class ReturnValue;
430 V8_INLINE T* operator*() const { return this->val_; }
431 };
432
433 // UniquePersistent is an alias for Global for historical reason.
434 template
435 using UniquePersistent = Global;
436
437 /**
438 * Interface for iterating through all the persistent handles in the heap.
439 */
440 class V8_EXPORT PersistentHandleVisitor {
441 public:
442 virtual ~PersistentHandleVisitor() = default;
443 virtual void VisitPersistentHandle(Persistent* value,
444 uint16_t class_id) {}
445 };
446
447 template
448 T* PersistentBase::New(Isolate* isolate, T* that) {
449 if (that == nullptr) return nullptr;
450 internal::Address* p = reinterpret_cast(that);
451 return reinterpret_cast(api_internal::GlobalizeReference(
452 reinterpret_cast(isolate), p));
453 }
454
455 template
456 template
457 void Persistent::Copy(const Persistent& that) {
458 static_assert(std::is_base_of::value, "type check");
459 this->Reset();
460 if (that.IsEmpty()) return;
461 internal::Address* p = reinterpret_cast(that.val_);
462 this->val_ = reinterpret_cast(api_internal::CopyGlobalReference(p));
463 M::Copy(that, this);
464 }
465
466 template
467 bool PersistentBase::IsWeak() const {
468 using I = internal::Internals;
469 if (this->IsEmpty()) return false;
470 return I::GetNodeState(reinterpret_cast(this->val_)) ==
471 I::kNodeStateIsWeakValue;
472 }
473
474 template
475 void PersistentBase::Reset() {
476 if (this->IsEmpty()) return;
477 api_internal::DisposeGlobal(reinterpret_cast(this->val_));
478 val_ = nullptr;
479 }
480
481 /**
482 * If non-empty, destroy the underlying storage cell
483 * and create a new one with the contents of other if other is non empty
484 */
485 template
486 template
487 void PersistentBase::Reset(Isolate* isolate, const Local& other) {
488 static_assert(std::is_base_of::value, "type check");
489 Reset();
490 if (other.IsEmpty()) return;
491 this->val_ = New(isolate, other.val_);
492 }
493
494 /**
495 * If non-empty, destroy the underlying storage cell
496 * and create a new one with the contents of other if other is non empty
497 */
498 template
499 template
500 void PersistentBase::Reset(Isolate* isolate,
501 const PersistentBase& other) {
502 static_assert(std::is_base_of::value, "type check");
503 Reset();
504 if (other.IsEmpty()) return;
505 this->val_ = New(isolate, other.val_);
506 }
507
508 template
509 template
510 V8_INLINE void PersistentBase::SetWeak(
511 P* parameter, typename WeakCallbackInfo::Callback callback,
512 WeakCallbackType type) {
513 using Callback = WeakCallbackInfo::Callback;
514 #if (__GNUC__ >= 8) && !defined(__clang__)
515 #pragma GCC diagnostic push
516 #pragma GCC diagnostic ignored "-Wcast-function-type"
517 #endif
518 api_internal::MakeWeak(reinterpret_cast(this->val_),
519 parameter, reinterpret_cast(callback), type);
520 #if (__GNUC__ >= 8) && !defined(__clang__)
521 #pragma GCC diagnostic pop
522 #endif
523 }
524
525 template
526 void PersistentBase::SetWeak() {
527 api_internal::MakeWeak(reinterpret_cast(&this->val_));
528 }
529
530 template
531 template
532 P* PersistentBase::ClearWeak() {
533 return reinterpret_cast(api_internal::ClearWeak(
534 reinterpret_cast(this->val_)));
535 }
536
537 template
538 void PersistentBase::AnnotateStrongRetainer(const char* label) {
539 api_internal::AnnotateStrongRetainer(
540 reinterpret_cast(this->val_), label);
541 }
542
543 template
544 void PersistentBase::SetWrapperClassId(uint16_t class_id) {
545 using I = internal::Internals;
546 if (this->IsEmpty()) return;
547 internal::Address* obj = reinterpret_cast(this->val_);
548 uint8_t* addr = reinterpret_cast(obj) + I::kNodeClassIdOffset;
549 *reinterpret_cast(addr) = class_id;
550 }
551
552 template
553 uint16_t PersistentBase::WrapperClassId() const {
554 using I = internal::Internals;
555 if (this->IsEmpty()) return 0;
556 internal::Address* obj = reinterpret_cast(this->val_);
557 uint8_t* addr = reinterpret_cast(obj) + I::kNodeClassIdOffset;
558 return *reinterpret_cast(addr);
559 }
560
561 template
562 Global::Global(Global&& other) : PersistentBase(other.val_) {
563 if (other.val_ != nullptr) {
564 api_internal::MoveGlobalReference(
565 reinterpret_cast(&other.val_),
566 reinterpret_cast(&this->val_));
567 other.val_ = nullptr;
568 }
569 }
570
571 template
572 template
573 Global& Global::operator=(Global&& rhs) {
574 static_assert(std::is_base_of::value, "type check");
575 if (this != &rhs) {
576 this->Reset();
577 if (rhs.val_ != nullptr) {
578 this->val_ = rhs.val_;
579 api_internal::MoveGlobalReference(
580 reinterpret_cast(&rhs.val_),
581 reinterpret_cast(&this->val_));
582 rhs.val_ = nullptr;
583 }
584 }
585 return *this;
586 }
587
588 } // namespace v8
589
590 #endif // INCLUDE_V8_PERSISTENT_HANDLE_H_