1 /* Cycript - Optimizing JavaScript Compiler/Runtime
 
   2  * Copyright (C) 2009-2013  Jay Freeman (saurik)
 
   5 /* GNU General Public License, Version 3 {{{ */
 
   7  * Cycript is free software: you can redistribute it and/or modify
 
   8  * it under the terms of the GNU General Public License as published
 
   9  * by the Free Software Foundation, either version 3 of the License,
 
  10  * or (at your option) any later version.
 
  12  * Cycript is distributed in the hope that it will be useful, but
 
  13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
  14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  15  * GNU General Public License for more details.
 
  17  * You should have received a copy of the GNU General Public License
 
  18  * along with Cycript.  If not, see <http://www.gnu.org/licenses/>.
 
  22 #ifndef CYCRIPT_EXCEPTION_HPP
 
  23 #define CYCRIPT_EXCEPTION_HPP
 
  26 #include <JavaScriptCore/JSBase.h>
 
  29 // XXX: does _assert really need this?
 
  32 #include "Standard.hpp"
 
  37     virtual ~CYException() {
 
  40     virtual const char *PoolCString(CYPool &pool) const = 0;
 
  42     virtual JSValueRef CastJSValue(JSContextRef context) const = 0;
 
  46 void CYThrow(const char *format, ...) _noreturn;
 
  49 void CYThrow(JSContextRef context, JSValueRef value);
 
  54 #define CYCatch(value) \
 
  55     catch (const CYException &error) { \
 
  56         *exception = error.CastJSValue(context); \
 
  59         *exception = CYCastJSValue(context, "catch(...)"); \
 
  63 #define _assert_(mode, test, code, format, ...) do \
 
  65         CYThrow("*** _%s(%s):%s(%u):%s" format, mode, code, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
 
  68 // XXX: fix this: _ is not safe; this is /not/ Menes ;P
 
  70 #define _assert(test) \
 
  71     _assert_("assert", (test), #test, "")
 
  73 #define _require(expr) ({ \
 
  74     __typeof__(expr) _value = (expr); \
 
  75     _assert_("require", _value != NULL, #expr, ""); \
 
  78 #define _trace() do { \
 
  79     fprintf(stderr, "_trace():%u\n", __LINE__); \
 
  82 static _finline bool CYContains(int value, size_t many, const int *okay) {
 
  83     for (size_t i(0); i != many; ++i)
 
  89 #define _syscall_(expr, many, okay) ({ \
 
  90     __typeof__(expr) _value; \
 
  91     do if ((long) (_value = (expr)) != -1) \
 
  93     else if (CYContains(errno, many, ((const int [many]) okay))) \
 
  96         _assert_("syscall", errno == EINTR, #expr, " [errno=%d]", errno); \
 
 101 #define _syscall(expr) \
 
 102     _syscall_(expr, 0, {})
 
 104 #define _krncall(expr) \
 
 106         kern_return_t _krnstatus((expr)); \
 
 107         _assert_("krncall", _krnstatus == KERN_SUCCESS, #expr, " [return=0x%x]", _krnstatus); \
 
 110 #define _sqlcall(expr) ({ \
 
 111     __typeof__(expr) _value = (expr); \
 
 112     _assert_("sqlcall", _value == 0 || _value >= 100 && _value < 200, #expr, " %u:%s", _value sqlite3_errmsg(database_)); \
 
 115 struct CYJSException {
 
 116     JSContextRef context_;
 
 119     CYJSException(JSContextRef context) :
 
 126         CYThrow(context_, value_);
 
 129     operator JSValueRef *() {
 
 134 #define _jsccall(code, args...) ({ \
 
 135     CYJSException _error(context); \
 
 136     (code)(args, _error); \
 
 139 #endif/*CYCRIPT_ERROR_HPP*/