Coverage Report

Created: 2026-02-14 08:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibJS/Runtime/FinalizationRegistryConstructor.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, 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/Error.h>
9
#include <LibJS/Runtime/FinalizationRegistry.h>
10
#include <LibJS/Runtime/FinalizationRegistryConstructor.h>
11
#include <LibJS/Runtime/GlobalObject.h>
12
#include <LibJS/Runtime/JobCallback.h>
13
14
namespace JS {
15
16
JS_DEFINE_ALLOCATOR(FinalizationRegistryConstructor);
17
18
FinalizationRegistryConstructor::FinalizationRegistryConstructor(Realm& realm)
19
0
    : NativeFunction(realm.vm().names.FinalizationRegistry.as_string(), realm.intrinsics().function_prototype())
20
0
{
21
0
}
22
23
void FinalizationRegistryConstructor::initialize(Realm& realm)
24
0
{
25
0
    auto& vm = this->vm();
26
0
    Base::initialize(realm);
27
28
    // 26.2.2.1 FinalizationRegistry.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
29
0
    define_direct_property(vm.names.prototype, realm.intrinsics().finalization_registry_prototype(), 0);
30
31
0
    define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
32
0
}
33
34
// 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
35
ThrowCompletionOr<Value> FinalizationRegistryConstructor::call()
36
0
{
37
0
    auto& vm = this->vm();
38
39
    // 1. If NewTarget is undefined, throw a TypeError exception.
40
0
    return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.FinalizationRegistry);
41
0
}
42
43
// 26.2.1.1 FinalizationRegistry ( cleanupCallback ), https://tc39.es/ecma262/#sec-finalization-registry-cleanup-callback
44
ThrowCompletionOr<NonnullGCPtr<Object>> FinalizationRegistryConstructor::construct(FunctionObject& new_target)
45
0
{
46
0
    auto& vm = this->vm();
47
48
    // 2. If IsCallable(cleanupCallback) is false, throw a TypeError exception.
49
0
    auto cleanup_callback = vm.argument(0);
50
0
    if (!cleanup_callback.is_function())
51
0
        return vm.throw_completion<TypeError>(ErrorType::NotAFunction, cleanup_callback.to_string_without_side_effects());
52
53
    // 3. Let finalizationRegistry be ? OrdinaryCreateFromConstructor(NewTarget, "%FinalizationRegistry.prototype%", « [[Realm]], [[CleanupCallback]], [[Cells]] »).
54
    // 4. Let fn be the active function object.
55
    // NOTE: This is not necessary, the active function object is `this`.
56
    // 5. Set finalizationRegistry.[[Realm]] to fn.[[Realm]].
57
    // 6. Set finalizationRegistry.[[CleanupCallback]] to HostMakeJobCallback(cleanupCallback).
58
    // 7. Set finalizationRegistry.[[Cells]] to a new empty List.
59
    // NOTE: This is done inside FinalizationRegistry instead of here.
60
    // 8. Return finalizationRegistry.
61
0
    return TRY(ordinary_create_from_constructor<FinalizationRegistry>(vm, new_target, &Intrinsics::finalization_registry_prototype, *realm(), vm.host_make_job_callback(cleanup_callback.as_function())));
62
0
}
63
64
}