summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.cpp
blob: a03622d70b3ba6c087f8de1e18bce6a546983f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/AggregateError.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/PromiseResolvingElementFunctions.h>

namespace JS {

void PromiseValueList::visit_edges(Visitor& visitor)
{
    Cell::visit_edges(visitor);
    for (auto& val : m_values)
        visitor.visit(val);
}

PromiseResolvingElementFunction::PromiseResolvingElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements, Object& prototype)
    : NativeFunction(prototype)
    , m_index(index)
    , m_values(values)
    , m_capability(capability)
    , m_remaining_elements(remaining_elements)
{
}

void PromiseResolvingElementFunction::initialize(Realm& realm)
{
    Base::initialize(realm);
    define_direct_property(vm().names.length, Value(1), Attribute::Configurable);
}

ThrowCompletionOr<Value> PromiseResolvingElementFunction::call()
{
    if (m_already_called)
        return js_undefined();
    m_already_called = true;

    return resolve_element();
}

void PromiseResolvingElementFunction::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);

    visitor.visit(&m_values);
    visitor.visit(m_capability);
    visitor.visit(&m_remaining_elements);
}

PromiseAllResolveElementFunction* PromiseAllResolveElementFunction::create(Realm& realm, size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements)
{
    return realm.heap().allocate<PromiseAllResolveElementFunction>(realm, index, values, capability, remaining_elements, *realm.intrinsics().function_prototype());
}

PromiseAllResolveElementFunction::PromiseAllResolveElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements, Object& prototype)
    : PromiseResolvingElementFunction(index, values, capability, remaining_elements, prototype)
{
}

ThrowCompletionOr<Value> PromiseAllResolveElementFunction::resolve_element()
{
    auto& vm = this->vm();
    auto& realm = *vm.current_realm();

    // 8. Set values[index] to x.
    m_values.values()[m_index] = vm.argument(0);

    // 9. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
    // 10. If remainingElementsCount.[[Value]] is 0, then
    if (--m_remaining_elements.value == 0) {
        // a. Let valuesArray be CreateArrayFromList(values).
        auto values_array = Array::create_from(realm, m_values.values());

        // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
        return JS::call(vm, *m_capability->resolve(), js_undefined(), values_array);
    }

    // 11. Return undefined.
    return js_undefined();
}

PromiseAllSettledResolveElementFunction* PromiseAllSettledResolveElementFunction::create(Realm& realm, size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements)
{
    return realm.heap().allocate<PromiseAllSettledResolveElementFunction>(realm, index, values, capability, remaining_elements, *realm.intrinsics().function_prototype());
}

PromiseAllSettledResolveElementFunction::PromiseAllSettledResolveElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements, Object& prototype)
    : PromiseResolvingElementFunction(index, values, capability, remaining_elements, prototype)
{
}

ThrowCompletionOr<Value> PromiseAllSettledResolveElementFunction::resolve_element()
{
    auto& vm = this->vm();
    auto& realm = *vm.current_realm();

    // 9. Let obj be OrdinaryObjectCreate(%Object.prototype%).
    auto* object = Object::create(realm, realm.intrinsics().object_prototype());

    // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "fulfilled").
    MUST(object->create_data_property_or_throw(vm.names.status, PrimitiveString::create(vm, "fulfilled"sv)));

    // 11. Perform ! CreateDataPropertyOrThrow(obj, "value", x).
    MUST(object->create_data_property_or_throw(vm.names.value, vm.argument(0)));

    // 12. Set values[index] to obj.
    m_values.values()[m_index] = object;

    // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
    // 14. If remainingElementsCount.[[Value]] is 0, then
    if (--m_remaining_elements.value == 0) {
        // a. Let valuesArray be CreateArrayFromList(values).
        auto values_array = Array::create_from(realm, m_values.values());

        // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
        return JS::call(vm, *m_capability->resolve(), js_undefined(), values_array);
    }

    // 15. Return undefined.
    return js_undefined();
}

PromiseAllSettledRejectElementFunction* PromiseAllSettledRejectElementFunction::create(Realm& realm, size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements)
{
    return realm.heap().allocate<PromiseAllSettledRejectElementFunction>(realm, index, values, capability, remaining_elements, *realm.intrinsics().function_prototype());
}

PromiseAllSettledRejectElementFunction::PromiseAllSettledRejectElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements, Object& prototype)
    : PromiseResolvingElementFunction(index, values, capability, remaining_elements, prototype)
{
}

ThrowCompletionOr<Value> PromiseAllSettledRejectElementFunction::resolve_element()
{
    auto& vm = this->vm();
    auto& realm = *vm.current_realm();

    // 9. Let obj be OrdinaryObjectCreate(%Object.prototype%).
    auto* object = Object::create(realm, realm.intrinsics().object_prototype());

    // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "rejected").
    MUST(object->create_data_property_or_throw(vm.names.status, PrimitiveString::create(vm, "rejected"sv)));

    // 11. Perform ! CreateDataPropertyOrThrow(obj, "reason", x).
    MUST(object->create_data_property_or_throw(vm.names.reason, vm.argument(0)));

    // 12. Set values[index] to obj.
    m_values.values()[m_index] = object;

    // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
    // 14. If remainingElementsCount.[[Value]] is 0, then
    if (--m_remaining_elements.value == 0) {
        // a. Let valuesArray be CreateArrayFromList(values).
        auto values_array = Array::create_from(realm, m_values.values());

        // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
        return JS::call(vm, *m_capability->resolve(), js_undefined(), values_array);
    }

    // 15. Return undefined.
    return js_undefined();
}

PromiseAnyRejectElementFunction* PromiseAnyRejectElementFunction::create(Realm& realm, size_t index, PromiseValueList& errors, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements)
{
    return realm.heap().allocate<PromiseAnyRejectElementFunction>(realm, index, errors, capability, remaining_elements, *realm.intrinsics().function_prototype());
}

PromiseAnyRejectElementFunction::PromiseAnyRejectElementFunction(size_t index, PromiseValueList& errors, NonnullGCPtr<PromiseCapability> capability, RemainingElements& remaining_elements, Object& prototype)
    : PromiseResolvingElementFunction(index, errors, capability, remaining_elements, prototype)
{
}

ThrowCompletionOr<Value> PromiseAnyRejectElementFunction::resolve_element()
{
    auto& vm = this->vm();
    auto& realm = *vm.current_realm();

    // 8. Set errors[index] to x.
    m_values.values()[m_index] = vm.argument(0);

    // 9. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
    // 10. If remainingElementsCount.[[Value]] is 0, then
    if (--m_remaining_elements.value == 0) {
        // a. Let error be a newly created AggregateError object.
        auto error = AggregateError::create(realm);

        // b. Perform ! DefinePropertyOrThrow(error, "errors", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: CreateArrayFromList(errors) }).
        auto errors_array = Array::create_from(realm, m_values.values());
        MUST(error->define_property_or_throw(vm.names.errors, { .value = errors_array, .writable = true, .enumerable = false, .configurable = true }));

        // c. Return ? Call(promiseCapability.[[Reject]], undefined, « error »).
        return JS::call(vm, *m_capability->reject(), js_undefined(), error);
    }

    return js_undefined();
}

}