Serenity Operating System
1/*
2 * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibJS/Runtime/AbstractOperations.h>
8#include <LibJS/Runtime/FinalizationRegistry.h>
9
10namespace JS {
11
12FinalizationRegistry::FinalizationRegistry(Realm& realm, JobCallback cleanup_callback, Object& prototype)
13 : Object(ConstructWithPrototypeTag::Tag, prototype)
14 , WeakContainer(heap())
15 , m_realm(realm)
16 , m_cleanup_callback(move(cleanup_callback))
17{
18}
19
20void FinalizationRegistry::add_finalization_record(Cell& target, Value held_value, Cell* unregister_token)
21{
22 VERIFY(!held_value.is_empty());
23 m_records.append({ &target, held_value, unregister_token });
24}
25
26// Extracted from FinalizationRegistry.prototype.unregister ( unregisterToken )
27bool FinalizationRegistry::remove_by_token(Cell& unregister_token)
28{
29 // 4. Let removed be false.
30 auto removed = false;
31
32 // 5. For each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell of finalizationRegistry.[[Cells]], do
33 for (auto it = m_records.begin(); it != m_records.end(); ++it) {
34 // a. If cell.[[UnregisterToken]] is not empty and SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then
35 if (it->unregister_token == &unregister_token) {
36 // i. Remove cell from finalizationRegistry.[[Cells]].
37 it.remove(m_records);
38
39 // ii. Set removed to true.
40 removed = true;
41 }
42 }
43
44 // 6. Return removed.
45 return removed;
46}
47
48void FinalizationRegistry::remove_dead_cells(Badge<Heap>)
49{
50 auto any_cells_were_removed = false;
51 for (auto& record : m_records) {
52 if (!record.target || record.target->state() == Cell::State::Live)
53 continue;
54 record.target = nullptr;
55 any_cells_were_removed = true;
56 break;
57 }
58 if (any_cells_were_removed)
59 vm().host_enqueue_finalization_registry_cleanup_job(*this);
60}
61
62// 9.13 CleanupFinalizationRegistry ( finalizationRegistry ), https://tc39.es/ecma262/#sec-cleanup-finalization-registry
63ThrowCompletionOr<void> FinalizationRegistry::cleanup(Optional<JobCallback> callback)
64{
65 auto& vm = this->vm();
66
67 // 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
68 // Note: Ensured by type.
69
70 // 2. Let callback be finalizationRegistry.[[CleanupCallback]].
71 auto& cleanup_callback = callback.has_value() ? callback.value() : m_cleanup_callback;
72
73 // 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty, an implementation may perform the following steps:
74 for (auto it = m_records.begin(); it != m_records.end(); ++it) {
75 // a. Choose any such cell.
76 if (it->target != nullptr)
77 continue;
78
79 // b. Remove cell from finalizationRegistry.[[Cells]].
80 MarkedVector<Value> arguments(vm.heap());
81 arguments.append(it->held_value);
82 it.remove(m_records);
83
84 // c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
85 TRY(vm.host_call_job_callback(cleanup_callback, js_undefined(), move(arguments)));
86 }
87
88 // 4. Return unused.
89 return {};
90}
91
92void FinalizationRegistry::visit_edges(Cell::Visitor& visitor)
93{
94 Base::visit_edges(visitor);
95 visitor.visit(m_realm);
96 for (auto& record : m_records) {
97 visitor.visit(record.held_value);
98 visitor.visit(record.unregister_token);
99 }
100}
101
102}