Completion.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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()
  110. : m_value(JS::Completion(JS::Completion::EmptyTag {}))
  111. {
  112. }
  113. Optional(Optional<JS::Completion> const& other)
  114. {
  115. if (other.has_value())
  116. m_value = other.m_value;
  117. }
  118. Optional(Optional&& other)
  119. : m_value(other.m_value)
  120. {
  121. }
  122. template<typename U = JS::Completion>
  123. explicit(!IsConvertible<U&&, JS::Completion>) Optional(U&& value) requires(!IsSame<RemoveCVReference<U>, Optional<JS::Completion>> && IsConstructible<JS::Completion, U&&>)
  124. : m_value(forward<U>(value))
  125. {
  126. }
  127. Optional& operator=(Optional const& other)
  128. {
  129. if (this != &other) {
  130. clear();
  131. m_value = other.m_value;
  132. }
  133. return *this;
  134. }
  135. Optional& operator=(Optional&& other)
  136. {
  137. if (this != &other) {
  138. clear();
  139. m_value = other.m_value;
  140. }
  141. return *this;
  142. }
  143. void clear()
  144. {
  145. m_value = JS::Completion(JS::Completion::EmptyTag {});
  146. }
  147. [[nodiscard]] bool has_value() const
  148. {
  149. return !m_value.is_empty();
  150. }
  151. [[nodiscard]] JS::Completion& value() &
  152. {
  153. VERIFY(has_value());
  154. return m_value;
  155. }
  156. [[nodiscard]] JS::Completion const& value() const&
  157. {
  158. VERIFY(has_value());
  159. return m_value;
  160. }
  161. [[nodiscard]] JS::Completion value() &&
  162. {
  163. return release_value();
  164. }
  165. [[nodiscard]] JS::Completion release_value()
  166. {
  167. VERIFY(has_value());
  168. JS::Completion released_value = m_value;
  169. clear();
  170. return released_value;
  171. }
  172. JS::Completion value_or(JS::Completion const& fallback) const&
  173. {
  174. if (has_value())
  175. return value();
  176. return fallback;
  177. }
  178. [[nodiscard]] JS::Completion value_or(JS::Completion&& fallback) &&
  179. {
  180. if (has_value())
  181. return value();
  182. return fallback;
  183. }
  184. JS::Completion const& operator*() const { return value(); }
  185. JS::Completion& operator*() { return value(); }
  186. JS::Completion const* operator->() const { return &value(); }
  187. JS::Completion* operator->() { return &value(); }
  188. private:
  189. JS::Completion m_value;
  190. };
  191. }
  192. namespace JS {
  193. template<typename ValueType>
  194. class [[nodiscard]] ThrowCompletionOr {
  195. public:
  196. ThrowCompletionOr() requires(IsSame<ValueType, Empty>)
  197. : m_value(Empty {})
  198. {
  199. }
  200. // Not `explicit` on purpose so that `return vm.throw_completion<Error>(...);` is possible.
  201. ThrowCompletionOr(Completion throw_completion)
  202. : m_throw_completion(move(throw_completion))
  203. {
  204. VERIFY(m_throw_completion->is_error());
  205. }
  206. // Not `explicit` on purpose so that `return value;` is possible.
  207. ThrowCompletionOr(ValueType value)
  208. : m_value(move(value))
  209. {
  210. if constexpr (IsSame<ValueType, Value>)
  211. VERIFY(!m_value->is_empty());
  212. }
  213. ThrowCompletionOr(ThrowCompletionOr const&) = default;
  214. ThrowCompletionOr& operator=(ThrowCompletionOr const&) = default;
  215. ThrowCompletionOr(ThrowCompletionOr&&) = default;
  216. ThrowCompletionOr& operator=(ThrowCompletionOr&&) = default;
  217. // Allows implicit construction of ThrowCompletionOr<T> from a type U if T(U) is a supported constructor.
  218. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
  219. // Disabled for POD types to avoid weird conversion shenanigans.
  220. template<typename WrappedValueType>
  221. ThrowCompletionOr(WrappedValueType value) requires(!IsPOD<ValueType>)
  222. : m_value(move(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 requires(!IsSame<ValueType, Empty>) { return m_value.has_value(); }
  228. [[nodiscard]] ValueType const& value() const requires(!IsSame<ValueType, Empty>) { return *m_value; }
  229. // These are for compatibility with the TRY() macro in AK.
  230. [[nodiscard]] bool is_error() const { return m_throw_completion.has_value(); }
  231. [[nodiscard]] ValueType release_value() { return m_value.release_value(); }
  232. Completion release_error() { return m_throw_completion.release_value(); }
  233. private:
  234. Optional<Completion> m_throw_completion;
  235. Optional<ValueType> m_value;
  236. };
  237. template<>
  238. class ThrowCompletionOr<void> : public ThrowCompletionOr<Empty> {
  239. public:
  240. using ThrowCompletionOr<Empty>::ThrowCompletionOr;
  241. };
  242. ThrowCompletionOr<Value> await(VM&, Value);
  243. // 6.2.3.2 NormalCompletion ( value ), https://tc39.es/ecma262/#sec-normalcompletion
  244. inline Completion normal_completion(Optional<Value> value)
  245. {
  246. return { Completion::Type::Normal, move(value), {} };
  247. }
  248. // 6.2.3.3 ThrowCompletion ( value ), https://tc39.es/ecma262/#sec-throwcompletion
  249. Completion throw_completion(Value);
  250. }