#ifndef V8_HANDLES_MAYBE_HANDLES_H_
#define V8_HANDLES_MAYBE_HANDLES_H_
#include <type_traits>
#include "src/handles/handles.h"
namespace v8 {
namespace internal {
struct NullMaybeHandleType { … };
constexpr NullMaybeHandleType kNullMaybeHandle;
template <typename T>
class MaybeHandle final { … };
template <typename T>
std::ostream& operator<<(std::ostream& os, MaybeHandle<T> handle);
class MaybeObjectHandle { … };
#ifdef V8_ENABLE_DIRECT_HANDLE
template <typename T>
class MaybeDirectHandle final {
public:
V8_INLINE MaybeDirectHandle() = default;
V8_INLINE MaybeDirectHandle(NullMaybeHandleType) {}
template <typename S, typename = std::enable_if_t<is_subtype_v<S, T>>>
V8_INLINE MaybeDirectHandle(DirectHandle<S> handle)
: location_(handle.address()) {}
template <typename S, typename = std::enable_if_t<is_subtype_v<S, T>>>
V8_INLINE MaybeDirectHandle(Handle<S> handle)
: MaybeDirectHandle(DirectHandle<S>(handle)) {}
template <typename S, typename = std::enable_if_t<is_subtype_v<S, T>>>
V8_INLINE MaybeDirectHandle(MaybeDirectHandle<S> maybe_handle)
: location_(maybe_handle.location_) {}
template <typename S, typename = std::enable_if_t<is_subtype_v<S, T>>>
V8_INLINE MaybeDirectHandle(MaybeIndirectHandle<S> maybe_handle)
: location_(maybe_handle.location_ == nullptr ? kTaggedNullAddress
: *maybe_handle.location_) {
}
V8_INLINE MaybeDirectHandle(Tagged<T> object, Isolate* isolate);
V8_INLINE MaybeDirectHandle(Tagged<T> object, LocalHeap* local_heap);
V8_INLINE void Assert() const { DCHECK_NE(location_, kTaggedNullAddress); }
V8_INLINE void Check() const { CHECK_NE(location_, kTaggedNullAddress); }
V8_INLINE DirectHandle<T> ToHandleChecked() const {
Check();
return DirectHandle<T>(location_);
}
template <typename S>
V8_WARN_UNUSED_RESULT V8_INLINE bool ToHandle(DirectHandle<S>* out) const {
if (location_ == kTaggedNullAddress) {
*out = DirectHandle<T>::null();
return false;
} else {
*out = DirectHandle<T>(location_);
return true;
}
}
V8_INLINE Address address() const { return location_; }
bool is_null() const { return location_ == kTaggedNullAddress; }
protected:
V8_INLINE explicit MaybeDirectHandle(Address location)
: location_(location) {}
Address location_ = kTaggedNullAddress;
template <typename>
friend class MaybeDirectHandle;
template <typename>
friend class MaybeHandle;
template <typename To, typename From>
friend inline MaybeDirectHandle<To> Cast(MaybeDirectHandle<From> value,
const v8::SourceLocation& loc);
};
class MaybeObjectDirectHandle {
public:
inline MaybeObjectDirectHandle()
: reference_type_(HeapObjectReferenceType::STRONG) {}
inline MaybeObjectDirectHandle(Tagged<MaybeObject> object, Isolate* isolate);
inline MaybeObjectDirectHandle(Tagged<Object> object, Isolate* isolate);
inline MaybeObjectDirectHandle(Tagged<MaybeObject> object,
LocalHeap* local_heap);
inline MaybeObjectDirectHandle(Tagged<Object> object, LocalHeap* local_heap);
inline explicit MaybeObjectDirectHandle(DirectHandle<Object> object);
static inline MaybeObjectDirectHandle Weak(Tagged<Object> object,
Isolate* isolate);
static inline MaybeObjectDirectHandle Weak(DirectHandle<Object> object);
inline Tagged<MaybeObject> operator*() const;
inline Tagged<MaybeObject> operator->() const;
inline DirectHandle<Object> object() const;
inline bool is_identical_to(const MaybeObjectDirectHandle& other) const;
bool is_null() const { return handle_.is_null(); }
private:
inline MaybeObjectDirectHandle(Tagged<Object> object,
HeapObjectReferenceType reference_type,
Isolate* isolate);
inline MaybeObjectDirectHandle(DirectHandle<Object> object,
HeapObjectReferenceType reference_type);
HeapObjectReferenceType reference_type_;
MaybeDirectHandle<Object> handle_;
};
#endif
}
}
#endif