1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:17:35 +00:00

LibJS: Add Cell::must_survive_garbage_collection() mechanism

This allows cells to prevent themselves from being garbage collected,
even when there are no references to them.
This commit is contained in:
Andreas Kling 2022-10-24 12:37:54 +02:00
parent 8ace6b4f1d
commit 51579810bd
3 changed files with 24 additions and 4 deletions

View file

@ -1,11 +1,12 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/Noncopyable.h>
@ -80,15 +81,25 @@ public:
// This will be called on unmarked objects by the garbage collector in a separate pass before destruction.
virtual void finalize() { }
// This allows cells to survive GC by choice, even if nothing points to them.
// It's used to implement special rules in the web platform.
// NOTE: Cells must call set_overrides_must_survive_garbage_collection() for this to be honored.
virtual bool must_survive_garbage_collection() const { return false; }
bool overrides_must_survive_garbage_collection(Badge<Heap>) const { return m_overrides_must_survive_garbage_collection; }
Heap& heap() const;
VM& vm() const;
protected:
Cell() = default;
void set_overrides_must_survive_garbage_collection(bool b) { m_overrides_must_survive_garbage_collection = b; }
private:
bool m_mark : 1 { false };
State m_state : 7 { State::Live };
bool m_overrides_must_survive_garbage_collection { false };
State m_state : 1 { State::Live };
};
}