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

LibJS: Implement stage 3 proposal FinalizationRegistry changes

Specifically the 'Symbol as WeakMap Keys Proposal'.
This commit is contained in:
Idan Horowitz 2022-06-22 23:10:11 +03:00
parent 53ed8decaf
commit a79796ea4a
5 changed files with 48 additions and 27 deletions

View file

@ -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
*/
@ -17,13 +17,13 @@ FinalizationRegistry::FinalizationRegistry(Realm& realm, JobCallback cleanup_cal
{
}
void FinalizationRegistry::add_finalization_record(Cell& target, Value held_value, Object* unregister_token)
void FinalizationRegistry::add_finalization_record(Cell& target, Value held_value, Cell* unregister_token)
{
VERIFY(!held_value.is_empty());
m_records.append({ &target, held_value, unregister_token });
}
bool FinalizationRegistry::remove_by_token(Object& unregister_token)
bool FinalizationRegistry::remove_by_token(Cell& unregister_token)
{
auto removed = false;
for (auto it = m_records.begin(); it != m_records.end(); ++it) {

View file

@ -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
*/
@ -25,8 +25,8 @@ public:
explicit FinalizationRegistry(Realm&, JobCallback, Object& prototype);
virtual ~FinalizationRegistry() override = default;
void add_finalization_record(Cell& target, Value held_value, Object* unregister_token);
bool remove_by_token(Object& unregister_token);
void add_finalization_record(Cell& target, Value held_value, Cell* unregister_token);
bool remove_by_token(Cell& unregister_token);
ThrowCompletionOr<void> cleanup(Optional<JobCallback> = {});
virtual void remove_dead_cells(Badge<Heap>) override;
@ -46,7 +46,7 @@ private:
struct FinalizationRecord {
Cell* target { nullptr };
Value held_value;
Object* unregister_token { nullptr };
Cell* unregister_token { nullptr };
};
SinglyLinkedList<FinalizationRecord> m_records;
};

View file

@ -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
*/
@ -50,18 +50,18 @@ JS_DEFINE_NATIVE_FUNCTION(FinalizationRegistryPrototype::register_)
auto* finalization_registry = TRY(typed_this_object(global_object));
auto target = vm.argument(0);
if (!target.is_object())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
if (!can_be_held_weakly(target))
return vm.throw_completion<TypeError>(global_object, ErrorType::CannotBeHeldWeakly, target.to_string_without_side_effects());
auto held_value = vm.argument(1);
if (same_value(target, held_value))
return vm.throw_completion<TypeError>(global_object, ErrorType::FinalizationRegistrySameTargetAndValue);
auto unregister_token = vm.argument(2);
if (!unregister_token.is_object() && !unregister_token.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, unregister_token.to_string_without_side_effects());
if (!can_be_held_weakly(unregister_token) && !unregister_token.is_undefined())
return vm.throw_completion<TypeError>(global_object, ErrorType::CannotBeHeldWeakly, unregister_token.to_string_without_side_effects());
finalization_registry->add_finalization_record(target.as_cell(), held_value, unregister_token.is_undefined() ? nullptr : &unregister_token.as_object());
finalization_registry->add_finalization_record(target.as_cell(), held_value, unregister_token.is_undefined() ? nullptr : &unregister_token.as_cell());
return js_undefined();
}
@ -72,10 +72,10 @@ JS_DEFINE_NATIVE_FUNCTION(FinalizationRegistryPrototype::unregister)
auto* finalization_registry = TRY(typed_this_object(global_object));
auto unregister_token = vm.argument(0);
if (!unregister_token.is_object())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, unregister_token.to_string_without_side_effects());
if (!can_be_held_weakly(unregister_token))
return vm.throw_completion<TypeError>(global_object, ErrorType::CannotBeHeldWeakly, unregister_token.to_string_without_side_effects());
return Value(finalization_registry->remove_by_token(unregister_token.as_object()));
return Value(finalization_registry->remove_by_token(unregister_token.as_cell()));
}
}