]> git.saurik.com Git - cycript.git/blob - Exception.hpp
73287ddd17b9797676df52a681057ebfbc448dd8
[cycript.git] / Exception.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_EXCEPTION_HPP
23 #define CYCRIPT_EXCEPTION_HPP
24
25 #ifdef CY_EXECUTE
26 #include <JavaScriptCore/JSBase.h>
27 #endif
28
29 // XXX: does _assert really need this?
30 #include <errno.h>
31
32 #include "Standard.hpp"
33
34 class CYPool;
35
36 struct CYException {
37 virtual ~CYException() {
38 }
39
40 virtual const char *PoolCString(CYPool &pool) const = 0;
41 #ifdef CY_EXECUTE
42 virtual JSValueRef CastJSValue(JSContextRef context) const = 0;
43 #endif
44 };
45
46 void CYThrow(const char *format, ...) _noreturn;
47
48 #ifdef CY_EXECUTE
49 void CYThrow(JSContextRef context, JSValueRef value);
50 #endif
51
52 #define CYTry \
53 try
54 #define CYCatch(value) \
55 catch (const CYException &error) { \
56 *exception = error.CastJSValue(context); \
57 return value; \
58 } catch (...) { \
59 *exception = CYCastJSValue(context, "catch(...)"); \
60 return value; \
61 }
62
63 #define _assert_(mode, test, code, format, ...) do \
64 if (!(test)) \
65 CYThrow("*** _%s(%s):%s(%u):%s" format, mode, code, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
66 while (false)
67
68 // XXX: fix this: _ is not safe; this is /not/ Menes ;P
69 #undef _assert
70 #define _assert(test) \
71 _assert_("assert", (test), #test, "")
72
73 #define _trace() do { \
74 fprintf(stderr, "_trace():%u\n", __LINE__); \
75 } while (false)
76
77 static _finline bool CYContains(int value, size_t many, int *okay) {
78 for (size_t i(0); i != many; ++i)
79 if (value == okay[i])
80 return true;
81 return false;
82 }
83
84 #define _syscall_(expr, many, okay) ({ \
85 __typeof__(expr) _value; \
86 do if ((long) (_value = (expr)) != -1) \
87 break; \
88 else if (CYContains(errno, many, ((int [many]) okay))) \
89 break; \
90 else \
91 _assert_("syscall", errno == EINTR, #expr, " [errno=%d]", errno); \
92 while (true); \
93 _value; \
94 })
95
96 #define _syscall(expr) \
97 _syscall_(expr, 0, {})
98
99 #define _aprcall(expr) \
100 do { \
101 apr_status_t _aprstatus((expr)); \
102 _assert_("aprcall", _aprstatus == APR_SUCCESS, #expr, ""); \
103 } while (false)
104
105 #define _krncall(expr) \
106 do { \
107 kern_return_t _krnstatus((expr)); \
108 _assert_("krncall", _krnstatus == KERN_SUCCESS, #expr, " [return=0x%x]", _krnstatus); \
109 } while (false)
110
111 #define _sqlcall(expr) ({ \
112 __typeof__(expr) _value = (expr); \
113 _assert_("sqlcall", _value == 0 || _value >= 100 && _value < 200, #expr, " %u:%s", _value sqlite3_errmsg(database_)); \
114 })
115
116 struct CYJSException {
117 JSContextRef context_;
118 JSValueRef value_;
119
120 CYJSException(JSContextRef context) :
121 context_(context),
122 value_(NULL)
123 {
124 }
125
126 ~CYJSException() {
127 CYThrow(context_, value_);
128 }
129
130 operator JSValueRef *() {
131 return &value_;
132 }
133 };
134
135 #define _jsccall(code, args...) ({ \
136 CYJSException _error(context); \
137 (code)(args, _error); \
138 })
139
140 #endif/*CYCRIPT_ERROR_HPP*/