static void replace_stop_with_return(mrb_state *mrb, mrb_irep *irep) { if (irep->iseq[irep->ilen - 1] == MKOP_A(OP_STOP, 0)) { irep->iseq = mrb_realloc(mrb, irep->iseq, (irep->ilen + 1) * sizeof(mrb_code)); irep->iseq[irep->ilen - 1] = MKOP_A(OP_LOADNIL, 0); irep->iseq[irep->ilen] = MKOP_AB(OP_RETURN, 0, OP_R_NORMAL); irep->ilen++; } }
static void replace_stop_with_return(mrb_state *mrb, mrb_irep *irep) { if (irep->iseq[irep->ilen - 1] == MKOP_A(OP_STOP, 0)) { if (irep->flags == MRB_ISEQ_NO_FREE) { mrb_code* iseq = (mrb_code *)mrb_malloc(mrb, (irep->ilen + 1) * sizeof(mrb_code)); memcpy(iseq, irep->iseq, irep->ilen * sizeof(mrb_code)); irep->iseq = iseq; irep->flags &= ~MRB_ISEQ_NO_FREE; } else { irep->iseq = (mrb_code *)mrb_realloc(mrb, irep->iseq, (irep->ilen + 1) * sizeof(mrb_code)); } irep->iseq[irep->ilen - 1] = MKOP_A(OP_LOADNIL, 0); irep->iseq[irep->ilen] = MKOP_AB(OP_RETURN, 0, OP_R_NORMAL); irep->ilen++; } }
void mrb_init_proc(mrb_state *mrb) { struct RProc *m; mrb_code *call_iseq = mrb_malloc(mrb, sizeof(mrb_code)); mrb_irep *call_irep = mrb_calloc(mrb, sizeof(mrb_irep), 1); if ( call_iseq == NULL || call_irep == NULL ) return; *call_iseq = MKOP_A(OP_CALL, 0); call_irep->idx = -1; call_irep->flags = MRB_IREP_NOFREE; call_irep->iseq = call_iseq; call_irep->ilen = 1; mrb->proc_class = mrb_define_class(mrb, "Proc", mrb->object_class); m = mrb_proc_new(mrb, call_irep); mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern(mrb, "call"), m); mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern(mrb, "[]"), m); }
/* ** proc.c - Proc class ** ** See Copyright Notice in mruby.h */ #include "mruby.h" #include "mruby/class.h" #include "mruby/proc.h" #include "opcode.h" static mrb_code call_iseq[] = { MKOP_A(OP_NOP, 0), MKOP_A(OP_CALL, 0), }; struct RProc * mrb_proc_new(mrb_state *mrb, mrb_irep *irep) { struct RProc *p; mrb_callinfo *ci = mrb->c->ci; p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class); p->target_class = 0; if (ci) { if (ci->proc) p->target_class = ci->proc->target_class; if (!p->target_class) p->target_class = ci->target_class; } p->body.irep = irep;
/* ** proc.c - Proc class ** ** See Copyright Notice in mruby.h */ #include "mruby.h" #include "mruby/class.h" #include "mruby/proc.h" #include "opcode.h" static mrb_code call_iseq[] = { MKOP_A(OP_CALL, 0), }; struct RProc * mrb_proc_new(mrb_state *mrb, mrb_irep *irep) { struct RProc *p; p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class); p->target_class = (mrb->c->ci) ? mrb->c->ci->target_class : 0; p->body.irep = irep; p->env = 0; mrb_irep_incref(mrb, irep); return p; } static inline void closure_setup(mrb_state *mrb, struct RProc *p, int nlocals)