Skip to content

Commit

Permalink
Simplify rescue stack management; ref #3683
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Jun 1, 2017
1 parent 7ff90b5 commit eb5a606
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions include/mruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ typedef struct {
struct RProc *proc;
mrb_value *stackent;
int nregs;
int ridx;
int rpos;
int epos;
struct REnv *env;
mrb_code *pc; /* return address */
Expand Down Expand Up @@ -141,7 +141,7 @@ struct mrb_context {
mrb_callinfo *cibase, *ciend;

mrb_code **rescue; /* exception handler stack */
int rsize;
int rsize, ridx;
struct RProc **ensure; /* ensure handler stack */
int esize, eidx;

Expand Down
22 changes: 10 additions & 12 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,6 @@ cipush(mrb_state *mrb)
struct mrb_context *c = mrb->c;
mrb_callinfo *ci = c->ci;

int ridx = ci->ridx;

if (ci + 1 == c->ciend) {
ptrdiff_t size = ci - c->cibase;

Expand All @@ -245,7 +243,7 @@ cipush(mrb_state *mrb)
}
ci = ++c->ci;
ci->epos = mrb->c->eidx;
ci->ridx = ridx;
ci->rpos = mrb->c->ridx;
ci->env = 0;
ci->pc = 0;
ci->err = 0;
Expand Down Expand Up @@ -1125,12 +1123,12 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)

CASE(OP_ONERR) {
/* sBx pc+=sBx on exception */
if (mrb->c->rsize <= mrb->c->ci->ridx) {
if (mrb->c->rsize <= mrb->c->ridx) {
if (mrb->c->rsize == 0) mrb->c->rsize = RESCUE_STACK_INIT_SIZE;
else mrb->c->rsize *= 2;
mrb->c->rescue = (mrb_code **)mrb_realloc(mrb, mrb->c->rescue, sizeof(mrb_code*) * mrb->c->rsize);
}
mrb->c->rescue[mrb->c->ci->ridx++] = pc + GETARG_sBx(i);
mrb->c->rescue[mrb->c->ridx++] = pc + GETARG_sBx(i);
NEXT;
}

Expand Down Expand Up @@ -1180,7 +1178,7 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
int a = GETARG_A(i);

while (a--) {
mrb->c->ci->ridx--;
mrb->c->ridx--;
}
NEXT;
}
Expand Down Expand Up @@ -1757,11 +1755,11 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
L_RAISE:
ci0 = ci = mrb->c->ci;
if (ci == mrb->c->cibase) {
if (ci->ridx == 0) goto L_FTOP;
if (mrb->c->ridx == 0) goto L_FTOP;
goto L_RESCUE;
}
stk = mrb->c->stack;
while (ci[0].ridx == ci[-1].ridx) {
while (mrb->c->ridx == ci->rpos) {
cipop(mrb);
mrb->c->stack = ci->stackent;
if (ci->acc == CI_ACC_SKIP && prev_jmp) {
Expand All @@ -1771,7 +1769,7 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
ci = mrb->c->ci;
if (ci == mrb->c->cibase) {
mrb->c->stack = stk;
if (ci->ridx == 0) {
if (mrb->c->ridx == 0) {
L_FTOP: /* fiber top */
if (mrb->c == mrb->root_c) {
mrb->c->stack = mrb->c->stbase;
Expand All @@ -1788,23 +1786,23 @@ mrb_vm_exec(mrb_state *mrb, struct RProc *proc, mrb_code *pc)
break;
}
/* call ensure only when we skip this callinfo */
if (ci[0].ridx == ci[-1].ridx) {
if (mrb->c->ridx == ci->rpos) {
while (mrb->c->eidx > ci->epos) {
ecall(mrb, --mrb->c->eidx);
ci = mrb->c->ci;
}
}
}
L_RESCUE:
if (ci->ridx == 0) goto L_STOP;
if (mrb->c->ridx == 0) goto L_STOP;
proc = ci->proc;
irep = proc->body.irep;
pool = irep->pool;
syms = irep->syms;
if (ci != ci0) {
mrb->c->stack = ci[1].stackent;
}
pc = mrb->c->rescue[--ci->ridx];
pc = mrb->c->rescue[--mrb->c->ridx];
}
else {
mrb_callinfo *ci = mrb->c->ci;
Expand Down

0 comments on commit eb5a606

Please sign in to comment.