/src/serenity/Userland/Libraries/LibJS/Runtime/FinalizationRegistry.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/SinglyLinkedList.h> |
10 | | #include <LibJS/Heap/GCPtr.h> |
11 | | #include <LibJS/Runtime/FunctionObject.h> |
12 | | #include <LibJS/Runtime/GlobalObject.h> |
13 | | #include <LibJS/Runtime/JobCallback.h> |
14 | | #include <LibJS/Runtime/Object.h> |
15 | | #include <LibJS/Runtime/Value.h> |
16 | | #include <LibJS/Runtime/WeakContainer.h> |
17 | | |
18 | | namespace JS { |
19 | | |
20 | | class FinalizationRegistry final |
21 | | : public Object |
22 | | , public WeakContainer { |
23 | | JS_OBJECT(FinalizationRegistry, Object); |
24 | | JS_DECLARE_ALLOCATOR(FinalizationRegistry); |
25 | | |
26 | | public: |
27 | 0 | virtual ~FinalizationRegistry() override = default; |
28 | | |
29 | | void add_finalization_record(Cell& target, Value held_value, Cell* unregister_token); |
30 | | bool remove_by_token(Cell& unregister_token); |
31 | | ThrowCompletionOr<void> cleanup(GCPtr<JobCallback> = {}); |
32 | | |
33 | | virtual void remove_dead_cells(Badge<Heap>) override; |
34 | | |
35 | 0 | Realm& realm() { return *m_realm; } |
36 | 0 | Realm const& realm() const { return *m_realm; } |
37 | | |
38 | 0 | JobCallback& cleanup_callback() { return *m_cleanup_callback; } |
39 | 0 | JobCallback const& cleanup_callback() const { return *m_cleanup_callback; } |
40 | | |
41 | | private: |
42 | | FinalizationRegistry(Realm&, NonnullGCPtr<JobCallback>, Object& prototype); |
43 | | |
44 | | virtual void visit_edges(Visitor& visitor) override; |
45 | | |
46 | | NonnullGCPtr<Realm> m_realm; |
47 | | NonnullGCPtr<JobCallback> m_cleanup_callback; |
48 | | |
49 | | struct FinalizationRecord { |
50 | | GCPtr<Cell> target; |
51 | | Value held_value; |
52 | | GCPtr<Cell> unregister_token; |
53 | | }; |
54 | | SinglyLinkedList<FinalizationRecord> m_records; |
55 | | }; |
56 | | |
57 | | } |