/src/serenity/Userland/Libraries/LibJS/Runtime/PromiseResolvingElementFunctions.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/StringView.h> |
10 | | #include <LibJS/Runtime/NativeFunction.h> |
11 | | #include <LibJS/Runtime/PromiseCapability.h> |
12 | | |
13 | | namespace JS { |
14 | | |
15 | | struct RemainingElements final : public Cell { |
16 | | JS_CELL(RemainingElements, Cell); |
17 | | JS_DECLARE_ALLOCATOR(RemainingElements); |
18 | | |
19 | | u64 value { 0 }; |
20 | | |
21 | | private: |
22 | | RemainingElements() = default; |
23 | | |
24 | | explicit RemainingElements(u64 initial_value) |
25 | 0 | : value(initial_value) |
26 | 0 | { |
27 | 0 | } |
28 | | }; |
29 | | |
30 | | class PromiseValueList final : public Cell { |
31 | | JS_CELL(PromiseValueList, Cell); |
32 | | JS_DECLARE_ALLOCATOR(PromiseValueList); |
33 | | |
34 | | public: |
35 | 0 | Vector<Value>& values() { return m_values; } |
36 | 0 | Vector<Value> const& values() const { return m_values; } |
37 | | |
38 | | private: |
39 | 0 | PromiseValueList() = default; |
40 | | |
41 | | virtual void visit_edges(Visitor&) override; |
42 | | |
43 | | Vector<Value> m_values; |
44 | | }; |
45 | | |
46 | | class PromiseResolvingElementFunction : public NativeFunction { |
47 | | JS_OBJECT(PromiseResolvingElementFunction, NativeFunction); |
48 | | JS_DECLARE_ALLOCATOR(PromiseResolvingElementFunction); |
49 | | |
50 | | public: |
51 | | virtual void initialize(Realm&) override; |
52 | | virtual ~PromiseResolvingElementFunction() override = default; |
53 | | |
54 | | virtual ThrowCompletionOr<Value> call() override; |
55 | | |
56 | | protected: |
57 | | explicit PromiseResolvingElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&, Object& prototype); |
58 | | |
59 | | virtual ThrowCompletionOr<Value> resolve_element() = 0; |
60 | | |
61 | | size_t m_index { 0 }; |
62 | | NonnullGCPtr<PromiseValueList> m_values; |
63 | | NonnullGCPtr<PromiseCapability const> m_capability; |
64 | | NonnullGCPtr<RemainingElements> m_remaining_elements; |
65 | | |
66 | | private: |
67 | | virtual void visit_edges(Visitor&) override; |
68 | | |
69 | | bool m_already_called { false }; |
70 | | }; |
71 | | |
72 | | // 27.2.4.1.3 Promise.all Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.all-resolve-element-functions |
73 | | class PromiseAllResolveElementFunction final : public PromiseResolvingElementFunction { |
74 | | JS_OBJECT(PromiseAllResolveElementFunction, PromiseResolvingElementFunction); |
75 | | JS_DECLARE_ALLOCATOR(PromiseAllResolveElementFunction); |
76 | | |
77 | | public: |
78 | | static NonnullGCPtr<PromiseAllResolveElementFunction> create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&); |
79 | | |
80 | | virtual ~PromiseAllResolveElementFunction() override = default; |
81 | | |
82 | | private: |
83 | | explicit PromiseAllResolveElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&, Object& prototype); |
84 | | |
85 | | virtual ThrowCompletionOr<Value> resolve_element() override; |
86 | | }; |
87 | | |
88 | | // 27.2.4.2.2 Promise.allSettled Resolve Element Functions, https://tc39.es/ecma262/#sec-promise.allsettled-resolve-element-functions |
89 | | class PromiseAllSettledResolveElementFunction final : public PromiseResolvingElementFunction { |
90 | | JS_OBJECT(PromiseAllSettledResolveElementFunction, PromiseResolvingElementFunction); |
91 | | JS_DECLARE_ALLOCATOR(PromiseAllSettledResolveElementFunction); |
92 | | |
93 | | public: |
94 | | static NonnullGCPtr<PromiseAllSettledResolveElementFunction> create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&); |
95 | | |
96 | | virtual ~PromiseAllSettledResolveElementFunction() override = default; |
97 | | |
98 | | private: |
99 | | explicit PromiseAllSettledResolveElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&, Object& prototype); |
100 | | |
101 | | virtual ThrowCompletionOr<Value> resolve_element() override; |
102 | | }; |
103 | | |
104 | | // 27.2.4.2.3 Promise.allSettled Reject Element Functions, https://tc39.es/ecma262/#sec-promise.allsettled-reject-element-functions |
105 | | class PromiseAllSettledRejectElementFunction final : public PromiseResolvingElementFunction { |
106 | | JS_OBJECT(PromiseAllSettledRejectElementFunction, PromiseResolvingElementFunction); |
107 | | JS_DECLARE_ALLOCATOR(PromiseAllSettledRejectElementFunction); |
108 | | |
109 | | public: |
110 | | static NonnullGCPtr<PromiseAllSettledRejectElementFunction> create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&); |
111 | | |
112 | | virtual ~PromiseAllSettledRejectElementFunction() override = default; |
113 | | |
114 | | private: |
115 | | explicit PromiseAllSettledRejectElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&, Object& prototype); |
116 | | |
117 | | virtual ThrowCompletionOr<Value> resolve_element() override; |
118 | | }; |
119 | | |
120 | | // 27.2.4.3.2 Promise.any Reject Element Functions, https://tc39.es/ecma262/#sec-promise.any-reject-element-functions |
121 | | class PromiseAnyRejectElementFunction final : public PromiseResolvingElementFunction { |
122 | | JS_OBJECT(PromiseAnyRejectElementFunction, PromiseResolvingElementFunction); |
123 | | JS_DECLARE_ALLOCATOR(PromiseAnyRejectElementFunction); |
124 | | |
125 | | public: |
126 | | static NonnullGCPtr<PromiseAnyRejectElementFunction> create(Realm&, size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&); |
127 | | |
128 | | virtual ~PromiseAnyRejectElementFunction() override = default; |
129 | | |
130 | | private: |
131 | | explicit PromiseAnyRejectElementFunction(size_t, PromiseValueList&, NonnullGCPtr<PromiseCapability const>, RemainingElements&, Object& prototype); |
132 | | |
133 | | virtual ThrowCompletionOr<Value> resolve_element() override; |
134 | | }; |
135 | | |
136 | | } |