Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
3
 * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <LibJS/Runtime/GlobalObject.h>
9
#include <LibJS/Runtime/ObjectEnvironment.h>
10
#include <LibJS/Runtime/ValueInlines.h>
11
12
namespace JS {
13
14
JS_DEFINE_ALLOCATOR(ObjectEnvironment);
15
16
ObjectEnvironment::ObjectEnvironment(Object& binding_object, IsWithEnvironment is_with_environment, Environment* outer_environment)
17
0
    : Environment(outer_environment)
18
0
    , m_binding_object(binding_object)
19
0
    , m_with_environment(is_with_environment == IsWithEnvironment::Yes)
20
0
{
21
0
}
22
23
void ObjectEnvironment::visit_edges(Cell::Visitor& visitor)
24
0
{
25
0
    Base::visit_edges(visitor);
26
0
    visitor.visit(m_binding_object);
27
0
}
28
29
// 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n
30
ThrowCompletionOr<bool> ObjectEnvironment::has_binding(DeprecatedFlyString const& name, Optional<size_t>*) const
31
0
{
32
0
    auto& vm = this->vm();
33
34
    // 1. Let bindingObject be envRec.[[BindingObject]].
35
36
    // 2. Let foundBinding be ? HasProperty(bindingObject, N).
37
0
    bool found_binding = TRY(m_binding_object->has_property(name));
38
39
    // 3. If foundBinding is false, return false.
40
0
    if (!found_binding)
41
0
        return false;
42
43
    // 4. If envRec.[[IsWithEnvironment]] is false, return true.
44
0
    if (!m_with_environment)
45
0
        return true;
46
47
    // 5. Let unscopables be ? Get(bindingObject, @@unscopables).
48
0
    auto unscopables = TRY(m_binding_object->get(vm.well_known_symbol_unscopables()));
49
50
    // 6. If Type(unscopables) is Object, then
51
0
    if (unscopables.is_object()) {
52
        // a. Let blocked be ToBoolean(? Get(unscopables, N)).
53
0
        auto blocked = TRY(unscopables.as_object().get(name)).to_boolean();
54
55
        // b. If blocked is true, return false.
56
0
        if (blocked)
57
0
            return false;
58
0
    }
59
60
    // 7. Return true.
61
0
    return true;
62
0
}
63
64
// 9.1.1.2.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-object-environment-records-createmutablebinding-n-d
65
ThrowCompletionOr<void> ObjectEnvironment::create_mutable_binding(VM&, DeprecatedFlyString const& name, bool can_be_deleted)
66
0
{
67
    // 1. Let bindingObject be envRec.[[BindingObject]].
68
    // 2. Perform ? DefinePropertyOrThrow(bindingObject, N, PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }).
69
0
    TRY(m_binding_object->define_property_or_throw(name, { .value = js_undefined(), .writable = true, .enumerable = true, .configurable = can_be_deleted }));
70
71
    // 3. Return unused.
72
0
    return {};
73
0
}
74
75
// 9.1.1.2.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-createimmutablebinding-n-s
76
ThrowCompletionOr<void> ObjectEnvironment::create_immutable_binding(VM&, DeprecatedFlyString const&, bool)
77
0
{
78
    // "The CreateImmutableBinding concrete method of an object Environment Record is never used within this specification."
79
0
    VERIFY_NOT_REACHED();
80
0
}
81
82
// 9.1.1.2.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-object-environment-records-initializebinding-n-v
83
ThrowCompletionOr<void> ObjectEnvironment::initialize_binding(VM& vm, DeprecatedFlyString const& name, Value value, Environment::InitializeBindingHint hint)
84
0
{
85
    // 1. Assert: hint is normal.
86
0
    VERIFY(hint == Environment::InitializeBindingHint::Normal);
87
88
    // 2. Perform ? envRec.SetMutableBinding(N, V, false).
89
0
    TRY(set_mutable_binding(vm, name, value, false));
90
91
    // 2. Return unused.
92
0
    return {};
93
0
}
94
95
// 9.1.1.2.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-object-environment-records-setmutablebinding-n-v-s
96
ThrowCompletionOr<void> ObjectEnvironment::set_mutable_binding(VM&, DeprecatedFlyString const& name, Value value, bool strict)
97
0
{
98
0
    auto& vm = this->vm();
99
100
    // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check since we only use that
101
    //               information to throw errors in strict mode.
102
    //               We can't do this for with environments, since it would be observable (e.g via a Proxy)
103
    // FIXME: I think we could combine HasProperty and Set in strict mode if Set would return a bit more failure information.
104
0
    if (!m_with_environment && !strict)
105
0
        return m_binding_object->set(name, value, Object::ShouldThrowExceptions::No);
106
107
    // 1. Let bindingObject be envRec.[[BindingObject]].
108
    // 2. Let stillExists be ? HasProperty(bindingObject, N).
109
0
    auto still_exists = TRY(m_binding_object->has_property(name));
110
111
    // 3. If stillExists is false and S is true, throw a ReferenceError exception.
112
0
    if (!still_exists && strict)
113
0
        return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
114
115
    // 4. Perform ? Set(bindingObject, N, V, S).
116
0
    auto result_or_error = m_binding_object->set(name, value, strict ? Object::ShouldThrowExceptions::Yes : Object::ShouldThrowExceptions::No);
117
118
    // Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
119
0
    if (result_or_error.is_error() && strict) {
120
0
        auto property_or_error = m_binding_object->internal_get_own_property(name);
121
        // Return the initial error instead of masking it with the new error
122
0
        if (property_or_error.is_error())
123
0
            return result_or_error.release_error();
124
0
        auto property = property_or_error.release_value();
125
0
        if (property.has_value() && !property->writable.value_or(true)) {
126
0
            return vm.throw_completion<TypeError>(ErrorType::DescWriteNonWritable, name);
127
0
        }
128
0
    }
129
130
0
    if (result_or_error.is_error())
131
0
        return result_or_error.release_error();
132
133
    // 5. Return unused.
134
0
    return {};
135
0
}
136
137
// 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
138
ThrowCompletionOr<Value> ObjectEnvironment::get_binding_value(VM&, DeprecatedFlyString const& name, bool strict)
139
0
{
140
0
    auto& vm = this->vm();
141
142
    // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check
143
    //               since Get will return undefined for missing properties anyway. So we take advantage of this
144
    //               to avoid doing both HasProperty and Get.
145
    //               We can't do this for with environments, since it would be observable (e.g via a Proxy)
146
    // FIXME: We could combine HasProperty and Get in non-strict mode if Get would return a bit more failure information.
147
0
    if (!m_with_environment && !strict)
148
0
        return m_binding_object->get(name);
149
150
    // 1. Let bindingObject be envRec.[[BindingObject]].
151
    // 2. Let value be ? HasProperty(bindingObject, N).
152
0
    auto value = TRY(m_binding_object->has_property(name));
153
154
    // 3. If value is false, then
155
0
    if (!value) {
156
        // a. If S is false, return undefined; otherwise throw a ReferenceError exception.
157
0
        if (!strict)
158
0
            return js_undefined();
159
0
        return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
160
0
    }
161
162
    // 4. Return ? Get(bindingObject, N).
163
0
    return m_binding_object->get(name);
164
0
}
165
166
// 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
167
ThrowCompletionOr<bool> ObjectEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
168
0
{
169
    // 1. Let bindingObject be envRec.[[BindingObject]].
170
    // 2. Return ? bindingObject.[[Delete]](N).
171
0
    return m_binding_object->internal_delete(name);
172
0
}
173
174
}