Completion.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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. // Temporary helper akin to TRY(), but returning a default-constructed type (e.g. empty JS::Value)
  15. // instead of the throw completion record. Use this as the bridge between functions that have
  16. // already been updated to use completions and functions that haven't.
  17. #define TRY_OR_DISCARD(expression) \
  18. ({ \
  19. auto _temporary_result = (expression); \
  20. if (_temporary_result.is_error()) \
  21. return {}; \
  22. _temporary_result.release_value(); \
  23. })
  24. // MUST() is to the spec's `!` what TRY() is to `?`.
  25. // https://tc39.es/ecma262/#sec-returnifabrupt-shorthands
  26. #define MUST(expression) \
  27. ({ \
  28. auto _temporary_result = (expression); \
  29. VERIFY(!_temporary_result.is_error()); \
  30. /* The return value of "! Something()" is commonly */ \
  31. /* ignored, so we assign to a temporary variable here */ \
  32. /* to avoid having to (void) all the things. */ \
  33. auto _temporary_value = _temporary_result.release_value(); \
  34. move(_temporary_value); \
  35. })
  36. // 6.2.3 The Completion Record Specification Type, https://tc39.es/ecma262/#sec-completion-record-specification-type
  37. class [[nodiscard]] Completion {
  38. public:
  39. enum class Type {
  40. Normal,
  41. Break,
  42. Continue,
  43. Return,
  44. Throw,
  45. };
  46. Completion(Type type, Optional<Value> value, Optional<FlyString> target)
  47. : m_type(type)
  48. , m_value(move(value))
  49. , m_target(move(target))
  50. {
  51. if (m_value.has_value())
  52. VERIFY(!m_value->is_empty());
  53. }
  54. // 5.2.3.1 Implicit Completion Values, https://tc39.es/ecma262/#sec-implicit-completion-values
  55. // Not `explicit` on purpose.
  56. Completion(Value value)
  57. : Completion(Type::Normal, value, {})
  58. {
  59. }
  60. Completion()
  61. : Completion(js_undefined())
  62. {
  63. }
  64. [[nodiscard]] Type type() const { return m_type; }
  65. [[nodiscard]] bool has_value() const { return m_value.has_value(); }
  66. [[nodiscard]] Value value() const { return *m_value; }
  67. [[nodiscard]] bool has_target() const { return m_target.has_value(); }
  68. [[nodiscard]] FlyString const& target() const { return *m_target; }
  69. // "abrupt completion refers to any completion with a [[Type]] value other than normal"
  70. [[nodiscard]] bool is_abrupt() const { return m_type != Type::Normal; }
  71. // These are for compatibility with the TRY() macro in AK.
  72. [[nodiscard]] bool is_error() const { return m_type == Type::Throw; }
  73. [[nodiscard]] Value release_value() { return m_value.release_value(); }
  74. Completion release_error()
  75. {
  76. VERIFY(is_error());
  77. return { m_type, release_value(), move(m_target) };
  78. }
  79. // 6.2.3.4 UpdateEmpty ( completionRecord, value ), https://tc39.es/ecma262/#sec-updateempty
  80. Completion update_empty(Value value) const
  81. {
  82. // 1. Assert: If completionRecord.[[Type]] is either return or throw, then completionRecord.[[Value]] is not empty.
  83. if (m_type == Type::Return || m_type == Type::Throw)
  84. VERIFY(m_value.has_value());
  85. // 2. If completionRecord.[[Value]] is not empty, return Completion(completionRecord).
  86. if (m_value.has_value())
  87. return *this;
  88. // 3. Return Completion { [[Type]]: completionRecord.[[Type]], [[Value]]: value, [[Target]]: completionRecord.[[Target]] }.
  89. return { m_type, value, m_target };
  90. }
  91. private:
  92. Type m_type { Type::Normal }; // [[Type]]
  93. Optional<Value> m_value; // [[Value]]
  94. Optional<FlyString> m_target; // [[Target]]
  95. };
  96. template<typename ValueType>
  97. class [[nodiscard]] ThrowCompletionOr {
  98. public:
  99. ThrowCompletionOr() requires(IsSame<ValueType, Empty>)
  100. : m_value(Empty {})
  101. {
  102. }
  103. // Not `explicit` on purpose so that `return vm.throw_completion<Error>(...);` is possible.
  104. ThrowCompletionOr(Completion throw_completion)
  105. : m_throw_completion(move(throw_completion))
  106. {
  107. VERIFY(throw_completion.is_error());
  108. }
  109. // Not `explicit` on purpose so that `return value;` is possible.
  110. ThrowCompletionOr(ValueType value)
  111. : m_value(move(value))
  112. {
  113. if constexpr (IsSame<ValueType, Value>)
  114. VERIFY(!value.is_empty());
  115. }
  116. // Allows implicit construction of ThrowCompletionOr<T> from a type U if T(U) is a supported constructor.
  117. // Most commonly: Value from Object* or similar, so we can omit the curly braces from "return { TRY(...) };".
  118. // Disabled for POD types to avoid weird conversion shenanigans.
  119. template<typename WrappedValueType>
  120. ThrowCompletionOr(WrappedValueType value) requires(!IsPOD<ValueType>)
  121. : m_value(move(value))
  122. {
  123. }
  124. [[nodiscard]] bool is_throw_completion() const { return m_throw_completion.has_value(); }
  125. Completion const& throw_completion() const { return *m_throw_completion; }
  126. [[nodiscard]] bool has_value() const requires(!IsSame<ValueType, Empty>) { return m_value.has_value(); }
  127. [[nodiscard]] ValueType const& value() const requires(!IsSame<ValueType, Empty>) { return *m_value; }
  128. // These are for compatibility with the TRY() macro in AK.
  129. [[nodiscard]] bool is_error() const { return m_throw_completion.has_value(); }
  130. [[nodiscard]] ValueType release_value() { return m_value.release_value(); }
  131. Completion release_error() { return m_throw_completion.release_value(); }
  132. private:
  133. Optional<Completion> m_throw_completion;
  134. Optional<ValueType> m_value;
  135. };
  136. template<>
  137. class ThrowCompletionOr<void> : public ThrowCompletionOr<Empty> {
  138. public:
  139. using ThrowCompletionOr<Empty>::ThrowCompletionOr;
  140. };
  141. // 6.2.3.2 NormalCompletion ( value ), https://tc39.es/ecma262/#sec-normalcompletion
  142. inline Completion normal_completion(Optional<Value> value)
  143. {
  144. return { Completion::Type::Normal, value, {} };
  145. }
  146. // 6.2.3.3 ThrowCompletion ( value ), https://tc39.es/ecma262/#sec-throwcompletion
  147. inline Completion throw_completion(Value value)
  148. {
  149. return { Completion::Type::Throw, value, {} };
  150. }
  151. }