]> git.saurik.com Git - cycript.git/blob - Exception.hpp
3d4c5cf039809d98a4e1edd6fceae946a11799df
[cycript.git] / Exception.hpp
1 #ifndef CYCRIPT_EXCEPTION_HPP
2 #define CYCRIPT_EXCEPTION_HPP
3
4 #include <JavaScriptCore/JSBase.h>
5
6 #include "Standard.hpp"
7
8 struct CYException {
9 virtual const char *PoolCString(apr_pool_t *pool) const = 0;
10 virtual JSValueRef CastJSValue(JSContextRef context) const = 0;
11 };
12
13 void CYThrow(const char *format, ...) _noreturn;
14 void CYThrow(JSContextRef context, JSValueRef value);
15
16 #define CYTry \
17 try
18 #define CYCatch \
19 catch (const CYException &error) { \
20 *exception = error.CastJSValue(context); \
21 return NULL; \
22 } catch (...) { \
23 *exception = CYCastJSValue(context, "catch(...)"); \
24 return NULL; \
25 }
26
27 #define _assert(test, args...) do { \
28 if (!(test)) \
29 CYThrow("*** _assert(%s):%s(%u):%s [errno=%d]", #test, __FILE__, __LINE__, __FUNCTION__, errno); \
30 } while (false)
31
32 #define _trace() do { \
33 fprintf(stderr, "_trace():%u\n", __LINE__); \
34 } while (false)
35
36 #define _syscall(expr) ({ \
37 __typeof__(expr) _value; \
38 do if ((long) (_value = (expr)) != -1) \
39 break; \
40 else switch (errno) { \
41 case EINTR: \
42 continue; \
43 default: \
44 _assert(false); \
45 } while (true); \
46 _value; \
47 })
48
49 #define _aprcall(expr) \
50 do { \
51 apr_status_t _aprstatus((expr)); \
52 _assert(_aprstatus == APR_SUCCESS); \
53 } while (false)
54
55 #define _sqlcall(expr) ({ \
56 __typeof__(expr) _value = (expr); \
57 if (_value != 0 && (_value < 100 || _value >= 200)) \
58 _assert(false, "_sqlcall(%u:%s): %s\n", _value, #expr, sqlite3_errmsg(database_)); \
59 _value; \
60 })
61
62 #endif/*CYCRIPT_ERROR_HPP*/