]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
d15b59f5 JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
d15b59f5 | 6 | /* |
c15969fd JF |
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. | |
d15b59f5 | 11 | * |
c15969fd JF |
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. | |
d15b59f5 | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
d15b59f5 JF |
20 | /* }}} */ |
21 | ||
37954781 JF |
22 | #ifndef CYCRIPT_EXCEPTION_HPP |
23 | #define CYCRIPT_EXCEPTION_HPP | |
24 | ||
9cad30fa | 25 | #ifdef CY_EXECUTE |
37954781 | 26 | #include <JavaScriptCore/JSBase.h> |
9cad30fa | 27 | #endif |
37954781 | 28 | |
0cbeddf8 JF |
29 | // XXX: does _assert really need this? |
30 | #include <errno.h> | |
31 | ||
37954781 JF |
32 | #include "Standard.hpp" |
33 | ||
b799113b JF |
34 | class CYPool; |
35 | ||
37954781 | 36 | struct CYException { |
7c6c5b0a JF |
37 | virtual ~CYException() { |
38 | } | |
39 | ||
b799113b | 40 | virtual const char *PoolCString(CYPool &pool) const = 0; |
9cad30fa | 41 | #ifdef CY_EXECUTE |
37954781 | 42 | virtual JSValueRef CastJSValue(JSContextRef context) const = 0; |
9cad30fa | 43 | #endif |
37954781 JF |
44 | }; |
45 | ||
46 | void CYThrow(const char *format, ...) _noreturn; | |
9cad30fa JF |
47 | |
48 | #ifdef CY_EXECUTE | |
37954781 | 49 | void CYThrow(JSContextRef context, JSValueRef value); |
9cad30fa | 50 | #endif |
37954781 JF |
51 | |
52 | #define CYTry \ | |
53 | try | |
55c6d6ab | 54 | #define CYCatch(value) \ |
37954781 JF |
55 | catch (const CYException &error) { \ |
56 | *exception = error.CastJSValue(context); \ | |
55c6d6ab | 57 | return value; \ |
37954781 JF |
58 | } catch (...) { \ |
59 | *exception = CYCastJSValue(context, "catch(...)"); \ | |
55c6d6ab | 60 | return value; \ |
37954781 JF |
61 | } |
62 | ||
a4dbf05b JF |
63 | // XXX: fix this: _ is not safe; this is /not/ Menes ;P |
64 | #undef _assert | |
37954781 JF |
65 | #define _assert(test, args...) do { \ |
66 | if (!(test)) \ | |
67 | CYThrow("*** _assert(%s):%s(%u):%s [errno=%d]", #test, __FILE__, __LINE__, __FUNCTION__, errno); \ | |
68 | } while (false) | |
69 | ||
70 | #define _trace() do { \ | |
71 | fprintf(stderr, "_trace():%u\n", __LINE__); \ | |
72 | } while (false) | |
73 | ||
74 | #define _syscall(expr) ({ \ | |
75 | __typeof__(expr) _value; \ | |
76 | do if ((long) (_value = (expr)) != -1) \ | |
77 | break; \ | |
78 | else switch (errno) { \ | |
79 | case EINTR: \ | |
80 | continue; \ | |
81 | default: \ | |
82 | _assert(false); \ | |
83 | } while (true); \ | |
84 | _value; \ | |
85 | }) | |
86 | ||
87 | #define _aprcall(expr) \ | |
88 | do { \ | |
89 | apr_status_t _aprstatus((expr)); \ | |
90 | _assert(_aprstatus == APR_SUCCESS); \ | |
91 | } while (false) | |
92 | ||
b6961e53 JF |
93 | #define _krncall(expr) \ |
94 | do { \ | |
95 | kern_return_t _krnstatus((expr)); \ | |
96 | _assert(_krnstatus == KERN_SUCCESS); \ | |
97 | } while (false) | |
98 | ||
37954781 JF |
99 | #define _sqlcall(expr) ({ \ |
100 | __typeof__(expr) _value = (expr); \ | |
101 | if (_value != 0 && (_value < 100 || _value >= 200)) \ | |
102 | _assert(false, "_sqlcall(%u:%s): %s\n", _value, #expr, sqlite3_errmsg(database_)); \ | |
103 | _value; \ | |
104 | }) | |
105 | ||
f1b5a47f JF |
106 | struct CYJSException { |
107 | JSContextRef context_; | |
108 | JSValueRef value_; | |
109 | ||
110 | CYJSException(JSContextRef context) : | |
111 | context_(context), | |
112 | value_(NULL) | |
113 | { | |
114 | } | |
115 | ||
116 | ~CYJSException() { | |
117 | CYThrow(context_, value_); | |
118 | } | |
119 | ||
120 | operator JSValueRef *() { | |
121 | return &value_; | |
122 | } | |
123 | }; | |
124 | ||
125 | #define _jsccall(code, args...) ({ \ | |
126 | CYJSException _error(context); \ | |
127 | (code)(args, _error); \ | |
128 | }) | |
129 | ||
37954781 | 130 | #endif/*CYCRIPT_ERROR_HPP*/ |