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