summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.h
blob: bbfeff46ad68b4d840b0a5822d069112c43cb54d (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
/*
 * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/StringView.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/PromiseCapability.h>

namespace JS {

struct RemainingElements final : public Cell {
    JS_CELL(RemainingElements, Cell);

    u64 value { 0 };

private:
    RemainingElements() = default;

    explicit RemainingElements(u64 initial_value)
        : value(initial_value)
    {
    }
};

class PromiseValueList final : public Cell {
    JS_CELL(PromiseValueList, Cell);

public:
    Vector<Value>& values() { return m_values; }
    Vector<Value> const& values() const { return m_values; }

private:
    PromiseValueList() = default;

    virtual void visit_edges(Visitor&) override;

    Vector<Value> m_values;
};

class PromiseResolvingElementFunction : public NativeFunction {
    JS_OBJECT(PromiseResolvingFunction, NativeFunction);

public:
    virtual void initialize(Realm&) override;
    virtual ~PromiseResolvingElementFunction() override = default;

    virtual ThrowCompletionOr<Value> call() override;

protected:
    explicit PromiseResolvingElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&, Object& prototype);

    virtual ThrowCompletionOr<Value> resolve_element() = 0;

    size_t m_index { 0 };
    PromiseValueList& m_values;
    NonnullGCPtr<PromiseCapability> m_capability;
    RemainingElements& m_remaining_elements;

private:
    virtual void visit_edges(Visitor&) override;

    bool m_already_called { false };
};

// 27.2.4.1.3 Promise.all Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.all-resolve-element-functions
class PromiseAllResolveElementFunction final : public PromiseResolvingElementFunction {
    JS_OBJECT(PromiseResolvingFunction, NativeFunction);

public:
    static PromiseAllResolveElementFunction* create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&);

    virtual ~PromiseAllResolveElementFunction() override = default;

private:
    explicit PromiseAllResolveElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&, Object& prototype);

    virtual ThrowCompletionOr<Value> resolve_element() override;
};

// 27.2.4.2.2 Promise.allSettled Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.allsettled-resolve-element-functions
class PromiseAllSettledResolveElementFunction final : public PromiseResolvingElementFunction {
    JS_OBJECT(PromiseResolvingFunction, NativeFunction);

public:
    static PromiseAllSettledResolveElementFunction* create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&);

    virtual ~PromiseAllSettledResolveElementFunction() override = default;

private:
    explicit PromiseAllSettledResolveElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&, Object& prototype);

    virtual ThrowCompletionOr<Value> resolve_element() override;
};

// 27.2.4.2.3 Promise.allSettled Reject Element Functions, https://tc39.es/ecma262/#sec-promise.allsettled-reject-element-functions
class PromiseAllSettledRejectElementFunction final : public PromiseResolvingElementFunction {
    JS_OBJECT(PromiseResolvingFunction, PromiseResolvingElementFunction);

public:
    static PromiseAllSettledRejectElementFunction* create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&);

    virtual ~PromiseAllSettledRejectElementFunction() override = default;

private:
    explicit PromiseAllSettledRejectElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&, Object& prototype);

    virtual ThrowCompletionOr<Value> resolve_element() override;
};

// 27.2.4.3.2 Promise.any Reject Element Functions, https://tc39.es/ecma262/#sec-promise.any-reject-element-functions
class PromiseAnyRejectElementFunction final : public PromiseResolvingElementFunction {
    JS_OBJECT(PromiseResolvingFunction, PromiseResolvingElementFunction);

public:
    static PromiseAnyRejectElementFunction* create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&);

    virtual ~PromiseAnyRejectElementFunction() override = default;

private:
    explicit PromiseAnyRejectElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability>, RemainingElements&, Object& prototype);

    virtual ThrowCompletionOr<Value> resolve_element() override;
};

}