mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:37:34 +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:
parent
f1de4f8872
commit
52b76060f9
1 changed files with 21 additions and 0 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue