Files
ladybird/Libraries/LibJS/Runtime/WeakRef.h
Andreas Kling 245b7d74a7 LibGC: Prune weak containers in stop-the-world phase of GC
Move weak container cleanup (remove_dead_cells) out of both
sweep_dead_cells() and start_incremental_sweep() to the place
where it is actually safe to inspect cell state: collect_garbage().

Previously, remove_dead_cells could access cells that had already
been swept and poisoned by ASAN, causing use-after-poison crashes
when a new GC triggered while an incremental sweep was in progress.
2026-05-10 10:58:11 +02:00

45 lines
1.1 KiB
C++

/*
* Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGC/WeakContainer.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
class WeakRef final
: public Object
, public GC::WeakContainer {
JS_OBJECT(WeakRef, Object);
GC_DECLARE_ALLOCATOR(WeakRef);
public:
static GC::Ref<WeakRef> create(Realm&, Object&);
static GC::Ref<WeakRef> create(Realm&, Symbol&);
virtual ~WeakRef() override = default;
auto const& value() const { return m_value; }
void update_execution_generation() { m_last_execution_generation = vm().execution_generation(); }
virtual Cell const& owner_cell(Badge<GC::Heap>) const override { return *this; }
virtual void remove_dead_cells(Badge<GC::Heap>) override;
private:
explicit WeakRef(Object&, Object& prototype);
explicit WeakRef(Symbol&, Object& prototype);
virtual void visit_edges(Visitor&) override;
Variant<GC::Ptr<Object>, GC::Ptr<Symbol>, Empty> m_value;
u32 m_last_execution_generation { 0 };
};
}