summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-01-20 14:24:49 -0500
committerLinus Groh <mail@linusgroh.de>2023-01-20 20:31:38 +0000
commit52b76060f9cea7e045419290e04d861f7125486d (patch)
tree23931cbc1047b406f254f98138cdf9ed1b42a7fd /Userland
parentf1de4f88720cd33acc2042eee7b4f07db560d02f (diff)
downloadserenity-52b76060f9cea7e045419290e04d861f7125486d.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Completion.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Completion.h b/Userland/Libraries/LibJS/Runtime/Completion.h
index 2d61864e5a..b052273e5f 100644
--- a/Userland/Libraries/LibJS/Runtime/Completion.h
+++ b/Userland/Libraries/LibJS/Runtime/Completion.h
@@ -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: