]> git.saurik.com Git - cycript.git/blame - Exception.hpp
Linux requires AX_PTHREAD to autodetect -lpthread.
[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
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
36class CYPool;
37
37954781 38struct CYException {
7c6c5b0a
JF
39 virtual ~CYException() {
40 }
41
b799113b 42 virtual const char *PoolCString(CYPool &pool) const = 0;
9cad30fa 43#ifdef CY_EXECUTE
37954781 44 virtual JSValueRef CastJSValue(JSContextRef context) const = 0;
9cad30fa 45#endif
37954781
JF
46};
47
48void CYThrow(const char *format, ...) _noreturn;
9cad30fa
JF
49
50#ifdef CY_EXECUTE
37954781 51void CYThrow(JSContextRef context, JSValueRef value);
9cad30fa 52#endif
37954781
JF
53
54#define CYTry \
55 try
55c6d6ab 56#define CYCatch(value) \
37954781
JF
57 catch (const CYException &error) { \
58 *exception = error.CastJSValue(context); \
55c6d6ab 59 return value; \
37954781
JF
60 } catch (...) { \
61 *exception = CYCastJSValue(context, "catch(...)"); \
55c6d6ab 62 return value; \
37954781
JF
63 }
64
49c0d263
JF
65#define _assert_(mode, test, code, format, ...) do \
66 if (!(test)) \
67 CYThrow("*** _%s(%s):%s(%u):%s" format, mode, code, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
68while (false)
69
a4dbf05b
JF
70// XXX: fix this: _ is not safe; this is /not/ Menes ;P
71#undef _assert
49c0d263
JF
72#define _assert(test) \
73 _assert_("assert", (test), #test, "")
37954781 74
8f41509f
JF
75#define _require(expr) ({ \
76 __typeof__(expr) _value = (expr); \
77 _assert_("require", _value != NULL, #expr, ""); \
78_value; })
79
37954781
JF
80#define _trace() do { \
81 fprintf(stderr, "_trace():%u\n", __LINE__); \
82} while (false)
83
985e3d1e 84static _finline bool CYContains(int value, size_t many, const int *okay) {
ccd33bdc
JF
85 for (size_t i(0); i != many; ++i)
86 if (value == okay[i])
87 return true;
88 return false;
89}
90
91#define _syscall_(expr, many, okay) ({ \
37954781
JF
92 __typeof__(expr) _value; \
93 do if ((long) (_value = (expr)) != -1) \
94 break; \
985e3d1e 95 else if (CYContains(errno, many, ((const int [many]) okay))) \
ccd33bdc 96 break; \
49c0d263
JF
97 else \
98 _assert_("syscall", errno == EINTR, #expr, " [errno=%d]", errno); \
99 while (true); \
37954781
JF
100 _value; \
101})
102
ccd33bdc
JF
103#define _syscall(expr) \
104 _syscall_(expr, 0, {})
105
37954781
JF
106#define _sqlcall(expr) ({ \
107 __typeof__(expr) _value = (expr); \
49c0d263 108 _assert_("sqlcall", _value == 0 || _value >= 100 && _value < 200, #expr, " %u:%s", _value sqlite3_errmsg(database_)); \
37954781
JF
109})
110
f1b5a47f
JF
111struct CYJSException {
112 JSContextRef context_;
113 JSValueRef value_;
114
115 CYJSException(JSContextRef context) :
116 context_(context),
117 value_(NULL)
118 {
119 }
120
2eb5f06f 121 ~CYJSException() noexcept(false) {
f1b5a47f
JF
122 CYThrow(context_, value_);
123 }
124
125 operator JSValueRef *() {
126 return &value_;
127 }
128};
129
130#define _jsccall(code, args...) ({ \
131 CYJSException _error(context); \
132 (code)(args, _error); \
133})
134
37954781 135#endif/*CYCRIPT_ERROR_HPP*/