1 /* vim: set expandtab ts=4 sw=4: */
2 /*
3  * You may redistribute this program and/or modify it under the terms of
4  * the GNU General Public License as published by the Free Software Foundation,
5  * either version 3 of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
14  */
15 #ifndef Er_H
16 #define Er_H
17 
18 #include "memory/Allocator.h"
19 #include "util/Gcc.h"
20 #include "util/Js.h"
21 
22 #include "util/Linker.h"
23 Linker_require("exception/Er.c")
24 
25 struct Er_Ret
26 {
27     const char* message;
28 };
29 
30 Js({ this.Er_JS = require("../exception/Er.js").create(); })
31 
32 #define Er_DEFUN(...) \
33     Gcc_USE_RET Js_or({ return this.Er_JS.defun(Js_Q __VA_ARGS__ Js_Q) }, __VA_ARGS__)
34 
35 Gcc_PRINTF(4, 5)
36 Gcc_USE_RET
37 struct Er_Ret* Er__raise(char* file, int line, struct Allocator* alloc, char* format, ...);
38 #define Er_raise(...) \
39     do { \
40         struct Er_Ret* Er_ret = Er__raise(Gcc_SHORT_FILE, Gcc_LINE, __VA_ARGS__); \
41         Js_or({ return 'return Er_ret;' }, Er__assertFail(Er_ret)); \
42     } while (0)
43     // CHECKFILES_IGNORE missing ;
44 
45 #define Er(expr) Js_or({ return this.Er_JS.er(Js_Q expr Js_Q, Gcc_SHORT_FILE, Gcc_LINE); }, expr)
46 
47 #define Er_assert(expr) \
48     Js_or({ return this.Er_JS.assert(Js_Q expr Js_Q, Gcc_SHORT_FILE, Gcc_LINE); }, expr)
49 
50 #define Er_check(ret, expr) \
51     Js_or({ return this.Er_JS.check(#ret, Js_Q expr Js_Q, Gcc_SHORT_FILE, Gcc_LINE); }, expr)
52 
53 #define Er_ret(val) Js_or({ return this.Er_JS.ret(Js_Q val Js_Q); }, return val)
54 
Er_unwind(const char * file,int line,struct Er_Ret * ret)55 static inline struct Er_Ret* Er_unwind(const char* file, int line, struct Er_Ret* ret)
56 {
57     return ret;
58 }
59 
60 void Er__assertFail(struct Er_Ret* er);
61 
62 #endif
63