1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef frontend_BinAST_macros_h
8 #define frontend_BinAST_macros_h
9 
10 #include "vm/JSContext.h"
11 
12 // Evaluate an expression EXPR, checking that the result is not falsy.
13 //
14 // Throw `cx->alreadyReportedError()` if it returns 0/nullptr.
15 #define BINJS_TRY(EXPR)                   \
16   do {                                    \
17     if (MOZ_UNLIKELY(!(EXPR))) {          \
18       return cx_->alreadyReportedError(); \
19     }                                     \
20   } while (false)
21 
22 // Evaluate an expression EXPR, checking that the result is not falsy.
23 // In case of success, assign the result to VAR.
24 //
25 // Throw `cx->alreadyReportedError()` if it returns 0/nullptr.
26 #define BINJS_TRY_VAR(VAR, EXPR)          \
27   do {                                    \
28     VAR = (EXPR);                         \
29     if (MOZ_UNLIKELY(!(VAR))) {           \
30       return cx_->alreadyReportedError(); \
31     }                                     \
32   } while (false)
33 
34 // Evaluate an expression EXPR, checking that the result is not falsy.
35 // In case of success, assign the result to a new variable VAR.
36 //
37 // Throw `cx->alreadyReportedError()` if it returns 0/nullptr.
38 #define BINJS_TRY_DECL(VAR, EXPR)       \
39   auto VAR = (EXPR);                    \
40   if (MOZ_UNLIKELY(!(VAR))) {           \
41     return cx_->alreadyReportedError(); \
42   }
43 
44 #define BINJS_TRY_EMPL(VAR, EXPR)           \
45   do {                                      \
46     auto _tryEmplResult = (EXPR);           \
47     if (MOZ_UNLIKELY(!_tryEmplResult)) {    \
48       return cx_->alreadyReportedError();   \
49     }                                       \
50     (VAR).emplace(_tryEmplResult.unwrap()); \
51   } while (false)
52 
53 #define BINJS_MOZ_TRY_EMPLACE(VAR, EXPR)                 \
54   do {                                                   \
55     auto _tryEmplResult = (EXPR);                        \
56     if (MOZ_UNLIKELY(_tryEmplResult.isErr())) {          \
57       return ::mozilla::Err(_tryEmplResult.unwrapErr()); \
58     }                                                    \
59     (VAR).emplace(_tryEmplResult.unwrap());              \
60   } while (false)
61 
62 // Evaluate an expression EXPR, checking that the result is a success.
63 // In case of success, unwrap and assign the result to a new variable VAR.
64 //
65 // In case of error, propagate the error.
66 #define BINJS_MOZ_TRY_DECL(VAR, EXPR)          \
67   auto _##VAR = (EXPR);                        \
68   if (MOZ_UNLIKELY(_##VAR.isErr())) {          \
69     return ::mozilla::Err(_##VAR.unwrapErr()); \
70   }                                            \
71   auto VAR = _##VAR.unwrap();
72 
73 #endif  // frontend_BinAST_macros_h
74