Completion.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Normal,
  19. Break,
  20. Continue,
  21. Return,
  22. Throw,
  23. };
  24. ALWAYS_INLINE Completion(Type type, Optional<Value> value, Optional<FlyString> target)
  25. : m_type(type)
  26. , m_value(move(value))
  27. , m_target(move(target))
  28. {
  29. if (m_value.has_value())
  30. VERIFY(!m_value->is_empty());
  31. }
  32. Completion(ThrowCompletionOr<Value> const&);
  33. // 5.2.3.1 Implicit Completion Values, https://tc39.es/ecma262/#sec-implicit-completion-values
  34. // Not `explicit` on purpose.
  35. ALWAYS_INLINE Completion(Value value)
  36. : Completion(Type::Normal, value, {})
  37. {
  38. }
  39. ALWAYS_INLINE Completion()
  40. : Completion(js_undefined())
  41. {
  42. }
  43. Completion(Completion const&) = default;
  44. Completion& operator=(Completion const&) = default;
  45. Completion(Completion&&) = default;
  46. Completion& operator=(Completion&&) = default;
  47. [[nodiscard]] Type type() const { return m_type; }
  48. [[nodiscard]] Optional<Value>& value() { return m_value; }
  49. [[nodiscard]] Optional<Value> const& value() const { return m_value; }
  50. [[nodiscard]] Optional<FlyString>& target() { return m_target; }
  51. [[nodiscard]] Optional<FlyString> const& target() const { return m_target; }
  52. // "abrupt completion refers to any completion with a [[Type]] value other than normal"
  53. [[nodiscard]] bool is_abrupt() const { return m_type != Type::Normal; }
  54. // These are for compatibility with the TRY() macro in AK.
  55. [[nodiscard]] bool is_error() const { return m_type == Type::Throw; }
  56. [[nodiscard]] Optional<Value> release_value() { return move(m_value); }
  57. Completion release_error()
  58. {
  59. VERIFY(is_error());
  60. VERIFY(m_value.has_value());
  61. return { m_type, release_value(), move(m_target) };
  62. }
  63. // 6.2.3.4 UpdateEmpty ( completionRecord, value ), https://tc39.es/ecma262/#sec-updateempty
  64. Completion update_empty(Optional<Value> value) const
  65. {
  66. // 1. Assert: If completionRecord.[[Type]] is either return or throw, then completionRecord.[[Value]] is not empty.
  67. if (m_type == Type::Return || m_type == Type::Throw)
  68. VERIFY(m_value.has_value());
  69. // 2. If completionRecord.[[Value]] is not empty, return Completion(completionRecord).
  70. if (m_value.has_value())
  71. return *this;
  72. // 3. Return Completion { [[Type]]: completionRecord.[[Type]], [[Value]]: value, [[Target]]: completionRecord.[[Target]] }.
  73. return { m_type, move(value), m_target };
  74. }
  75. private:
  76. Type m_type { Type::Normal }; // [[Type]]
  77. Optional<Value> m_value; // [[Value]]
  78. Optional<FlyString> m_target; // [[Target]]
  79. };
  80. template<typename ValueType>
  81. class [[nodiscard]] ThrowCompletionOr {
  82. public:
  83. ThrowCompletionOr() requires(IsSame<ValueType, Empty>)
  84. : m_value(Empty {})
  85. {
  86. }
  87. // Not `explicit` on purpose so that `return vm.throw_completion<Error>(...);` is possible.
  88. ThrowCompletionOr(Completion throw_completion)
  89. : m_throw_completion(move(throw_completion))
  90. {
  91. VERIFY(m_throw_completion->is_error());
  92. }
  93. // Not `explicit` on purpose so that `return value;` is possible.
  94. ThrowCompletionOr(ValueType value)
  95. : m_value(move(value))
  96. {
  97. if constexpr (IsSame<ValueType, Value>)
  98. VERIFY(!m_value->is_empty());
  99. }
  100. ThrowCompletionOr(ThrowCompletionOr const&) = default;
  101. ThrowCompletionOr& operator=(ThrowCompletionOr const&) = default;
  102. ThrowCompletionOr(ThrowCompletionOr&&) = default;
  103. ThrowCompletionOr& operator=(ThrowCompletionOr&&) = default;
  104. // Allows implicit construction of ThrowCompletionOr<T> from a type U if T(U) is a supported constructor.
  105. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
  106. // Disabled for POD types to avoid weird conversion shenanigans.
  107. template<typename WrappedValueType>
  108. ThrowCompletionOr(WrappedValueType value) requires(!IsPOD<ValueType>)
  109. : m_value(move(value))
  110. {
  111. }
  112. [[nodiscard]] bool is_throw_completion() const { return m_throw_completion.has_value(); }
  113. Completion const& throw_completion() const { return *m_throw_completion; }
  114. [[nodiscard]] bool has_value() const requires(!IsSame<ValueType, Empty>) { return m_value.has_value(); }
  115. [[nodiscard]] ValueType const& value() const requires(!IsSame<ValueType, Empty>) { return *m_value; }
  116. // These are for compatibility with the TRY() macro in AK.
  117. [[nodiscard]] bool is_error() const { return m_throw_completion.has_value(); }
  118. [[nodiscard]] ValueType release_value() { return m_value.release_value(); }
  119. Completion release_error() { return m_throw_completion.release_value(); }
  120. private:
  121. Optional<Completion> m_throw_completion;
  122. Optional<ValueType> m_value;
  123. };
  124. template<>
  125. class ThrowCompletionOr<void> : public ThrowCompletionOr<Empty> {
  126. public:
  127. using ThrowCompletionOr<Empty>::ThrowCompletionOr;
  128. };
  129. ThrowCompletionOr<Value> await(GlobalObject&, Value);
  130. // 6.2.3.2 NormalCompletion ( value ), https://tc39.es/ecma262/#sec-normalcompletion
  131. inline Completion normal_completion(Optional<Value> value)
  132. {
  133. return { Completion::Type::Normal, move(value), {} };
  134. }
  135. // 6.2.3.3 ThrowCompletion ( value ), https://tc39.es/ecma262/#sec-throwcompletion
  136. inline Completion throw_completion(Value value)
  137. {
  138. return { Completion::Type::Throw, value, {} };
  139. }
  140. }