From 63cc2650e3fc4b79614d3655cb6b83e3ec944083 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 6 Aug 2022 15:50:38 +0200 Subject: [PATCH] LibJS: Make Handle more user-friendly Allow *handle, !handle, handle.ptr(), assignment from compatible pointer types, etc. This is in preparation for using Handles in more generated code. --- Userland/Libraries/LibJS/Heap/Handle.cpp | 1 - Userland/Libraries/LibJS/Heap/Handle.h | 71 ++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Heap/Handle.cpp b/Userland/Libraries/LibJS/Heap/Handle.cpp index 66d97e193b..c440374e4d 100644 --- a/Userland/Libraries/LibJS/Heap/Handle.cpp +++ b/Userland/Libraries/LibJS/Heap/Handle.cpp @@ -6,7 +6,6 @@ #include #include -#include #include namespace JS { diff --git a/Userland/Libraries/LibJS/Heap/Handle.h b/Userland/Libraries/LibJS/Heap/Handle.h index bdb97c022d..85540adf36 100644 --- a/Userland/Libraries/LibJS/Heap/Handle.h +++ b/Userland/Libraries/LibJS/Heap/Handle.h @@ -50,13 +50,74 @@ public: return Handle(adopt_ref(*new HandleImpl(cell))); } - T* cell() { return static_cast(m_impl->cell()); } - const T* cell() const { return static_cast(m_impl->cell()); } + Handle(T* cell) + { + if (cell) + m_impl = adopt_ref(*new HandleImpl(cell)); + } - bool is_null() const { return m_impl.is_null(); } + Handle(T& cell) + : m_impl(adopt_ref(*new HandleImpl(&cell))) + { + } - T* operator->() { return cell(); } - T const* operator->() const { return cell(); } + T* cell() + { + if (!m_impl) + return nullptr; + return static_cast(m_impl->cell()); + } + + T const* cell() const + { + if (!m_impl) + return nullptr; + return static_cast(m_impl->cell()); + } + + T* ptr() + { + return cell(); + } + T const* ptr() const + { + return cell(); + } + + bool is_null() const + { + return m_impl.is_null(); + } + + T* operator->() + { + return cell(); + } + T const* operator->() const + { + return cell(); + } + + T& operator*() + { + return *cell(); + } + T const& operator*() const + { + return *cell(); + } + + bool operator!() const + { + return !cell(); + } + operator bool() const + { + return cell(); + } + + operator T*() { return cell(); } + operator T const*() const { return cell(); } private: explicit Handle(NonnullRefPtr impl)