mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:17:45 +00:00
LibJS: Convert FinalizationRegistry::cleanup to ThrowCompletionOr
This commit is contained in:
parent
dcc284705b
commit
f324d91106
3 changed files with 19 additions and 7 deletions
|
@ -58,18 +58,30 @@ void FinalizationRegistry::remove_dead_cells(Badge<Heap>)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9.13 CleanupFinalizationRegistry ( finalizationRegistry ), https://tc39.es/ecma262/#sec-cleanup-finalization-registry
|
// 9.13 CleanupFinalizationRegistry ( finalizationRegistry ), https://tc39.es/ecma262/#sec-cleanup-finalization-registry
|
||||||
void FinalizationRegistry::cleanup(FunctionObject* callback)
|
ThrowCompletionOr<void> FinalizationRegistry::cleanup(FunctionObject* callback)
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
// 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
|
||||||
|
// Note: Ensured by type.
|
||||||
|
|
||||||
|
// 2. Let callback be finalizationRegistry.[[CleanupCallback]].
|
||||||
auto cleanup_callback = callback ?: m_cleanup_callback;
|
auto cleanup_callback = callback ?: m_cleanup_callback;
|
||||||
|
|
||||||
|
// 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty, an implementation may perform the following steps:
|
||||||
for (auto it = m_records.begin(); it != m_records.end(); ++it) {
|
for (auto it = m_records.begin(); it != m_records.end(); ++it) {
|
||||||
|
// a. Choose any such cell.
|
||||||
if (it->target != nullptr)
|
if (it->target != nullptr)
|
||||||
continue;
|
continue;
|
||||||
(void)call(global_object(), *cleanup_callback, js_undefined(), it->held_value);
|
auto cell = *it;
|
||||||
|
|
||||||
|
// b. Remove cell from finalizationRegistry.[[Cells]].
|
||||||
it.remove(m_records);
|
it.remove(m_records);
|
||||||
if (vm.exception())
|
|
||||||
return;
|
// c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
|
||||||
|
(void)TRY(call(global_object(), *cleanup_callback, js_undefined(), cell.held_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4. Return NormalCompletion(empty).
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
|
void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
|
|
||||||
void add_finalization_record(Cell& target, Value held_value, Object* unregister_token);
|
void add_finalization_record(Cell& target, Value held_value, Object* unregister_token);
|
||||||
bool remove_by_token(Object& unregister_token);
|
bool remove_by_token(Object& unregister_token);
|
||||||
void cleanup(FunctionObject* callback = nullptr);
|
ThrowCompletionOr<void> cleanup(FunctionObject* callback = nullptr);
|
||||||
|
|
||||||
virtual void remove_dead_cells(Badge<Heap>) override;
|
virtual void remove_dead_cells(Badge<Heap>) override;
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ JS_DEFINE_NATIVE_FUNCTION(FinalizationRegistryPrototype::cleanup_some)
|
||||||
if (vm.argument_count() > 0 && !callback.is_function())
|
if (vm.argument_count() > 0 && !callback.is_function())
|
||||||
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
|
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, callback.to_string_without_side_effects());
|
||||||
|
|
||||||
finalization_registry->cleanup(callback.is_undefined() ? nullptr : &callback.as_function());
|
TRY(finalization_registry->cleanup(callback.is_undefined() ? nullptr : &callback.as_function()));
|
||||||
|
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue