1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
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/>.
22 #ifndef CYCRIPT_EXCEPTION_HPP
23 #define CYCRIPT_EXCEPTION_HPP
28 #include <JavaScriptCore/JSBase.h>
31 // XXX: does _assert really need this?
34 #include "Standard.hpp"
39 virtual ~CYException() {
42 virtual const char *PoolCString(CYPool &pool) const = 0;
44 virtual JSValueRef CastJSValue(JSContextRef context) const = 0;
48 void CYThrow(const char *format, ...) _noreturn;
51 void CYThrow(JSContextRef context, JSValueRef value);
56 #define CYCatch(value) \
57 catch (const CYException &error) { \
58 *exception = error.CastJSValue(context); \
61 *exception = CYCastJSValue(context, "catch(...)"); \
65 #define _assert_(mode, test, code, format, ...) do \
67 CYThrow("*** _%s(%s):%s(%u):%s" format, mode, code, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
70 // XXX: fix this: _ is not safe; this is /not/ Menes ;P
72 #define _assert(test) \
73 _assert_("assert", (test), #test, "")
75 #define _require(expr) ({ \
76 __typeof__(expr) _value = (expr); \
77 _assert_("require", _value != NULL, #expr, ""); \
80 #define _trace() do { \
81 fprintf(stderr, "_trace(%s:%u)\n", __FILE__, __LINE__); \
84 static _finline bool CYContains(int value, size_t many, const int *okay) {
85 for (size_t i(0); i != many; ++i)
91 #define _syscall_(expr, many, ...) ({ \
92 __typeof__(expr) _value; \
93 do if ((long) (_value = (expr)) != -1) \
95 else if (CYContains(errno, many, ((const int [many + 1]) {0, ##__VA_ARGS__} + 1))) \
98 _assert_("syscall", errno == EINTR, #expr, " [errno=%d]", errno); \
103 #define _syscall(expr) \
106 #define _sqlcall(expr) ({ \
107 __typeof__(expr) _value = (expr); \
108 _assert_("sqlcall", _value == 0 || _value >= 100 && _value < 200, #expr, " %u:%s", _value sqlite3_errmsg(database_)); \
111 struct CYJSException {
112 JSContextRef context_;
115 CYJSException(JSContextRef context) :
121 ~CYJSException() noexcept(false) {
122 CYThrow(context_, value_);
125 operator JSValueRef *() {
130 #define _jsccall(code, args...) ({ \
131 CYJSException _error(context); \
132 (code)(args, _error); \
135 #endif/*CYCRIPT_ERROR_HPP*/