2 #include <mruby/throw.h>
3 #include <mruby/error.h>
6 mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state)
8 struct mrb_jmpbuf *prev_jmp = mrb->jmp;
9 struct mrb_jmpbuf c_jmp;
10 mrb_value result = mrb_nil_value();
11 int ai = mrb_gc_arena_save(mrb);
13 if (state) { *state = FALSE; }
17 result = body(mrb, data);
21 result = mrb_obj_value(mrb->exc);
23 if (state) { *state = TRUE; }
24 } MRB_END_EXC(&c_jmp);
26 mrb_gc_arena_restore(mrb, ai);
27 mrb_gc_protect(mrb, result);
32 mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t ensure, mrb_value e_data)
34 struct mrb_jmpbuf *prev_jmp = mrb->jmp;
35 struct mrb_jmpbuf c_jmp;
37 int ai = mrb_gc_arena_save(mrb);
41 result = body(mrb, b_data);
45 mrb_gc_arena_restore(mrb, ai);
47 MRB_THROW(mrb->jmp); /* rethrow catched exceptions */
48 } MRB_END_EXC(&c_jmp);
50 mrb_gc_arena_restore(mrb, ai);
51 mrb_gc_protect(mrb, result);
53 mrb_gc_arena_restore(mrb, ai);
54 mrb_gc_protect(mrb, result);
59 mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
60 mrb_func_t rescue, mrb_value r_data)
62 return mrb_rescue_exceptions(mrb, body, b_data, rescue, r_data, 1, &mrb->eStandardError_class);
66 mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data,
67 mrb_int len, struct RClass **classes)
69 struct mrb_jmpbuf *prev_jmp = mrb->jmp;
70 struct mrb_jmpbuf c_jmp;
72 mrb_bool error_matched = FALSE;
74 int ai = mrb_gc_arena_save(mrb);
78 result = body(mrb, b_data);
83 for (i = 0; i < len; ++i) {
84 if (mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), classes[i])) {
90 if (!error_matched) { MRB_THROW(mrb->jmp); }
93 mrb_gc_arena_restore(mrb, ai);
94 result = rescue(mrb, r_data);
95 } MRB_END_EXC(&c_jmp);
97 mrb_gc_arena_restore(mrb, ai);
98 mrb_gc_protect(mrb, result);
103 mrb_mruby_error_gem_init(mrb_state *mrb)
108 mrb_mruby_error_gem_final(mrb_state *mrb)