From 1b391d78ae2829b0c52bca011460046ecf6af230 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 16 Apr 2020 16:07:50 +0200 Subject: [PATCH] LibJS: Allow cells to mark null pointers This simplifies the cell visiting functions by letting them not worry about the pointers they pass to the visitor being null. --- Libraries/LibJS/Heap/Heap.cpp | 7 ++----- Libraries/LibJS/Runtime/Cell.cpp | 8 +++++++- Libraries/LibJS/Runtime/Cell.h | 5 ++++- Libraries/LibJS/Runtime/LexicalEnvironment.cpp | 3 +-- Libraries/LibJS/Runtime/Shape.cpp | 6 ++---- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Libraries/LibJS/Heap/Heap.cpp b/Libraries/LibJS/Heap/Heap.cpp index ca98e33b91..b565182a3a 100644 --- a/Libraries/LibJS/Heap/Heap.cpp +++ b/Libraries/LibJS/Heap/Heap.cpp @@ -188,7 +188,7 @@ class MarkingVisitor final : public Cell::Visitor { public: MarkingVisitor() {} - virtual void visit(Cell* cell) + virtual void visit_impl(Cell* cell) { if (cell->is_marked()) return; @@ -206,11 +206,8 @@ void Heap::mark_live_cells(const HashTable& roots) dbg() << "mark_live_cells:"; #endif MarkingVisitor visitor; - for (auto* root : roots) { - if (!root) - continue; + for (auto* root : roots) visitor.visit(root); - } } void Heap::sweep_dead_cells() diff --git a/Libraries/LibJS/Runtime/Cell.cpp b/Libraries/LibJS/Runtime/Cell.cpp index 222dd85502..c80b59e824 100644 --- a/Libraries/LibJS/Runtime/Cell.cpp +++ b/Libraries/LibJS/Runtime/Cell.cpp @@ -34,10 +34,16 @@ namespace JS { +void Cell::Visitor::visit(Cell* cell) +{ + if (cell) + visit_impl(cell); +} + void Cell::Visitor::visit(Value value) { if (value.is_cell()) - visit(value.as_cell()); + visit_impl(value.as_cell()); } Heap& Cell::heap() const diff --git a/Libraries/LibJS/Runtime/Cell.h b/Libraries/LibJS/Runtime/Cell.h index f9ae449ab3..9989618ed4 100644 --- a/Libraries/LibJS/Runtime/Cell.h +++ b/Libraries/LibJS/Runtime/Cell.h @@ -49,8 +49,11 @@ public: class Visitor { public: - virtual void visit(Cell*) = 0; + void visit(Cell*); void visit(Value); + + protected: + virtual void visit_impl(Cell*) = 0; }; virtual void visit_children(Visitor&) {} diff --git a/Libraries/LibJS/Runtime/LexicalEnvironment.cpp b/Libraries/LibJS/Runtime/LexicalEnvironment.cpp index ddf244bb68..518126e0d5 100644 --- a/Libraries/LibJS/Runtime/LexicalEnvironment.cpp +++ b/Libraries/LibJS/Runtime/LexicalEnvironment.cpp @@ -45,8 +45,7 @@ LexicalEnvironment::~LexicalEnvironment() void LexicalEnvironment::visit_children(Visitor& visitor) { Cell::visit_children(visitor); - if (m_parent) - visitor.visit(m_parent); + visitor.visit(m_parent); for (auto& it : m_variables) visitor.visit(it.value.value); } diff --git a/Libraries/LibJS/Runtime/Shape.cpp b/Libraries/LibJS/Runtime/Shape.cpp index 28e10c5cc9..cbc7649c81 100644 --- a/Libraries/LibJS/Runtime/Shape.cpp +++ b/Libraries/LibJS/Runtime/Shape.cpp @@ -81,10 +81,8 @@ Shape::~Shape() void Shape::visit_children(Cell::Visitor& visitor) { Cell::visit_children(visitor); - if (m_prototype) - visitor.visit(m_prototype); - if (m_previous) - visitor.visit(m_previous); + visitor.visit(m_prototype); + visitor.visit(m_previous); for (auto& it : m_forward_transitions) visitor.visit(it.value); }