diff options
Diffstat (limited to 'src/proc.c')
| -rw-r--r-- | src/proc.c | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/src/proc.c b/src/proc.c index c00b09acf..b971b6520 100644 --- a/src/proc.c +++ b/src/proc.c @@ -14,8 +14,26 @@ static const mrb_code call_iseq[] = { OP_CALL, }; +static const mrb_irep call_irep = { + 0, /* nlocals */ + 2, /* nregs */ + 0, /* clen */ + MRB_ISEQ_NO_FREE | MRB_IREP_NO_FREE, /* flags */ + call_iseq, /* iseq */ + NULL, /* pool */ + NULL, /* syms */ + NULL, /* reps */ + NULL, /* lv */ + NULL, /* debug_info */ + 1, /* ilen */ + 0, /* plen */ + 0, /* slen */ + 1, /* rlen */ + 0, /* refcnt */ +}; + struct RProc* -mrb_proc_new(mrb_state *mrb, mrb_irep *irep) +mrb_proc_new(mrb_state *mrb, const mrb_irep *irep) { struct RProc *p; mrb_callinfo *ci = mrb->c->ci; @@ -34,7 +52,7 @@ mrb_proc_new(mrb_state *mrb, mrb_irep *irep) p->e.target_class = tc; } p->body.irep = irep; - mrb_irep_incref(mrb, irep); + mrb_irep_incref(mrb, (mrb_irep*)irep); return p; } @@ -44,7 +62,7 @@ env_new(mrb_state *mrb, mrb_int nlocals) { struct REnv *e; mrb_callinfo *ci = mrb->c->ci; - int bidx; + mrb_int bidx; e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, NULL); MRB_ENV_SET_LEN(e, nlocals); @@ -63,7 +81,7 @@ static void closure_setup(mrb_state *mrb, struct RProc *p) { mrb_callinfo *ci = mrb->c->ci; - struct RProc *up = p->upper; + const struct RProc *up = p->upper; struct REnv *e = NULL; if (ci && ci->env) { @@ -90,7 +108,7 @@ closure_setup(mrb_state *mrb, struct RProc *p) } struct RProc* -mrb_closure_new(mrb_state *mrb, mrb_irep *irep) +mrb_closure_new(mrb_state *mrb, const mrb_irep *irep) { struct RProc *p = mrb_proc_new(mrb, irep); @@ -153,7 +171,7 @@ mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals) MRB_API mrb_value mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx) { - struct RProc *p = mrb->c->ci->proc; + const struct RProc *p = mrb->c->ci->proc; struct REnv *e; if (!p || !MRB_PROC_CFUNC_P(p)) { @@ -181,7 +199,7 @@ mrb_proc_copy(struct RProc *a, struct RProc *b) a->flags = b->flags; a->body = b->body; if (!MRB_PROC_CFUNC_P(a) && a->body.irep) { - a->body.irep->refcnt++; + mrb_irep_incref(NULL, (mrb_irep*)a->body.irep); } a->upper = b->upper; a->e.env = b->e.env; @@ -200,7 +218,7 @@ mrb_proc_s_new(mrb_state *mrb, mrb_value proc_class) p = (struct RProc *)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb_class_ptr(proc_class)); mrb_proc_copy(p, mrb_proc_ptr(blk)); proc = mrb_obj_value(p); - mrb_funcall_with_block(mrb, proc, mrb_intern_lit(mrb, "initialize"), 0, NULL, proc); + mrb_funcall_with_block(mrb, proc, MRB_SYM(initialize), 0, NULL, proc); if (!MRB_PROC_STRICT_P(p) && mrb->c->ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb->c->ci[-1].env) { p->flags |= MRB_PROC_ORPHAN; @@ -224,7 +242,7 @@ mrb_proc_init_copy(mrb_state *mrb, mrb_value self) static mrb_value proc_arity(mrb_state *mrb, mrb_value self) { - return mrb_fixnum_value(mrb_proc_arity(mrb_proc_ptr(self))); + return mrb_int_value(mrb, mrb_proc_arity(mrb_proc_ptr(self))); } /* 15.3.1.2.6 */ @@ -262,7 +280,7 @@ proc_lambda(mrb_state *mrb, mrb_value self) mrb_int mrb_proc_arity(const struct RProc *p) { - struct mrb_irep *irep; + const mrb_irep *irep; const mrb_code *pc; mrb_aspec aspec; int ma, op, ra, pa, arity; @@ -293,40 +311,20 @@ mrb_proc_arity(const struct RProc *p) return arity; } -static void -tempirep_free(mrb_state *mrb, void *p) -{ - if (p) mrb_irep_free(mrb, (mrb_irep *)p); -} - -static const mrb_data_type tempirep_type = { "temporary irep", tempirep_free }; - void mrb_init_proc(mrb_state *mrb) { struct RProc *p; mrb_method_t m; - struct RData *irep_obj = mrb_data_object_alloc(mrb, mrb->object_class, NULL, &tempirep_type); - mrb_irep *call_irep; - static const mrb_irep mrb_irep_zero = { 0 }; - - call_irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep)); - irep_obj->data = call_irep; - *call_irep = mrb_irep_zero; - call_irep->flags = MRB_ISEQ_NO_FREE; - call_irep->iseq = call_iseq; - call_irep->ilen = 1; - call_irep->nregs = 2; /* receiver and block */ mrb_define_class_method(mrb, mrb->proc_class, "new", mrb_proc_s_new, MRB_ARGS_NONE()|MRB_ARGS_BLOCK()); mrb_define_method(mrb, mrb->proc_class, "initialize_copy", mrb_proc_init_copy, MRB_ARGS_REQ(1)); mrb_define_method(mrb, mrb->proc_class, "arity", proc_arity, MRB_ARGS_NONE()); - p = mrb_proc_new(mrb, call_irep); - irep_obj->data = NULL; + p = mrb_proc_new(mrb, &call_irep); MRB_METHOD_FROM_PROC(m, p); - mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern_lit(mrb, "call"), m); - mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern_lit(mrb, "[]"), m); + mrb_define_method_raw(mrb, mrb->proc_class, MRB_SYM(call), m); + mrb_define_method_raw(mrb, mrb->proc_class, MRB_QSYM(aref), m); mrb_define_class_method(mrb, mrb->kernel_module, "lambda", proc_lambda, MRB_ARGS_NONE()|MRB_ARGS_BLOCK()); /* 15.3.1.2.6 */ mrb_define_method(mrb, mrb->kernel_module, "lambda", proc_lambda, MRB_ARGS_NONE()|MRB_ARGS_BLOCK()); /* 15.3.1.3.27 */ |
