| 1 |
/** |
| 2 |
** @file mruby/error.h - Exception class |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef MRUBY_ERROR_H |
| 8 |
#define MRUBY_ERROR_H |
| 9 |
|
| 10 |
#include "common.h" |
| 11 |
|
| 12 |
/** |
| 13 |
* mruby error handling. |
| 14 |
*/ |
| 15 |
MRB_BEGIN_DECL |
| 16 |
|
| 17 |
struct RException { |
| 18 |
MRB_OBJECT_HEADER; |
| 19 |
struct iv_tbl *iv; |
| 20 |
struct RObject *mesg; // NULL or probably RString |
| 21 |
struct RObject *backtrace; // NULL, RArray or RData |
| 22 |
}; |
| 23 |
|
| 24 |
/* error that should terminate execution */ |
| 25 |
#define MRB_EXC_EXIT 65536 |
| 26 |
#define MRB_EXC_EXIT_P(e) ((e)->flags & MRB_EXC_EXIT) |
| 27 |
/* retrieve status value from exc; need <mruby/variable.h> and <mruby/presym.h> */ |
| 28 |
#define MRB_EXC_EXIT_STATUS(mrb,e) ((int)mrb_as_int((mrb),mrb_obj_iv_get((mrb),(e),MRB_SYM(status)))) |
| 29 |
/* exit with SystemExit status */ |
| 30 |
#define MRB_EXC_CHECK_EXIT(mrb,e) do {if (MRB_EXC_EXIT_P(e)) exit(MRB_EXC_EXIT_STATUS((mrb),(e)));} while (0) |
| 31 |
|
| 32 |
#define mrb_exc_ptr(v) ((struct RException*)mrb_ptr(v)) |
| 33 |
|
| 34 |
MRB_API mrb_noreturn void mrb_sys_fail(mrb_state *mrb, const char *mesg); |
| 35 |
MRB_API mrb_value mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str); |
| 36 |
#define mrb_exc_new_lit(mrb, c, lit) mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, lit)) |
| 37 |
MRB_API mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, const char *fmt, ...); |
| 38 |
|
| 39 |
#if defined(MRB_64BIT) || defined(MRB_USE_FLOAT32) || defined(MRB_NAN_BOXING) || defined(MRB_WORD_BOXING) |
| 40 |
struct RBreak { |
| 41 |
MRB_OBJECT_HEADER; |
| 42 |
const struct RProc *proc; |
| 43 |
mrb_value val; |
| 44 |
}; |
| 45 |
#define mrb_break_value_get(brk) ((brk)->val) |
| 46 |
#define mrb_break_value_set(brk, v) ((brk)->val = v) |
| 47 |
#else |
| 48 |
struct RBreak { |
| 49 |
MRB_OBJECT_HEADER; |
| 50 |
const struct RProc *proc; |
| 51 |
union mrb_value_union value; |
| 52 |
}; |
| 53 |
#define RBREAK_VALUE_TT_MASK ((1 << 8) - 1) |
| 54 |
static inline mrb_value |
| 55 |
mrb_break_value_get(struct RBreak *brk) |
| 56 |
{ |
| 57 |
mrb_value val; |
| 58 |
val.value = brk->value; |
| 59 |
val.tt = (enum mrb_vtype)(brk->flags & RBREAK_VALUE_TT_MASK); |
| 60 |
return val; |
| 61 |
} |
| 62 |
static inline void |
| 63 |
mrb_break_value_set(struct RBreak *brk, mrb_value val) |
| 64 |
{ |
| 65 |
brk->value = val.value; |
| 66 |
brk->flags &= ~RBREAK_VALUE_TT_MASK; |
| 67 |
brk->flags |= val.tt; |
| 68 |
} |
| 69 |
#endif /* MRB_64BIT || MRB_USE_FLOAT32 || MRB_NAN_BOXING || MRB_WORD_BOXING */ |
| 70 |
#define mrb_break_proc_get(brk) ((brk)->proc) |
| 71 |
#define mrb_break_proc_set(brk, p) ((brk)->proc = p) |
| 72 |
|
| 73 |
/** |
| 74 |
* Error check |
| 75 |
* |
| 76 |
*/ |
| 77 |
/* clear error status in the mrb_state structure */ |
| 78 |
MRB_API void mrb_clear_error(mrb_state *mrb); |
| 79 |
/* returns TRUE if error in the previous call; internally calls mrb_clear_error() */ |
| 80 |
MRB_API mrb_bool mrb_check_error(mrb_state *mrb); |
| 81 |
|
| 82 |
/** |
| 83 |
* Protect |
| 84 |
* |
| 85 |
*/ |
| 86 |
typedef mrb_value mrb_protect_error_func(mrb_state *mrb, void *userdata); |
| 87 |
MRB_API mrb_value mrb_protect_error(mrb_state *mrb, mrb_protect_error_func *body, void *userdata, mrb_bool *error); |
| 88 |
|
| 89 |
/** |
| 90 |
* Protect (takes mrb_value for body argument) |
| 91 |
* |
| 92 |
* Implemented in the mruby-error mrbgem |
| 93 |
*/ |
| 94 |
MRB_API mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state); |
| 95 |
|
| 96 |
/** |
| 97 |
* Ensure |
| 98 |
* |
| 99 |
* Implemented in the mruby-error mrbgem |
| 100 |
*/ |
| 101 |
MRB_API mrb_value mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, |
| 102 |
mrb_func_t ensure, mrb_value e_data); |
| 103 |
|
| 104 |
/** |
| 105 |
* Rescue |
| 106 |
* |
| 107 |
* Implemented in the mruby-error mrbgem |
| 108 |
*/ |
| 109 |
MRB_API mrb_value mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data, |
| 110 |
mrb_func_t rescue, mrb_value r_data); |
| 111 |
|
| 112 |
/** |
| 113 |
* Rescue exception |
| 114 |
* |
| 115 |
* Implemented in the mruby-error mrbgem |
| 116 |
*/ |
| 117 |
MRB_API mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, |
| 118 |
mrb_func_t rescue, mrb_value r_data, |
| 119 |
mrb_int len, struct RClass **classes); |
| 120 |
|
| 121 |
MRB_END_DECL |
| 122 |
|
| 123 |
#endif /* MRUBY_ERROR_H */ |