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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <AK/Optional.h>
#include <AK/Try.h>
#include <AK/Variant.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
// 6.2.3 The Completion Record Specification Type, https://tc39.es/ecma262/#sec-completion-record-specification-type
class [[nodiscard]] Completion {
public:
enum class Type {
Empty,
Normal,
Break,
Continue,
Return,
Throw,
};
ALWAYS_INLINE Completion(Type type, Optional<Value> value, Optional<FlyString> target)
: m_type(type)
, m_value(move(value))
, m_target(move(target))
{
VERIFY(type != Type::Empty);
if (m_value.has_value())
VERIFY(!m_value->is_empty());
}
Completion(ThrowCompletionOr<Value> const&);
// 5.2.3.1 Implicit Completion Values, https://tc39.es/ecma262/#sec-implicit-completion-values
// Not `explicit` on purpose.
ALWAYS_INLINE Completion(Value value)
: Completion(Type::Normal, value, {})
{
}
ALWAYS_INLINE Completion(Optional<Value> value)
: Completion(Type::Normal, move(value), {})
{
}
ALWAYS_INLINE Completion()
: Completion(js_undefined())
{
}
Completion(Completion const&) = default;
Completion& operator=(Completion const&) = default;
Completion(Completion&&) = default;
Completion& operator=(Completion&&) = default;
[[nodiscard]] Type type() const
{
VERIFY(m_type != Type::Empty);
return m_type;
}
[[nodiscard]] Optional<Value>& value() { return m_value; }
[[nodiscard]] Optional<Value> const& value() const { return m_value; }
[[nodiscard]] Optional<FlyString>& target() { return m_target; }
[[nodiscard]] Optional<FlyString> const& target() const { return m_target; }
// "abrupt completion refers to any completion with a [[Type]] value other than normal"
[[nodiscard]] bool is_abrupt() const { return m_type != Type::Normal; }
// These are for compatibility with the TRY() macro in AK.
[[nodiscard]] bool is_error() const { return m_type == Type::Throw; }
[[nodiscard]] Optional<Value> release_value() { return move(m_value); }
Completion release_error()
{
VERIFY(is_error());
VERIFY(m_value.has_value());
return { m_type, release_value(), move(m_target) };
}
// 6.2.3.4 UpdateEmpty ( completionRecord, value ), https://tc39.es/ecma262/#sec-updateempty
Completion update_empty(Optional<Value> value) const
{
// 1. Assert: If completionRecord.[[Type]] is either return or throw, then completionRecord.[[Value]] is not empty.
if (m_type == Type::Return || m_type == Type::Throw)
VERIFY(m_value.has_value());
// 2. If completionRecord.[[Value]] is not empty, return ? completionRecord.
if (m_value.has_value())
return *this;
// 3. Return Completion Record { [[Type]]: completionRecord.[[Type]], [[Value]]: value, [[Target]]: completionRecord.[[Target]] }.
return { m_type, move(value), m_target };
}
private:
class EmptyTag {
};
friend AK::Optional<Completion>;
Completion(EmptyTag)
: m_type(Type::Empty)
{
}
bool is_empty() const
{
return m_type == Type::Empty;
}
Type m_type { Type::Normal }; // [[Type]]
Optional<Value> m_value; // [[Value]]
Optional<FlyString> m_target; // [[Target]]
};
}
namespace AK {
template<>
class Optional<JS::Completion> {
template<typename U>
friend class Optional;
public:
using ValueType = JS::Completion;
Optional() = default;
Optional(Optional<JS::Completion> const& other)
{
if (other.has_value())
m_value = other.m_value;
}
Optional(Optional&& other)
: m_value(move(other.m_value))
{
}
template<typename U = JS::Completion>
explicit(!IsConvertible<U&&, JS::Completion>) Optional(U&& value)
requires(!IsSame<RemoveCVReference<U>, Optional<JS::Completion>> && IsConstructible<JS::Completion, U &&>)
: m_value(forward<U>(value))
{
}
Optional& operator=(Optional const& other)
{
if (this != &other) {
clear();
m_value = other.m_value;
}
return *this;
}
Optional& operator=(Optional&& other)
{
if (this != &other) {
clear();
m_value = other.m_value;
}
return *this;
}
void clear()
{
m_value = JS::Completion(JS::Completion::EmptyTag {});
}
[[nodiscard]] bool has_value() const
{
return !m_value.is_empty();
}
[[nodiscard]] JS::Completion& value() &
{
VERIFY(has_value());
return m_value;
}
[[nodiscard]] JS::Completion const& value() const&
{
VERIFY(has_value());
return m_value;
}
[[nodiscard]] JS::Completion value() &&
{
return release_value();
}
[[nodiscard]] JS::Completion release_value()
{
VERIFY(has_value());
JS::Completion released_value = m_value;
clear();
return released_value;
}
JS::Completion value_or(JS::Completion const& fallback) const&
{
if (has_value())
return value();
return fallback;
}
[[nodiscard]] JS::Completion value_or(JS::Completion&& fallback) &&
{
if (has_value())
return value();
return fallback;
}
JS::Completion const& operator*() const { return value(); }
JS::Completion& operator*() { return value(); }
JS::Completion const* operator->() const { return &value(); }
JS::Completion* operator->() { return &value(); }
private:
JS::Completion m_value { JS::Completion::EmptyTag {} };
};
}
namespace JS {
template<typename ValueType>
class [[nodiscard]] ThrowCompletionOr {
public:
ThrowCompletionOr()
requires(IsSame<ValueType, Empty>)
: m_value(Empty {})
{
}
// Not `explicit` on purpose so that `return vm.throw_completion<Error>(...);` is possible.
ThrowCompletionOr(Completion throw_completion)
: m_throw_completion(move(throw_completion))
{
VERIFY(m_throw_completion->is_error());
}
// Not `explicit` on purpose so that `return value;` is possible.
ThrowCompletionOr(ValueType value)
: m_value(move(value))
{
if constexpr (IsSame<ValueType, Value>)
VERIFY(!m_value->is_empty());
}
ThrowCompletionOr(ThrowCompletionOr const&) = default;
ThrowCompletionOr& operator=(ThrowCompletionOr const&) = default;
ThrowCompletionOr(ThrowCompletionOr&&) = default;
ThrowCompletionOr& operator=(ThrowCompletionOr&&) = default;
// Allows implicit construction of ThrowCompletionOr<T> from a type U if T(U) is a supported constructor.
// Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
// Disabled for POD types to avoid weird conversion shenanigans.
template<typename WrappedValueType>
ThrowCompletionOr(WrappedValueType const& value)
requires(!IsPOD<ValueType>)
: m_value(value)
{
}
[[nodiscard]] bool is_throw_completion() const { return m_throw_completion.has_value(); }
Completion const& throw_completion() const { return *m_throw_completion; }
[[nodiscard]] bool has_value() const
requires(!IsSame<ValueType, Empty>)
{
return m_value.has_value();
}
[[nodiscard]] ValueType const& value() const
requires(!IsSame<ValueType, Empty>)
{
return *m_value;
}
// These are for compatibility with the TRY() macro in AK.
[[nodiscard]] bool is_error() const { return m_throw_completion.has_value(); }
[[nodiscard]] ValueType release_value() { return m_value.release_value(); }
Completion release_error() { return m_throw_completion.release_value(); }
private:
Optional<Completion> m_throw_completion;
Optional<ValueType> m_value;
};
template<>
class ThrowCompletionOr<void> : public ThrowCompletionOr<Empty> {
public:
using ThrowCompletionOr<Empty>::ThrowCompletionOr;
};
ThrowCompletionOr<Value> await(VM&, Value);
// 6.2.3.2 NormalCompletion ( value ), https://tc39.es/ecma262/#sec-normalcompletion
inline Completion normal_completion(Optional<Value> value)
{
return { Completion::Type::Normal, move(value), {} };
}
// 6.2.3.3 ThrowCompletion ( value ), https://tc39.es/ecma262/#sec-throwcompletion
Completion throw_completion(Value);
}
|