ladybird/Userland/Libraries/LibJS/Runtime/FinalizationRegistry.h
Andreas Kling c364520c24 LibJS+js+test-js: Add GC debug mode that keeps cells "alive" as zombies
This patch adds a `-z` option to js and test-js. When run in this mode,
garbage cells are never actually destroyed. We instead keep them around
in a special zombie state.

This allows us to validate that zombies don't get marked in future GC
scans (since there were not supposed to be any more references!) :^)

Cells get notified when they become a zombie (via did_become_zombie())
and this is used by WeakContainer cells to deregister themselves from
the heap.
2021-09-11 16:52:03 +02:00

49 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/SinglyLinkedList.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/WeakContainer.h>
namespace JS {
class FinalizationRegistry final
: public Object
, public WeakContainer {
JS_OBJECT(FinalizationRegistry, Object);
public:
static FinalizationRegistry* create(GlobalObject&, FunctionObject&);
explicit FinalizationRegistry(FunctionObject&, Object& prototype);
virtual ~FinalizationRegistry() override;
void add_finalization_record(Cell& target, Value held_value, Object* unregister_token);
bool remove_by_token(Object& unregister_token);
void cleanup(FunctionObject* callback = nullptr);
virtual void remove_swept_cells(Badge<Heap>, Span<Cell*>) override;
private:
virtual void visit_edges(Visitor& visitor) override;
virtual void did_become_zombie() override { deregister(); }
FunctionObject* m_cleanup_callback { nullptr };
struct FinalizationRecord {
Cell* target { nullptr };
Value held_value;
Object* unregister_token { nullptr };
};
SinglyLinkedList<FinalizationRecord> m_records;
};
}