Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-error/src/exception.c

raw
1 #include <mruby.h>
2 #include <mruby/error.h>
3
4 struct protect_data {
5 mrb_func_t body;
6 mrb_value data;
7 };
8
9 static mrb_value
10 protect_body(mrb_state *mrb, void *p)
11 {
12 struct protect_data *dp = (struct protect_data*)p;
13 return dp->body(mrb, dp->data);
14 }
15
16 MRB_API mrb_value
17 mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state)
18 {
19 struct protect_data protect_data = { body, data };
20 return mrb_protect_error(mrb, protect_body, &protect_data, state);
21 }
22
23 MRB_API mrb_value
24 mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t ensure, mrb_value e_data)
25 {
26 int ai = mrb_gc_arena_save(mrb);
27 struct protect_data protect_data = { body, b_data };
28 mrb_bool error;
29 mrb_value result = mrb_protect_error(mrb, protect_body, &protect_data, &error);
30 ensure(mrb, e_data);
31 mrb_gc_arena_restore(mrb, ai);
32 mrb_gc_protect(mrb, result);
33 if (error) {
34 mrb_exc_raise(mrb, result); /* rethrow caught exceptions */
35 }
36 return result;
37 }
38
39 MRB_API mrb_value
40 mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data,
41 mrb_func_t rescue, mrb_value r_data)
42 {
43 return mrb_rescue_exceptions(mrb, body, b_data, rescue, r_data, 1, &mrb->eStandardError_class);
44 }
45
46 MRB_API mrb_value
47 mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data,
48 mrb_int len, struct RClass **classes)
49 {
50 int ai = mrb_gc_arena_save(mrb);
51 struct protect_data protect_data = { body, b_data };
52 mrb_bool error;
53 mrb_value result = mrb_protect_error(mrb, protect_body, &protect_data, &error);
54 if (error) {
55 mrb_bool error_matched = FALSE;
56 for (mrb_int i = 0; i < len; i++) {
57 if (mrb_obj_is_kind_of(mrb, result, classes[i])) {
58 error_matched = TRUE;
59 break;
60 }
61 }
62
63 if (!error_matched) { mrb_exc_raise(mrb, result); }
64
65 mrb->exc = NULL;
66 result = rescue(mrb, r_data);
67 mrb_gc_arena_restore(mrb, ai);
68 mrb_gc_protect(mrb, result);
69 }
70 return result;
71 }
72
73 void
74 mrb_mruby_error_gem_init(mrb_state *mrb)
75 {
76 }
77
78 void
79 mrb_mruby_error_gem_final(mrb_state *mrb)
80 {
81 }