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).
This commit is contained in:
Andreas Kling 2020-03-09 22:14:51 +01:00
parent 05c80cac20
commit 386867da9f
Notes: sideshowbarker 2024-07-19 08:47:44 +09:00
3 changed files with 12 additions and 4 deletions

View file

@ -26,9 +26,17 @@
#include <AK/LogStream.h>
#include <LibJS/Cell.h>
#include <LibJS/Object.h>
#include <LibJS/Value.h>
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)

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/Forward.h>
#include <LibJS/Forward.h>
namespace JS {
@ -45,6 +46,7 @@ public:
class Visitor {
public:
virtual void visit(Cell*) = 0;
void visit(Value);
};
virtual void visit_children(Visitor&) {}

View file

@ -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);
}
}