]> git.saurik.com Git - cycript.git/blame_incremental - Exception.hpp
Support Void::PoolFFI when the value is undefined.
[cycript.git] / Exception.hpp
... / ...
CommitLineData
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3*/
4
5/* GNU Affero General Public License, Version 3 {{{ */
6/*
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
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
19**/
20/* }}} */
21
22#ifndef CYCRIPT_EXCEPTION_HPP
23#define CYCRIPT_EXCEPTION_HPP
24
25#include <cstdlib>
26
27#ifdef CY_EXECUTE
28#include <JavaScriptCore/JSBase.h>
29#endif
30
31// XXX: does _assert really need this?
32#include <errno.h>
33
34#include "Standard.hpp"
35
36class CYPool;
37
38struct _visible CYException {
39 virtual ~CYException() {
40 }
41
42 virtual const char *PoolCString(CYPool &pool) const = 0;
43#ifdef CY_EXECUTE
44 virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const = 0;
45#endif
46};
47
48void CYThrow(const char *format, ...) _noreturn;
49
50#ifdef CY_EXECUTE
51void CYThrow(JSContextRef context, JSValueRef value);
52#endif
53
54#define CYTry \
55 try
56#define CYCatch_(value, name) \
57 catch (const CYException &error) { \
58 *exception = error.CastJSValue(context, name); \
59 _assert(*exception != NULL); \
60 return value; \
61 } catch (...) { \
62 *exception = CYCastJSValue(context, "catch(...)"); \
63 _assert(*exception != NULL); \
64 return value; \
65 }
66#define CYCatch(value) \
67 CYCatch_(value, "Error")
68#define CYCatchObject() \
69 CYCatch(JSObjectMake(context, NULL, NULL))
70
71#define _assert_(mode, test, code, format, ...) do \
72 if (!(test)) \
73 CYThrow("*** _%s(%s):%s(%u):%s" format, mode, code, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
74while (false)
75
76// XXX: fix this: _ is not safe; this is /not/ Menes ;P
77#undef _assert
78#define _assert(test) \
79 _assert_("assert", (test), #test, "")
80
81#define _require(expr) ({ \
82 __typeof__(expr) _value = (expr); \
83 _assert_("require", _value != NULL, #expr, ""); \
84_value; })
85
86#define _trace() do { \
87 fprintf(stderr, "_trace(%s:%u)\n", __FILE__, __LINE__); \
88} while (false)
89
90static _finline bool CYContains(int value, size_t many, const int *okay) {
91 for (size_t i(0); i != many; ++i)
92 if (value == okay[i])
93 return true;
94 return false;
95}
96
97#define _syscall_(expr, many, ...) ({ \
98 __typeof__(expr) _value; \
99 do if ((long) (_value = (expr)) != -1) \
100 break; \
101 else if (CYContains(errno, many, ((const int [many + 1]) {0, ##__VA_ARGS__} + 1))) \
102 break; \
103 else \
104 _assert_("syscall", errno == EINTR, #expr, " [errno=%d]", errno); \
105 while (true); \
106 _value; \
107})
108
109#define _syscall(expr) \
110 _syscall_(expr, 0)
111
112#define _sqlcall(expr) ({ \
113 __typeof__(expr) _value = (expr); \
114 _assert_("sqlcall", _value == 0 || _value >= 100 && _value < 200, #expr, " %u:%s", _value, sqlite3_errmsg(database_)); \
115_value; })
116
117#ifdef CY_EXECUTE
118struct CYJSException {
119 JSContextRef context_;
120 JSValueRef value_;
121
122 CYJSException(JSContextRef context) :
123 context_(context),
124 value_(NULL)
125 {
126 }
127
128 ~CYJSException() noexcept(false) {
129 CYThrow(context_, value_);
130 }
131
132 operator JSValueRef *() {
133 return &value_;
134 }
135};
136
137#define _jsccall(code, args...) ({ \
138 CYJSException _error(context); \
139 (code)(args, _error); \
140})
141#endif
142
143#endif/*CYCRIPT_ERROR_HPP*/