| 1 |
#include <mruby.h> |
| 2 |
#include <mruby/class.h> |
| 3 |
#include <mruby/variable.h> |
| 4 |
#include <mruby/error.h> |
| 5 |
#include <mruby/proc.h> |
| 6 |
#include <mruby/opcode.h> |
| 7 |
#include <mruby/presym.h> |
| 8 |
|
| 9 |
MRB_PRESYM_DEFINE_VAR_AND_INITER(catch_syms_3, 1, MRB_SYM(call)) |
| 10 |
static const mrb_code catch_iseq_3[18] = { |
| 11 |
OP_ENTER, 0x00, 0x00, 0x00, // 000 ENTER 0:0:0:0:0:0:0 (0x0) |
| 12 |
OP_GETUPVAR, 0x02, 0x02, 0x01, // 004 GETUPVAR R2 2 1 |
| 13 |
OP_GETUPVAR, 0x03, 0x01, 0x01, // 008 GETUPVAR R3 1 1 |
| 14 |
OP_SEND, 0x02, 0x00, 0x01, // 012 SEND R2 :call n=1 |
| 15 |
OP_RETURN, 0x02, // 016 RETURN R2 |
| 16 |
}; |
| 17 |
static const mrb_irep catch_irep_3 = { |
| 18 |
2,5,0, |
| 19 |
MRB_IREP_STATIC,catch_iseq_3, |
| 20 |
NULL,catch_syms_3,NULL, |
| 21 |
NULL, |
| 22 |
NULL, |
| 23 |
18,0,1,0,0 |
| 24 |
}; |
| 25 |
static const mrb_irep *catch_reps_2[1] = { |
| 26 |
&catch_irep_3, |
| 27 |
}; |
| 28 |
static const mrb_code catch_iseq_2[13] = { |
| 29 |
OP_ENTER, 0x00, 0x00, 0x00, // 000 ENTER 0:0:0:0:0:0:0 (0x0) |
| 30 |
OP_LAMBDA, 0x02, 0x00, // 004 LAMBDA R2 I[0] |
| 31 |
OP_SEND, 0x02, 0x00, 0x00, // 007 SEND R2 :call n=0 |
| 32 |
OP_RETURN, 0x02, // 011 RETURN R2 |
| 33 |
}; |
| 34 |
static const mrb_irep catch_irep_2 = { |
| 35 |
2,4,0, |
| 36 |
MRB_IREP_STATIC,catch_iseq_2, |
| 37 |
NULL,catch_syms_3,catch_reps_2, |
| 38 |
NULL, |
| 39 |
NULL, |
| 40 |
13,0,1,1,0 |
| 41 |
}; |
| 42 |
static const mrb_irep *catch_reps_1[1] = { |
| 43 |
&catch_irep_2, |
| 44 |
}; |
| 45 |
MRB_PRESYM_DEFINE_VAR_AND_INITER(catch_syms_1, 3, MRB_SYM(Object), MRB_SYM(new), MRB_SYM(call)) |
| 46 |
static const mrb_code catch_iseq_1[29] = { |
| 47 |
OP_ENTER, 0x00, 0x20, 0x01, // 000 ENTER 0:1:0:0:0:0:1 (0x2001) |
| 48 |
OP_JMP, 0x00, 0x03, // 004 JMP 010 |
| 49 |
OP_JMP, 0x00, 0x0a, // 007 JMP 020 |
| 50 |
OP_GETCONST, 0x03, 0x00, // 010 GETCONST R3 Object |
| 51 |
OP_SEND, 0x03, 0x01, 0x00, // 013 SEND R3 :new n=0 |
| 52 |
OP_MOVE, 0x01, 0x03, // 017 MOVE R1 R3 |
| 53 |
OP_LAMBDA, 0x03, 0x00, // 020 LAMBDA R3 I[0] |
| 54 |
OP_SEND, 0x03, 0x02, 0x00, // 023 SEND R3 :call n=0 |
| 55 |
OP_RETURN, 0x03, // 027 RETURN R3 |
| 56 |
}; |
| 57 |
static const mrb_irep catch_irep = { |
| 58 |
3,5,0, |
| 59 |
MRB_IREP_STATIC,catch_iseq_1, |
| 60 |
NULL,catch_syms_1,catch_reps_1, |
| 61 |
NULL, |
| 62 |
NULL, |
| 63 |
29,0,3,1,0 |
| 64 |
}; |
| 65 |
static const struct RProc catch_proc = { |
| 66 |
NULL, NULL, MRB_TT_PROC, MRB_GC_RED, MRB_FL_OBJ_IS_FROZEN | MRB_PROC_SCOPE | MRB_PROC_STRICT, |
| 67 |
{ &catch_irep }, NULL, { NULL } |
| 68 |
}; |
| 69 |
|
| 70 |
static const mrb_callinfo * |
| 71 |
find_catcher(mrb_state *mrb, mrb_value tag) |
| 72 |
{ |
| 73 |
const mrb_callinfo *ci = mrb->c->ci; |
| 74 |
size_t n = ci - mrb->c->cibase; |
| 75 |
ci--; |
| 76 |
for (; n > 0; n--, ci--) { |
| 77 |
const mrb_value *arg1 = ci->stack + 1; |
| 78 |
if (ci->proc == &catch_proc && mrb_obj_eq(mrb, *arg1, tag)) { |
| 79 |
return ci; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
static mrb_value |
| 87 |
throw_m(mrb_state *mrb, mrb_value self) |
| 88 |
{ |
| 89 |
mrb_value tag, obj; |
| 90 |
if (mrb_get_args(mrb, "o|o", &tag, &obj) == 1) { |
| 91 |
obj = mrb_nil_value(); |
| 92 |
} |
| 93 |
|
| 94 |
const mrb_callinfo *ci = find_catcher(mrb, tag); |
| 95 |
if (ci) { |
| 96 |
struct RBreak *b = MRB_OBJ_ALLOC(mrb, MRB_TT_BREAK, NULL); |
| 97 |
mrb_break_value_set(b, obj); |
| 98 |
mrb_break_proc_set(b, ci[2].proc); /* Back to the closure in `catch` method */ |
| 99 |
mrb_exc_raise(mrb, mrb_obj_value(b)); |
| 100 |
} |
| 101 |
else { |
| 102 |
mrb_value argv[2] = {tag, obj}; |
| 103 |
mrb_exc_raise(mrb, mrb_obj_new(mrb, mrb_exc_get_id(mrb, MRB_ERROR_SYM(UncaughtThrowError)), 2, argv)); |
| 104 |
} |
| 105 |
/* not reached */ |
| 106 |
return mrb_nil_value(); |
| 107 |
} |
| 108 |
|
| 109 |
void |
| 110 |
mrb_mruby_catch_gem_init(mrb_state *mrb) |
| 111 |
{ |
| 112 |
mrb_method_t m; |
| 113 |
|
| 114 |
MRB_PRESYM_INIT_SYMBOLS(mrb, catch_syms_3); |
| 115 |
MRB_PRESYM_INIT_SYMBOLS(mrb, catch_syms_1); |
| 116 |
MRB_METHOD_FROM_PROC(m, &catch_proc); |
| 117 |
mrb_define_method_raw(mrb, mrb->kernel_module, MRB_SYM(catch), m); |
| 118 |
|
| 119 |
mrb_define_method(mrb, mrb->kernel_module, "throw", throw_m, MRB_ARGS_ARG(1,1)); |
| 120 |
} |
| 121 |
|
| 122 |
void |
| 123 |
mrb_mruby_catch_gem_final(mrb_state *mrb) |
| 124 |
{ |
| 125 |
} |