From c97244d3a5463c227cbc9df7416bacbeb646e432 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sat, 25 Dec 2021 01:07:19 +0000 Subject: [PATCH] LibJS: Add Handle specialisation for Value This allows you to keep an arbitrary JS::Value alive without having to hook visit_edges somewhere, e.g. by being a NativeFunction that overrides visit_edges. For example, this allows you to store JS::Handle as the key of a HashMap. This will be used to keep arbitrary Values alive in the key of a temporary HashMap in Array.prototype.groupByToMap. Co-authored-by: Ali Mohammad Pur --- Userland/Libraries/LibJS/Heap/Handle.h | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Userland/Libraries/LibJS/Heap/Handle.h b/Userland/Libraries/LibJS/Heap/Handle.h index 5df24c7d03..e68b1d59f1 100644 --- a/Userland/Libraries/LibJS/Heap/Handle.h +++ b/Userland/Libraries/LibJS/Heap/Handle.h @@ -12,6 +12,7 @@ #include #include #include +#include namespace JS { @@ -77,4 +78,42 @@ inline Handle make_handle(T& cell) return Handle::create(&cell); } +template<> +class Handle { +public: + Handle() = default; + + static Handle create(Value value) + { + if (value.is_cell()) + return Handle(value, &value.as_cell()); + return Handle(value); + } + + auto cell() { return m_handle.cell(); } + auto cell() const { return m_handle.cell(); } + auto value() const { return m_value; } + bool is_null() const { return m_handle.is_null(); } + +private: + explicit Handle(Value value) + : m_value(value) + { + } + + explicit Handle(Value value, Cell* cell) + : m_value(value) + , m_handle(Handle::create(cell)) + { + } + + Value m_value; + Handle m_handle; +}; + +inline Handle make_handle(Value value) +{ + return Handle::create(value); +} + }