Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibJS/Runtime/ShadowRealmConstructor.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
3
 * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <LibJS/Runtime/AbstractOperations.h>
9
#include <LibJS/Runtime/ShadowRealm.h>
10
#include <LibJS/Runtime/ShadowRealmConstructor.h>
11
12
namespace JS {
13
14
JS_DEFINE_ALLOCATOR(ShadowRealmConstructor);
15
16
// 3.2 The ShadowRealm Constructor, https://tc39.es/proposal-shadowrealm/#sec-shadowrealm-constructor
17
ShadowRealmConstructor::ShadowRealmConstructor(Realm& realm)
18
0
    : NativeFunction(realm.vm().names.ShadowRealm.as_string(), realm.intrinsics().function_prototype())
19
0
{
20
0
}
21
22
void ShadowRealmConstructor::initialize(Realm& realm)
23
0
{
24
0
    auto& vm = this->vm();
25
0
    Base::initialize(realm);
26
27
    // 3.3.1 ShadowRealm.prototype, https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype
28
0
    define_direct_property(vm.names.prototype, realm.intrinsics().shadow_realm_prototype(), 0);
29
30
0
    define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
31
0
}
32
33
// 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm
34
ThrowCompletionOr<Value> ShadowRealmConstructor::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.ShadowRealm);
40
0
}
41
42
// 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm
43
// https://github.com/tc39/proposal-shadowrealm/pull/410
44
ThrowCompletionOr<NonnullGCPtr<Object>> ShadowRealmConstructor::construct(FunctionObject& new_target)
45
0
{
46
0
    auto& vm = this->vm();
47
48
    // 2. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%ShadowRealm.prototype%", « [[ShadowRealm]] »).
49
0
    auto object = TRY(ordinary_create_from_constructor<ShadowRealm>(vm, new_target, &Intrinsics::shadow_realm_prototype));
50
51
    // 3. Let callerContext be the running execution context.
52
    // 4. Perform ? InitializeHostDefinedRealm().
53
    // 5. Let innerContext be the running execution context.
54
0
    auto inner_context = TRY(Realm::initialize_host_defined_realm(vm, nullptr, nullptr));
55
56
    // 6. Remove innerContext from the execution context stack and restore callerContext as the running execution context.
57
0
    vm.pop_execution_context();
58
59
    // 7. Let realmRec be the Realm of innerContext.
60
0
    auto& realm_record = *inner_context->realm;
61
62
    // 8. Set O.[[ShadowRealm]] to realmRec.
63
0
    object->set_shadow_realm(realm_record);
64
65
    // 9. Perform ? HostInitializeShadowRealm(realmRec).
66
0
    TRY(vm.host_initialize_shadow_realm(realm_record, move(inner_context), object));
67
68
    // 10. Return O.
69
0
    return object;
70
0
}
71
72
}