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 };
21 
22 #define mrb_exc_ptr(v) ((struct RException*)mrb_ptr(v))
23 
24 MRB_API void mrb_sys_fail(mrb_state *mrb, const char *mesg);
25 MRB_API mrb_value mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str);
26 #define mrb_exc_new_str_lit(mrb, c, lit) mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, lit))
27 MRB_API mrb_value mrb_make_exception(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
28 MRB_API mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value exc);
29 MRB_API mrb_value mrb_get_backtrace(mrb_state *mrb);
30 MRB_API mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, const char *fmt, ...);
31 
32 /* declaration for `fail` method */
33 MRB_API mrb_value mrb_f_raise(mrb_state*, mrb_value);
34 
35 #if defined(MRB_64BIT) || defined(MRB_USE_FLOAT) || defined(MRB_NAN_BOXING) || defined(MRB_WORD_BOXING)
36 struct RBreak {
37   MRB_OBJECT_HEADER;
38   struct RProc *proc;
39   mrb_value val;
40 };
41 #define mrb_break_value_get(brk) ((brk)->val)
42 #define mrb_break_value_set(brk, v) ((brk)->val = v)
43 #else
44 struct RBreak {
45   MRB_OBJECT_HEADER;
46   struct RProc *proc;
47   union mrb_value_union value;
48 };
49 #define RBREAK_VALUE_TT_MASK ((1 << 8) - 1)
50 static inline mrb_value
mrb_break_value_get(struct RBreak * brk)51 mrb_break_value_get(struct RBreak *brk)
52 {
53   mrb_value val;
54   val.value = brk->value;
55   val.tt = (enum mrb_vtype)(brk->flags & RBREAK_VALUE_TT_MASK);
56   return val;
57 }
58 static inline void
mrb_break_value_set(struct RBreak * brk,mrb_value val)59 mrb_break_value_set(struct RBreak *brk, mrb_value val)
60 {
61   brk->value = val.value;
62   brk->flags &= ~RBREAK_VALUE_TT_MASK;
63   brk->flags |= val.tt;
64 }
65 #endif  /* MRB_64BIT || MRB_USE_FLOAT || MRB_NAN_BOXING || MRB_WORD_BOXING */
66 #define mrb_break_proc_get(brk) ((brk)->proc)
67 #define mrb_break_proc_set(brk, p) ((brk)->proc = p)
68 
69 /**
70  * Protect
71  *
72  * Implemented in the mruby-error mrbgem
73  */
74 MRB_API mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state);
75 
76 /**
77  * Ensure
78  *
79  * Implemented in the mruby-error mrbgem
80  */
81 MRB_API mrb_value mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
82                              mrb_func_t ensure, mrb_value e_data);
83 
84 /**
85  * Rescue
86  *
87  * Implemented in the mruby-error mrbgem
88  */
89 MRB_API mrb_value mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
90                              mrb_func_t rescue, mrb_value r_data);
91 
92 /**
93  * Rescue exception
94  *
95  * Implemented in the mruby-error mrbgem
96  */
97 MRB_API mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
98                                         mrb_func_t rescue, mrb_value r_data,
99                                         mrb_int len, struct RClass **classes);
100 
101 MRB_END_DECL
102 
103 #endif  /* MRUBY_ERROR_H */
104