mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:47:34 +00:00
LibJS: Implement WeakRef changes from 'Symbol as WeakMap Keys Proposal'
This commit is contained in:
parent
dbd0110721
commit
53ed8decaf
6 changed files with 69 additions and 26 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -8,26 +8,38 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
WeakRef* WeakRef::create(GlobalObject& global_object, Object* object)
|
||||
WeakRef* WeakRef::create(GlobalObject& global_object, Object& value)
|
||||
{
|
||||
return global_object.heap().allocate<WeakRef>(global_object, object, *global_object.weak_ref_prototype());
|
||||
return global_object.heap().allocate<WeakRef>(global_object, value, *global_object.weak_ref_prototype());
|
||||
}
|
||||
|
||||
WeakRef::WeakRef(Object* object, Object& prototype)
|
||||
WeakRef* WeakRef::create(GlobalObject& global_object, Symbol& value)
|
||||
{
|
||||
return global_object.heap().allocate<WeakRef>(global_object, value, *global_object.weak_ref_prototype());
|
||||
}
|
||||
|
||||
WeakRef::WeakRef(Object& value, Object& prototype)
|
||||
: Object(prototype)
|
||||
, WeakContainer(heap())
|
||||
, m_value(object)
|
||||
, m_value(&value)
|
||||
, m_last_execution_generation(vm().execution_generation())
|
||||
{
|
||||
}
|
||||
|
||||
WeakRef::WeakRef(Symbol& value, Object& prototype)
|
||||
: Object(prototype)
|
||||
, WeakContainer(heap())
|
||||
, m_value(&value)
|
||||
, m_last_execution_generation(vm().execution_generation())
|
||||
{
|
||||
}
|
||||
|
||||
void WeakRef::remove_dead_cells(Badge<Heap>)
|
||||
{
|
||||
VERIFY(m_value);
|
||||
if (m_value->state() == Cell::State::Live)
|
||||
if (m_value.visit([](Cell* cell) -> bool { return cell->state() == Cell::State::Live; }, [](Empty) -> bool { VERIFY_NOT_REACHED(); }))
|
||||
return;
|
||||
|
||||
m_value = nullptr;
|
||||
m_value = Empty {};
|
||||
// This is an optimization, we deregister from the garbage collector early (even if we were not garbage collected ourself yet)
|
||||
// to reduce the garbage collection overhead, which we can do because a cleared weak ref cannot be reused.
|
||||
WeakContainer::deregister();
|
||||
|
@ -37,8 +49,10 @@ void WeakRef::visit_edges(Visitor& visitor)
|
|||
{
|
||||
Base::visit_edges(visitor);
|
||||
|
||||
if (vm().execution_generation() == m_last_execution_generation)
|
||||
visitor.visit(m_value);
|
||||
if (vm().execution_generation() == m_last_execution_generation) {
|
||||
auto* cell = m_value.visit([](Cell* cell) -> Cell* { return cell; }, [](Empty) -> Cell* { return nullptr; });
|
||||
visitor.visit(cell);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue