From 52b76060f9cea7e045419290e04d861f7125486d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 20 Jan 2023 14:24:49 -0500 Subject: [PATCH] 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. --- Userland/Libraries/LibJS/Runtime/Completion.h | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 #include #include +#include #include #include #include @@ -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(_completion.value()->as_object())); \ + \ + return _completion; \ + } \ + static_assert(!::AK::Detail::IsLvalueReference, \ + "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: