Completion.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/Optional.h>
  10. #include <AK/Try.h>
  11. #include <AK/Variant.h>
  12. #include <LibJS/Runtime/Value.h>
  13. namespace JS {
  14. // 6.2.3 The Completion Record Specification Type, https://tc39.es/ecma262/#sec-completion-record-specification-type
  15. class [[nodiscard]] Completion {
  16. public:
  17. enum class Type {
  18. Empty,
  19. Normal,
  20. Break,
  21. Continue,
  22. Return,
  23. Throw,
  24. };
  25. ALWAYS_INLINE Completion(Type type, Optional<Value> value, Optional<FlyString> target)
  26. : m_type(type)
  27. , m_value(move(value))
  28. , m_target(move(target))
  29. {
  30. VERIFY(type != Type::Empty);
  31. if (m_value.has_value())
  32. VERIFY(!m_value->is_empty());
  33. }
  34. Completion(ThrowCompletionOr<Value> const&);
  35. // 5.2.3.1 Implicit Completion Values, https://tc39.es/ecma262/#sec-implicit-completion-values
  36. // Not `explicit` on purpose.
  37. ALWAYS_INLINE Completion(Value value)
  38. : Completion(Type::Normal, value, {})
  39. {
  40. }
  41. ALWAYS_INLINE Completion(Optional<Value> value)
  42. : Completion(Type::Normal, move(value), {})
  43. {
  44. }
  45. ALWAYS_INLINE Completion()
  46. : Completion(js_undefined())
  47. {
  48. }
  49. Completion(Completion const&) = default;
  50. Completion& operator=(Completion const&) = default;
  51. Completion(Completion&&) = default;
  52. Completion& operator=(Completion&&) = default;
  53. [[nodiscard]] Type type() const
  54. {
  55. VERIFY(m_type != Type::Empty);
  56. return m_type;
  57. }
  58. [[nodiscard]] Optional<Value>& value() { return m_value; }
  59. [[nodiscard]] Optional<Value> const& value() const { return m_value; }
  60. [[nodiscard]] Optional<FlyString>& target() { return m_target; }
  61. [[nodiscard]] Optional<FlyString> const& target() const { return m_target; }
  62. // "abrupt completion refers to any completion with a [[Type]] value other than normal"
  63. [[nodiscard]] bool is_abrupt() const { return m_type != Type::Normal; }
  64. // These are for compatibility with the TRY() macro in AK.
  65. [[nodiscard]] bool is_error() const { return m_type == Type::Throw; }
  66. [[nodiscard]] Optional<Value> release_value() { return move(m_value); }
  67. Completion release_error()
  68. {
  69. VERIFY(is_error());
  70. VERIFY(m_value.has_value());
  71. return { m_type, release_value(), move(m_target) };
  72. }
  73. // 6.2.3.4 UpdateEmpty ( completionRecord, value ), https://tc39.es/ecma262/#sec-updateempty
  74. Completion update_empty(Optional<Value> value) const
  75. {
  76. // 1. Assert: If completionRecord.[[Type]] is either return or throw, then completionRecord.[[Value]] is not empty.
  77. if (m_type == Type::Return || m_type == Type::Throw)
  78. VERIFY(m_value.has_value());
  79. // 2. If completionRecord.[[Value]] is not empty, return ? completionRecord.
  80. if (m_value.has_value())
  81. return *this;
  82. // 3. Return Completion Record { [[Type]]: completionRecord.[[Type]], [[Value]]: value, [[Target]]: completionRecord.[[Target]] }.
  83. return { m_type, move(value), m_target };
  84. }
  85. private:
  86. class EmptyTag {
  87. };
  88. friend AK::Optional<Completion>;
  89. Completion(EmptyTag)
  90. : m_type(Type::Empty)
  91. {
  92. }
  93. bool is_empty() const
  94. {
  95. return m_type == Type::Empty;
  96. }
  97. Type m_type { Type::Normal }; // [[Type]]
  98. Optional<Value> m_value; // [[Value]]
  99. Optional<FlyString> m_target; // [[Target]]
  100. };
  101. }
  102. namespace AK {
  103. template<>
  104. class Optional<JS::Completion> {
  105. template<typename U>
  106. friend class Optional;
  107. public:
  108. using ValueType = JS::Completion;
  109. Optional() = default;
  110. Optional(Optional<JS::Completion> const& other)
  111. {
  112. if (other.has_value())
  113. m_value = other.m_value;
  114. }
  115. Optional(Optional&& other)
  116. : m_value(move(other.m_value))
  117. {
  118. }
  119. template<typename U = JS::Completion>
  120. explicit(!IsConvertible<U&&, JS::Completion>) Optional(U&& value)
  121. requires(!IsSame<RemoveCVReference<U>, Optional<JS::Completion>> && IsConstructible<JS::Completion, U &&>)
  122. : m_value(forward<U>(value))
  123. {
  124. }
  125. Optional& operator=(Optional const& other)
  126. {
  127. if (this != &other) {
  128. clear();
  129. m_value = other.m_value;
  130. }
  131. return *this;
  132. }
  133. Optional& operator=(Optional&& other)
  134. {
  135. if (this != &other) {
  136. clear();
  137. m_value = other.m_value;
  138. }
  139. return *this;
  140. }
  141. void clear()
  142. {
  143. m_value = JS::Completion(JS::Completion::EmptyTag {});
  144. }
  145. [[nodiscard]] bool has_value() const
  146. {
  147. return !m_value.is_empty();
  148. }
  149. [[nodiscard]] JS::Completion& value() &
  150. {
  151. VERIFY(has_value());
  152. return m_value;
  153. }
  154. [[nodiscard]] JS::Completion const& value() const&
  155. {
  156. VERIFY(has_value());
  157. return m_value;
  158. }
  159. [[nodiscard]] JS::Completion value() &&
  160. {
  161. return release_value();
  162. }
  163. [[nodiscard]] JS::Completion release_value()
  164. {
  165. VERIFY(has_value());
  166. JS::Completion released_value = m_value;
  167. clear();
  168. return released_value;
  169. }
  170. JS::Completion value_or(JS::Completion const& fallback) const&
  171. {
  172. if (has_value())
  173. return value();
  174. return fallback;
  175. }
  176. [[nodiscard]] JS::Completion value_or(JS::Completion&& fallback) &&
  177. {
  178. if (has_value())
  179. return value();
  180. return fallback;
  181. }
  182. JS::Completion const& operator*() const { return value(); }
  183. JS::Completion& operator*() { return value(); }
  184. JS::Completion const* operator->() const { return &value(); }
  185. JS::Completion* operator->() { return &value(); }
  186. private:
  187. JS::Completion m_value { JS::Completion::EmptyTag {} };
  188. };
  189. }
  190. namespace JS {
  191. template<typename ValueType>
  192. class [[nodiscard]] ThrowCompletionOr {
  193. public:
  194. ThrowCompletionOr()
  195. requires(IsSame<ValueType, Empty>)
  196. : m_value(Empty {})
  197. {
  198. }
  199. // Not `explicit` on purpose so that `return vm.throw_completion<Error>(...);` is possible.
  200. ThrowCompletionOr(Completion throw_completion)
  201. : m_throw_completion(move(throw_completion))
  202. {
  203. VERIFY(m_throw_completion->is_error());
  204. }
  205. // Not `explicit` on purpose so that `return value;` is possible.
  206. ThrowCompletionOr(ValueType value)
  207. : m_value(move(value))
  208. {
  209. if constexpr (IsSame<ValueType, Value>)
  210. VERIFY(!m_value->is_empty());
  211. }
  212. ThrowCompletionOr(ThrowCompletionOr const&) = default;
  213. ThrowCompletionOr& operator=(ThrowCompletionOr const&) = default;
  214. ThrowCompletionOr(ThrowCompletionOr&&) = default;
  215. ThrowCompletionOr& operator=(ThrowCompletionOr&&) = default;
  216. // Allows implicit construction of ThrowCompletionOr<T> from a type U if T(U) is a supported constructor.
  217. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
  218. // Disabled for POD types to avoid weird conversion shenanigans.
  219. template<typename WrappedValueType>
  220. ThrowCompletionOr(WrappedValueType const& value)
  221. requires(!IsPOD<ValueType>)
  222. : m_value(value)
  223. {
  224. }
  225. [[nodiscard]] bool is_throw_completion() const { return m_throw_completion.has_value(); }
  226. Completion const& throw_completion() const { return *m_throw_completion; }
  227. [[nodiscard]] bool has_value() const
  228. requires(!IsSame<ValueType, Empty>)
  229. {
  230. return m_value.has_value();
  231. }
  232. [[nodiscard]] ValueType const& value() const
  233. requires(!IsSame<ValueType, Empty>)
  234. {
  235. return *m_value;
  236. }
  237. // These are for compatibility with the TRY() macro in AK.
  238. [[nodiscard]] bool is_error() const { return m_throw_completion.has_value(); }
  239. [[nodiscard]] ValueType release_value() { return m_value.release_value(); }
  240. Completion release_error() { return m_throw_completion.release_value(); }
  241. private:
  242. Optional<Completion> m_throw_completion;
  243. Optional<ValueType> m_value;
  244. };
  245. template<>
  246. class ThrowCompletionOr<void> : public ThrowCompletionOr<Empty> {
  247. public:
  248. using ThrowCompletionOr<Empty>::ThrowCompletionOr;
  249. };
  250. ThrowCompletionOr<Value> await(VM&, Value);
  251. // 6.2.3.2 NormalCompletion ( value ), https://tc39.es/ecma262/#sec-normalcompletion
  252. inline Completion normal_completion(Optional<Value> value)
  253. {
  254. return { Completion::Type::Normal, move(value), {} };
  255. }
  256. // 6.2.3.3 ThrowCompletion ( value ), https://tc39.es/ecma262/#sec-throwcompletion
  257. Completion throw_completion(Value);
  258. }