V8_INLINE void SetWeak(P* parameter,
typename WeakCallbackInfo::Callback callback,
WeakCallbackType type);
/**
* Turns this handle into a weak phantom handle without finalization callback.
* The handle will be reset automatically when the garbage collector detects
* that the object is no longer reachable.
*/
V8_INLINE void SetWeak();
template
V8_INLINE P* ClearWeak();
// TODO(dcarney): remove this.
V8_INLINE void ClearWeak() { ClearWeak(); }
/**
* Annotates the strong handle with the given label, which is then used by the
* heap snapshot generator as a name of the edge from the root to the handle.
* The function does not take ownership of the label and assumes that the
* label is valid as long as the handle is valid.
*/
V8_INLINE void AnnotateStrongRetainer(const char* label);
/** Returns true if the handle's reference is weak. */
V8_INLINE bool IsWeak() const;
/**
* Assigns a wrapper class ID to the handle.
*/
V8_INLINE void SetWrapperClassId(uint16_t class_id);
/**
* Returns the class ID previously assigned to this handle or 0 if no class ID
* was previously assigned.
*/
V8_INLINE uint16_t WrapperClassId() const;
PersistentBase(const PersistentBase& other) = delete;
void operator=(const PersistentBase&) = delete;
private:
friend class Isolate;
friend class Utils;
template
friend class Local;
template
friend class Persistent;
template
friend class Global;
template
friend class PersistentBase;
template
friend class ReturnValue;
template
friend class PersistentValueMapBase;
friend class Object;
friend class internal::ValueHelper;
V8_INLINE PersistentBase() = default;
V8_INLINE explicit PersistentBase(internal::Address* location)
: IndirectHandleBase(location) {}
V8_INLINE static internal::Address* New(Isolate* isolate, T* that);
};
/**
* Default traits for Persistent. This class does not allow
* use of the copy constructor or assignment operator.
* At present kResetInDestructor is not set, but that will change in a future
* version.
*/
template
class NonCopyablePersistentTraits {
public:
using NonCopyablePersistent = Persistent>;
static const bool kResetInDestructor = false;
template
V8_INLINE static void Copy(const Persistent& source,
NonCopyablePersistent* dest) {
static_assert(sizeof(S) < 0,
"NonCopyablePersistentTraits::Copy is not instantiable");
}
};
/**
* A PersistentBase which allows copy and assignment.
*
* Copy, assignment and destructor behavior is controlled by the traits
* class M.
*
* CAVEAT: Persistent objects do not have proper destruction behavior by default
* and as such will leak the object without explicit clear. Consider using
* `v8::Global` instead which has proper destruction and move semantics.
*/
template
class Persistent : public PersistentBase {
public:
/**
* A Persistent with no storage cell.
*/
V8_INLINE Persistent() = default;
/**
* Construct a Persistent from a Local.
* When the Local is non-empty, a new storage cell is created
* pointing to the same object, and no flags are set.
*/
template
V8_INLINE Persistent(Isolate* isolate, Local that)
: PersistentBase(
PersistentBase::New(isolate, that.template value())) {
static_assert(std::is_base_of::value, "type check");
}
/**
* Construct a Persistent from a Persistent.
* When the Persistent is non-empty, a new storage cell is created
* pointing to the same object, and no flags are set.
*/
template
V8_INLINE Persistent(Isolate* isolate, const Persistent& that)
: PersistentBase(
PersistentBase::New(isolate, that.template value())) {
static_assert(std::is_base_of::value, "type check");
}
/**
* The copy constructors and assignment operator create a Persistent
* exactly as the Persistent constructor, but the Copy function from the
* traits class is called, allowing the setting of flags based on the
* copied Persistent.
*/
V8_INLINE Persistent(const Persistent& that) : PersistentBase() {
Copy(that);
}
template
V8_INLINE Persistent(const Persistent& that) : PersistentBase() {
Copy(that);
}
V8_INLINE Persistent& operator=(const Persistent& that) {
Copy(that);
return *this;
}
template
V8_INLINE Persistent& operator=(const Persistent& that) {
Copy(that);
return *this;
}
/**
* The destructor will dispose the Persistent based on the
* kResetInDestructor flags in the traits class. Since not calling dispose
* can result in a memory leak, it is recommended to always set this flag.
*/
V8_INLINE ~Persistent() {
if (M::kResetInDestructor) this->Reset();
}
// TODO(dcarney): this is pretty useless, fix or remove
template
V8_INLINE static Persistent& Cast(const Persistent& that) {
#ifdef V8_ENABLE_CHECKS
// If we're going to perform the type check then we have to check
// that the handle isn't empty before doing the checked cast.
if (!that.IsEmpty()) T::Cast(that.template value());
#endif
return reinterpret_cast&>(
const_cast&>(that));
}
// TODO(dcarney): this is pretty useless, fix or remove
template
V8_INLINE Persistent& As() const {
return Persistent::Cast(*this);
}
private:
friend class Isolate;
friend class Utils;
template
friend class Local;
template
friend class Persistent;
template
friend class ReturnValue;
template
V8_INLINE void Copy(const Persistent& that);
};
/**
* A PersistentBase which has move semantics.
*
* Note: Persistent class hierarchy is subject to future changes.
*/
template
class Global : public PersistentBase {
public:
/**
* A Global with no storage cell.
*/
V8_INLINE Global() = default;
/**
* Construct a Global from a Local.
* When the Local is non-empty, a new storage cell is created
* pointing to the same object, and no flags are set.
*/
template
V8_INLINE Global(Isolate* isolate, Local that)
: PersistentBase(
PersistentBase::New(isolate, that.template value())) {
static_assert(std::is_base_of::value, "type check");
}
/**
* Construct a Global from a PersistentBase.
* When the Persistent is non-empty, a new storage cell is created
* pointing to the same object, and no flags are set.
*/
template
V8_INLINE Global(Isolate* isolate, const PersistentBase& that)
: PersistentBase(
PersistentBase::New(isolate, that.template value())) {
static_assert(std::is_base_of::value, "type check");
}
/**
* Move constructor.
*/
V8_INLINE Global(Global&& other);
V8_INLINE ~Global() { this->Reset(); }
/**
* Move via assignment.
*/
template
V8_INLINE Global& operator=(Global&& rhs);
/**
* Pass allows returning uniques from functions, etc.
*/
Global Pass() { return static_cast(*this); }
/*
* For compatibility with Chromium's base::Bind (base::Passed).
*/
using MoveOnlyTypeForCPP03 = void;
Global(const Global&) = delete;
void operator=(const Global&) = delete;
private:
template
friend class ReturnValue;
};
// UniquePersistent is an alias for Global for historical reason.
template
using UniquePersistent = Global;
/**
* Interface for iterating through all the persistent handles in the heap.
*/
class V8_EXPORT PersistentHandleVisitor {
public:
virtual ~PersistentHandleVisitor() = default;
virtual void VisitPersistentHandle(Persistent* value,
uint16_t class_id) {}
};
template
internal::Address* PersistentBase::New(Isolate* isolate, T* that) {
if (internal::ValueHelper::IsEmpty(that)) return nullptr;
return api_internal::GlobalizeReference(
reinterpret_cast(isolate),
internal::ValueHelper::ValueAsAddress(that));
}
template
template
void Persistent::Copy(const Persistent& that) {
static_assert(std::is_base_of::value, "type check");
this->Reset();
if (that.IsEmpty()) return;
this->slot() = api_internal::CopyGlobalReference(that.slot());
M::Copy(that, this);
}
template
bool PersistentBase::IsWeak() const {
using I = internal::Internals;
if (this->IsEmpty()) return false;
return I::GetNodeState(this->slot()) == I::kNodeStateIsWeakValue;
}
template
void PersistentBase::Reset() {
if (this->IsEmpty()) return;
api_internal::DisposeGlobal(this->slot());
this->Clear();
}
/**
* If non-empty, destroy the underlying storage cell
* and create a new one with the contents of other if other is non empty
*/
template
template
void PersistentBase::Reset(Isolate* isolate, const Local& other) {
static_assert(std::is_base_of::value, "type check");
Reset();
if (other.IsEmpty()) return;
this->slot() = New(isolate, *other);
}
/**
* If non-empty, destroy the underlying storage cell
* and create a new one with the contents of other if other is non empty
*/
template
template
void PersistentBase::Reset(Isolate* isolate,
const PersistentBase& other) {
static_assert(std::is_base_of::value, "type check");
Reset();
if (other.IsEmpty()) return;
this->slot() = New(isolate, other.template value());
}
template
template
V8_INLINE void PersistentBase::SetWeak(
P* parameter, typename WeakCallbackInfo::Callback callback,
WeakCallbackType type) {
using Callback = WeakCallbackInfo::Callback;
#if (__GNUC__ >= 8) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
api_internal::MakeWeak(this->slot(), parameter,
reinterpret_cast(callback), type);
#if (__GNUC__ >= 8) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}
template
void PersistentBase::SetWeak() {
api_internal::MakeWeak(&this->slot());
}
template
template
P* PersistentBase::ClearWeak() {
return reinterpret_cast(api_internal::ClearWeak(this->slot()));
}
template
void PersistentBase::AnnotateStrongRetainer(const char* label) {
api_internal::AnnotateStrongRetainer(this->slot(), label);
}
template
void PersistentBase::SetWrapperClassId(uint16_t class_id) {
using I = internal::Internals;
if (this->IsEmpty()) return;
uint8_t* addr = reinterpret_cast(slot()) + I::kNodeClassIdOffset;
*reinterpret_cast(addr) = class_id;
}
template
uint16_t PersistentBase::WrapperClassId() const {
using I = internal::Internals;
if (this->IsEmpty()) return 0;
uint8_t* addr = reinterpret_cast(slot()) + I::kNodeClassIdOffset;
return *reinterpret_cast(addr);
}
template
Global::Global(Global&& other) : PersistentBase(other.slot()) {
if (!other.IsEmpty()) {
api_internal::MoveGlobalReference(&other.slot(), &this->slot());
other.Clear();
}
}
template
template
Global& Global::operator=(Global&& rhs) {
static_assert(std::is_base_of::value, "type check");
if (this != &rhs) {
this->Reset();
if (!rhs.IsEmpty()) {
this->slot() = rhs.slot();
api_internal::MoveGlobalReference(&rhs.slot(), &this->slot());
rhs.Clear();
}
}
return *this;
}
} // namespace v8
#endif // INCLUDE_V8_PERSISTENT_HANDLE_H_