/src/serenity/Userland/Libraries/LibJS/Runtime/WeakRefConstructor.cpp
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 | | #include <LibJS/Runtime/AbstractOperations.h> |
8 | | #include <LibJS/Runtime/Error.h> |
9 | | #include <LibJS/Runtime/GlobalObject.h> |
10 | | #include <LibJS/Runtime/WeakRef.h> |
11 | | #include <LibJS/Runtime/WeakRefConstructor.h> |
12 | | |
13 | | namespace JS { |
14 | | |
15 | | JS_DEFINE_ALLOCATOR(WeakRefConstructor); |
16 | | |
17 | | WeakRefConstructor::WeakRefConstructor(Realm& realm) |
18 | 0 | : NativeFunction(realm.vm().names.WeakRef.as_string(), realm.intrinsics().function_prototype()) |
19 | 0 | { |
20 | 0 | } |
21 | | |
22 | | void WeakRefConstructor::initialize(Realm& realm) |
23 | 0 | { |
24 | 0 | auto& vm = this->vm(); |
25 | 0 | Base::initialize(realm); |
26 | | |
27 | | // 26.1.2.1 WeakRef.prototype, https://tc39.es/ecma262/#sec-weak-ref.prototype |
28 | 0 | define_direct_property(vm.names.prototype, realm.intrinsics().weak_ref_prototype(), 0); |
29 | |
|
30 | 0 | define_direct_property(vm.names.length, Value(1), Attribute::Configurable); |
31 | 0 | } |
32 | | |
33 | | // 26.1.1.1 WeakRef ( target ), https://tc39.es/ecma262/#sec-weak-ref-target |
34 | | ThrowCompletionOr<Value> WeakRefConstructor::call() |
35 | 0 | { |
36 | 0 | auto& vm = this->vm(); |
37 | | |
38 | | // 1. If NewTarget is undefined, throw a TypeError exception. |
39 | 0 | return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.WeakRef); |
40 | 0 | } |
41 | | |
42 | | // 26.1.1.1 WeakRef ( target ), https://tc39.es/ecma262/#sec-weak-ref-target |
43 | | ThrowCompletionOr<NonnullGCPtr<Object>> WeakRefConstructor::construct(FunctionObject& new_target) |
44 | 0 | { |
45 | 0 | auto& vm = this->vm(); |
46 | 0 | auto target = vm.argument(0); |
47 | | |
48 | | // 2. If CanBeHeldWeakly(target) is false, throw a TypeError exception. |
49 | 0 | if (!can_be_held_weakly(target)) |
50 | 0 | return vm.throw_completion<TypeError>(ErrorType::CannotBeHeldWeakly, target.to_string_without_side_effects()); |
51 | | |
52 | | // 3. Let weakRef be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »). |
53 | | // 4. Perform AddToKeptObjects(target). |
54 | | // 5. Set weakRef.[[WeakRefTarget]] to target. |
55 | | // 6. Return weakRef. |
56 | 0 | if (target.is_object()) |
57 | 0 | return TRY(ordinary_create_from_constructor<WeakRef>(vm, new_target, &Intrinsics::weak_ref_prototype, target.as_object())); |
58 | 0 | VERIFY(target.is_symbol()); |
59 | 0 | return TRY(ordinary_create_from_constructor<WeakRef>(vm, new_target, &Intrinsics::weak_ref_prototype, target.as_symbol())); |
60 | 0 | } |
61 | | |
62 | | } |