From 386867da9f9d2da3ad39f3f3c550b81d9301809c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 9 Mar 2020 22:14:51 +0100 Subject: [PATCH] LibJS: Add a convenience helper for visiting a JS::Value We only really care to visit values if they refer to a Cell, but it's nice to be able to say visit(some_value). --- Libraries/LibJS/Cell.cpp | 8 ++++++++ Libraries/LibJS/Cell.h | 2 ++ Libraries/LibJS/Object.cpp | 6 ++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Libraries/LibJS/Cell.cpp b/Libraries/LibJS/Cell.cpp index 3aedb7cb66..8e0744ea0b 100644 --- a/Libraries/LibJS/Cell.cpp +++ b/Libraries/LibJS/Cell.cpp @@ -26,9 +26,17 @@ #include #include +#include +#include namespace JS { +void Cell::Visitor::visit(Value value) +{ + if (value.is_object()) + visit(value.as_object()); +} + const LogStream& operator<<(const LogStream& stream, const Cell* cell) { if (!cell) diff --git a/Libraries/LibJS/Cell.h b/Libraries/LibJS/Cell.h index d4e347a1af..274e41cb4f 100644 --- a/Libraries/LibJS/Cell.h +++ b/Libraries/LibJS/Cell.h @@ -27,6 +27,7 @@ #pragma once #include +#include namespace JS { @@ -45,6 +46,7 @@ public: class Visitor { public: virtual void visit(Cell*) = 0; + void visit(Value); }; virtual void visit_children(Visitor&) {} diff --git a/Libraries/LibJS/Object.cpp b/Libraries/LibJS/Object.cpp index 53f010681d..2d4f021a9f 100644 --- a/Libraries/LibJS/Object.cpp +++ b/Libraries/LibJS/Object.cpp @@ -51,10 +51,8 @@ void Object::put(String property_name, Value value) void Object::visit_children(Cell::Visitor& visitor) { Cell::visit_children(visitor); - for (auto& it : m_properties) { - if (it.value.is_object()) - visitor.visit(it.value.as_object()); - } + for (auto& it : m_properties) + visitor.visit(it.value); } }