]> git.saurik.com Git - cycript.git/blame - Exception.hpp
Directly use primitives to construct class types.
[cycript.git] / Exception.hpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
f95d2598 2 * Copyright (C) 2009-2014 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
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
34class CYPool;
35
37954781 36struct 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
46void CYThrow(const char *format, ...) _noreturn;
9cad30fa
JF
47
48#ifdef CY_EXECUTE
37954781 49void 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
49c0d263
JF
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__); \
66while (false)
67
a4dbf05b
JF
68// XXX: fix this: _ is not safe; this is /not/ Menes ;P
69#undef _assert
49c0d263
JF
70#define _assert(test) \
71 _assert_("assert", (test), #test, "")
37954781 72
8f41509f
JF
73#define _require(expr) ({ \
74 __typeof__(expr) _value = (expr); \
75 _assert_("require", _value != NULL, #expr, ""); \
76_value; })
77
37954781
JF
78#define _trace() do { \
79 fprintf(stderr, "_trace():%u\n", __LINE__); \
80} while (false)
81
985e3d1e 82static _finline bool CYContains(int value, size_t many, const int *okay) {
ccd33bdc
JF
83 for (size_t i(0); i != many; ++i)
84 if (value == okay[i])
85 return true;
86 return false;
87}
88
89#define _syscall_(expr, many, okay) ({ \
37954781
JF
90 __typeof__(expr) _value; \
91 do if ((long) (_value = (expr)) != -1) \
92 break; \
985e3d1e 93 else if (CYContains(errno, many, ((const int [many]) okay))) \
ccd33bdc 94 break; \
49c0d263
JF
95 else \
96 _assert_("syscall", errno == EINTR, #expr, " [errno=%d]", errno); \
97 while (true); \
37954781
JF
98 _value; \
99})
100
ccd33bdc
JF
101#define _syscall(expr) \
102 _syscall_(expr, 0, {})
103
b6961e53
JF
104#define _krncall(expr) \
105 do { \
106 kern_return_t _krnstatus((expr)); \
49c0d263 107 _assert_("krncall", _krnstatus == KERN_SUCCESS, #expr, " [return=0x%x]", _krnstatus); \
b6961e53
JF
108 } while (false)
109
37954781
JF
110#define _sqlcall(expr) ({ \
111 __typeof__(expr) _value = (expr); \
49c0d263 112 _assert_("sqlcall", _value == 0 || _value >= 100 && _value < 200, #expr, " %u:%s", _value sqlite3_errmsg(database_)); \
37954781
JF
113})
114
f1b5a47f
JF
115struct CYJSException {
116 JSContextRef context_;
117 JSValueRef value_;
118
119 CYJSException(JSContextRef context) :
120 context_(context),
121 value_(NULL)
122 {
123 }
124
125 ~CYJSException() {
126 CYThrow(context_, value_);
127 }
128
129 operator JSValueRef *() {
130 return &value_;
131 }
132};
133
134#define _jsccall(code, args...) ({ \
135 CYJSException _error(context); \
136 (code)(args, _error); \
137})
138
37954781 139#endif/*CYCRIPT_ERROR_HPP*/