summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-10-03 15:09:28 +0100
committerLinus Groh <mail@linusgroh.de>2021-10-03 20:14:03 +0100
commit7cd3f7de614271464ada8fa5034a41132b724570 (patch)
tree7b38e80b2b10772d3b9dbbafda3e3ed992ef2313
parent8f722302d9c038ae9c307bc553e55b8dbbcff7ed (diff)
downloadserenity-7cd3f7de614271464ada8fa5034a41132b724570.zip
LibJS: Add a MUST() macro, like TRY() but for the spec's `!` shortcut
-rw-r--r--Userland/Libraries/LibJS/Runtime/Completion.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Completion.h b/Userland/Libraries/LibJS/Runtime/Completion.h
index 7b6dbb7c3c..3a9b62b4b7 100644
--- a/Userland/Libraries/LibJS/Runtime/Completion.h
+++ b/Userland/Libraries/LibJS/Runtime/Completion.h
@@ -26,6 +26,19 @@ namespace JS {
_temporary_result.release_value(); \
})
+// MUST() is to the spec's `!` what TRY() is to `?`.
+// https://tc39.es/ecma262/#sec-returnifabrupt-shorthands
+#define MUST(expression) \
+ ({ \
+ auto _temporary_result = (expression); \
+ VERIFY(!_temporary_result.is_error()); \
+ /* The return value of "! Something()" is commonly */ \
+ /* ignored, so we assign to a temporary variable here */ \
+ /* to avoid having to (void) all the things. */ \
+ auto _temporary_value = _temporary_result.release_value(); \
+ move(_temporary_value); \
+ })
+
// 6.2.3 The Completion Record Specification Type, https://tc39.es/ecma262/#sec-completion-record-specification-type
class [[nodiscard]] Completion {
public: