1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:07:46 +00:00

LibJS: Add a macro for infallible operations that may throw OOM

If a spec step hasn't been marked as fallible, but might throw due to
OOM, this is to make it clear that OOM is the only thing that may cause
a failure.
This commit is contained in:
Timothy Flynn 2023-01-20 14:24:49 -05:00 committed by Linus Groh
parent f1de4f8872
commit 52b76060f9

View file

@ -10,6 +10,7 @@
#include <AK/DeprecatedFlyString.h>
#include <AK/Optional.h>
#include <AK/Try.h>
#include <AK/TypeCasts.h>
#include <AK/Variant.h>
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/Value.h>
@ -30,6 +31,26 @@ namespace JS {
_temporary_result.release_value(); \
})
#define MUST_OR_THROW_OOM(expression) \
({ \
/* Ignore -Wshadow to allow nesting the macro. */ \
AK_IGNORE_DIAGNOSTIC("-Wshadow", \
auto _temporary_result = (expression)); \
if (_temporary_result.is_error()) { \
auto _completion = _temporary_result.release_error(); \
\
/* We can't explicitly check for OOM because InternalError does not store the ErrorType */ \
VERIFY(_completion.value().has_value()); \
VERIFY(_completion.value()->is_object()); \
VERIFY(is<JS::InternalError>(_completion.value()->as_object())); \
\
return _completion; \
} \
static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_result.release_value())>, \
"Do not return a reference from a fallible expression"); \
_temporary_result.release_value(); \
})
// 6.2.3 The Completion Record Specification Type, https://tc39.es/ecma262/#sec-completion-record-specification-type
class [[nodiscard]] Completion {
public: