diff options
Diffstat (limited to 'mrbgems/mruby-eval/src/eval.c')
| -rw-r--r-- | mrbgems/mruby-eval/src/eval.c | 373 |
1 files changed, 99 insertions, 274 deletions
diff --git a/mrbgems/mruby-eval/src/eval.c b/mrbgems/mruby-eval/src/eval.c index e6a82723d..de1bcd02d 100644 --- a/mrbgems/mruby-eval/src/eval.c +++ b/mrbgems/mruby-eval/src/eval.c @@ -5,248 +5,68 @@ #include <mruby/proc.h> #include <mruby/opcode.h> #include <mruby/error.h> +#include <mruby/presym.h> +#include <mruby/variable.h> +struct REnv *mrb_env_new(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci, int nstacks, mrb_value *stack, struct RClass *tc); mrb_value mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p); mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self); - -static struct mrb_irep * -get_closure_irep(mrb_state *mrb, int level) -{ - struct RProc *proc = mrb->c->ci[-1].proc; - - while (level--) { - if (!proc) return NULL; - proc = proc->upper; - } - if (!proc) return NULL; - if (MRB_PROC_CFUNC_P(proc)) { - return NULL; - } - return proc->body.irep; -} - -/* search for irep lev above the bottom */ -static mrb_irep* -search_irep(mrb_irep *top, int bnest, int lev, mrb_irep *bottom) -{ - int i; - - for (i=0; i<top->rlen; i++) { - mrb_irep* tmp = top->reps[i]; - - if (tmp == bottom) return top; - tmp = search_irep(tmp, bnest-1, lev, bottom); - if (tmp) { - if (bnest == lev) return top; - return tmp; - } - } - return NULL; -} - -static uint16_t -search_variable(mrb_state *mrb, mrb_sym vsym, int bnest) -{ - mrb_irep *virep; - int level; - int pos; - - for (level = 0; (virep = get_closure_irep(mrb, level)); level++) { - if (virep->lv == NULL) { - continue; - } - for (pos = 0; pos < virep->nlocals - 1; pos++) { - if (vsym == virep->lv[pos].name) { - return (pos+1)<<8 | (level+bnest); - } - } - } - - return 0; -} - -static int -irep_argc(mrb_irep *irep) -{ - mrb_code c; - - c = irep->iseq[0]; - if (c == OP_ENTER) { - mrb_aspec ax = PEEK_W(irep->iseq+1); - /* extra 1 means a slot for block */ - return MRB_ASPEC_REQ(ax)+MRB_ASPEC_OPT(ax)+MRB_ASPEC_REST(ax)+MRB_ASPEC_POST(ax)+1; - } - return 0; -} - -static mrb_bool -potential_upvar_p(struct mrb_locals *lv, uint16_t v, int argc, uint16_t nlocals) -{ - if (v >= nlocals) return FALSE; - /* skip arguments */ - if (v < argc+1) return FALSE; - return TRUE; -} - -extern uint8_t mrb_insn_size[]; -extern uint8_t mrb_insn_size1[]; -extern uint8_t mrb_insn_size2[]; -extern uint8_t mrb_insn_size3[]; - -static void -patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top) -{ - int i; - uint32_t a; - uint16_t b; - uint8_t c; - mrb_code insn; - int argc = irep_argc(irep); - - for (i = 0; i < irep->ilen; ) { - insn = irep->iseq[i]; - switch(insn){ - case OP_EPUSH: - b = PEEK_S(irep->iseq+i+1); - patch_irep(mrb, irep->reps[b], bnest + 1, top); - break; - - case OP_LAMBDA: - case OP_BLOCK: - a = PEEK_B(irep->iseq+i+1); - b = PEEK_B(irep->iseq+i+2); - patch_irep(mrb, irep->reps[b], bnest + 1, top); - break; - - case OP_SEND: - b = PEEK_B(irep->iseq+i+2); - c = PEEK_B(irep->iseq+i+3); - if (c != 0) { - break; - } - else { - uint16_t arg = search_variable(mrb, irep->syms[b], bnest); - if (arg != 0) { - /* must replace */ - irep->iseq[i] = OP_GETUPVAR; - irep->iseq[i+2] = arg >> 8; - irep->iseq[i+3] = arg & 0xff; - } - } - break; - - case OP_MOVE: - a = PEEK_B(irep->iseq+i+1); - b = PEEK_B(irep->iseq+i+2); - /* src part */ - if (potential_upvar_p(irep->lv, b, argc, irep->nlocals)) { - uint16_t arg = search_variable(mrb, irep->lv[b - 1].name, bnest); - if (arg != 0) { - /* must replace */ - irep->iseq[i] = insn = OP_GETUPVAR; - irep->iseq[i+2] = arg >> 8; - irep->iseq[i+3] = arg & 0xff; - } - } - /* dst part */ - if (potential_upvar_p(irep->lv, a, argc, irep->nlocals)) { - uint16_t arg = search_variable(mrb, irep->lv[a - 1].name, bnest); - if (arg != 0) { - /* must replace */ - irep->iseq[i] = insn = OP_SETUPVAR; - irep->iseq[i+1] = (mrb_code)b; - irep->iseq[i+2] = arg >> 8; - irep->iseq[i+3] = arg & 0xff; - } - } - break; - - case OP_GETUPVAR: - a = PEEK_B(irep->iseq+i+1); - b = PEEK_B(irep->iseq+i+2); - c = PEEK_B(irep->iseq+i+3); - { - int lev = c+1; - mrb_irep *tmp = search_irep(top, bnest, lev, irep); - if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) { - uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest); - if (arg != 0) { - /* must replace */ - irep->iseq[i] = OP_GETUPVAR; - irep->iseq[i+2] = arg >> 8; - irep->iseq[i+3] = arg & 0xff; - } - } - } - break; - - case OP_SETUPVAR: - a = PEEK_B(irep->iseq+i+1); - b = PEEK_B(irep->iseq+i+2); - c = PEEK_B(irep->iseq+i+3); - { - int lev = c+1; - mrb_irep *tmp = search_irep(top, bnest, lev, irep); - if (potential_upvar_p(tmp->lv, b, irep_argc(tmp), tmp->nlocals)) { - uint16_t arg = search_variable(mrb, tmp->lv[b-1].name, bnest); - if (arg != 0) { - /* must replace */ - irep->iseq[i] = OP_SETUPVAR; - irep->iseq[i+1] = a; - irep->iseq[i+2] = arg >> 8; - irep->iseq[i+3] = arg & 0xff; - } - } - } - break; - - case OP_EXT1: - insn = PEEK_B(irep->iseq+1); - i += mrb_insn_size1[insn]; - continue; - case OP_EXT2: - insn = PEEK_B(irep->iseq+1); - i += mrb_insn_size2[insn]; - continue; - case OP_EXT3: - insn = PEEK_B(irep->iseq+1); - i += mrb_insn_size3[insn]; - continue; - } - i+=mrb_insn_size[insn]; - } -} - +mrb_value mrb_mod_module_eval(mrb_state*, mrb_value); void mrb_codedump_all(mrb_state*, struct RProc*); +void mrb_proc_merge_lvar(mrb_state *mrb, mrb_irep *irep, struct REnv *env, int num, const mrb_sym *lv, const mrb_value *stack); static struct RProc* -create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line) +create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line) { mrbc_context *cxt; struct mrb_parser_state *p; struct RProc *proc; + const struct RProc *scope; struct REnv *e; mrb_callinfo *ci; /* callinfo of eval caller */ struct RClass *target_class = NULL; - int bidx; + struct mrb_context *c = mrb->c; if (!mrb_nil_p(binding)) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil."); + mrb_value scope_obj; + if (!mrb_class_defined_id(mrb, MRB_SYM(Binding)) + || !mrb_obj_is_kind_of(mrb, binding, mrb_class_get_id(mrb, MRB_SYM(Binding)))) { + mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %C (expected binding)", + mrb_obj_class(mrb, binding)); + } + scope_obj = mrb_iv_get(mrb, binding, MRB_SYM(proc)); + mrb_assert(mrb_proc_p(scope_obj)); + scope = mrb_proc_ptr(scope_obj); + if (MRB_PROC_CFUNC_P(scope)) { + e = NULL; + } + else { + mrb_value env = mrb_iv_get(mrb, binding, MRB_SYM(env)); + mrb_assert(mrb_env_p(env)); + e = (struct REnv *)mrb_obj_ptr(env); + mrb_assert(e != NULL); + } + } + else { + ci = (c->ci > c->cibase) ? c->ci - 1 : c->cibase; + scope = ci->proc; + e = NULL; } cxt = mrbc_context_new(mrb); - cxt->lineno = (short)line; + cxt->lineno = (uint16_t)line; mrbc_filename(mrb, cxt, file ? file : "(eval)"); cxt->capture_errors = TRUE; cxt->no_optimize = TRUE; - cxt->on_eval = TRUE; + cxt->upper = scope && MRB_PROC_CFUNC_P(scope) ? NULL : scope; p = mrb_parse_nstring(mrb, s, len, cxt); /* only occur when memory ran out */ if (!p) { - mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state."); + mrbc_context_free(mrb, cxt); + mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state (out of memory)"); } if (0 < p->nerr) { @@ -254,15 +74,15 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, mrb_value str; if (file) { - str = mrb_format(mrb, " file %S line %S: %S", - mrb_str_new_cstr(mrb, file), - mrb_fixnum_value(p->error_buffer[0].lineno), - mrb_str_new_cstr(mrb, p->error_buffer[0].message)); + str = mrb_format(mrb, "file %s line %d: %s", + file, + p->error_buffer[0].lineno, + p->error_buffer[0].message); } else { - str = mrb_format(mrb, " line %S: %S", - mrb_fixnum_value(p->error_buffer[0].lineno), - mrb_str_new_cstr(mrb, p->error_buffer[0].message)); + str = mrb_format(mrb, "line %d: %s", + p->error_buffer[0].lineno, + p->error_buffer[0].message); } mrb_parser_free(p); mrbc_context_free(mrb, cxt); @@ -276,39 +96,30 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, mrbc_context_free(mrb, cxt); mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error"); } - if (mrb->c->ci > mrb->c->cibase) { - ci = &mrb->c->ci[-1]; + if (c->ci > c->cibase) { + ci = &c->ci[-1]; } else { - ci = mrb->c->cibase; - } - if (ci->proc) { - target_class = MRB_PROC_TARGET_CLASS(ci->proc); - } - if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) { - if (ci->env) { - e = ci->env; - } - else { - e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, - (struct RClass*)target_class); - e->mid = ci->mid; - e->stack = ci[1].stackent; - e->cxt = mrb->c; - MRB_ENV_SET_STACK_LEN(e, ci->proc->body.irep->nlocals); - bidx = ci->argc; - if (ci->argc < 0) bidx = 2; - else bidx += 1; - MRB_ENV_SET_BIDX(e, bidx); - ci->env = e; + ci = c->cibase; + } + if (scope) { + target_class = MRB_PROC_TARGET_CLASS(scope); + if (!MRB_PROC_CFUNC_P(scope)) { + if (e == NULL) { + /* when `binding` is nil */ + e = mrb_vm_ci_env(ci); + if (e == NULL) { + e = mrb_env_new(mrb, c, ci, ci->proc->body.irep->nlocals, ci->stack, target_class); + ci->u.env = e; + } + } + proc->e.env = e; + proc->flags |= MRB_PROC_ENVSET; + mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e); } - proc->e.env = e; - proc->flags |= MRB_PROC_ENVSET; - mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e); } - proc->upper = ci->proc; - mrb->c->ci->target_class = target_class; - patch_irep(mrb, proc->body.irep, 0, proc->body.irep); + proc->upper = scope; + mrb_vm_ci_target_class_set(mrb->c->ci, target_class); /* mrb_codedump_all(mrb, proc); */ mrb_parser_free(p); @@ -322,33 +133,27 @@ exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc) { /* no argument passed from eval() */ mrb->c->ci->argc = 0; - if (mrb->c->ci->acc < 0) { - ptrdiff_t cioff = mrb->c->ci - mrb->c->cibase; - mrb_value ret = mrb_top_run(mrb, proc, self, 0); - if (mrb->exc) { - mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); - } - mrb->c->ci = mrb->c->cibase + cioff; - return ret; - } /* clear block */ - mrb->c->stack[1] = mrb_nil_value(); + mrb->c->ci->stack[1] = mrb_nil_value(); return mrb_exec_irep(mrb, self, proc); } static mrb_value f_eval(mrb_state *mrb, mrb_value self) { - char *s; + const char *s; mrb_int len; mrb_value binding = mrb_nil_value(); - char *file = NULL; + const char *file = NULL; mrb_int line = 1; struct RProc *proc; mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line); proc = create_proc_from_string(mrb, s, len, binding, file, line); + if (!mrb_nil_p(binding)) { + self = mrb_iv_get(mrb, binding, MRB_SYM(recv)); + } mrb_assert(!MRB_PROC_CFUNC_P(proc)); return exec_irep(mrb, self, proc); } @@ -356,15 +161,10 @@ f_eval(mrb_state *mrb, mrb_value self) static mrb_value f_instance_eval(mrb_state *mrb, mrb_value self) { - mrb_value b; - mrb_int argc; mrb_value *argv; - - mrb_get_args(mrb, "*!&", &argv, &argc, &b); - - if (mrb_nil_p(b)) { - char *s; + if (!mrb_block_given_p(mrb)) { + const char *s; mrb_int len; - char *file = NULL; + const char *file = NULL; mrb_int line = 1; mrb_value cv; struct RProc *proc; @@ -374,20 +174,45 @@ f_instance_eval(mrb_state *mrb, mrb_value self) proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line); MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv)); mrb_assert(!MRB_PROC_CFUNC_P(proc)); - mrb->c->ci->target_class = mrb_class_ptr(cv); + mrb_vm_ci_target_class_set(mrb->c->ci, mrb_class_ptr(cv)); return exec_irep(mrb, self, proc); } else { - mrb_get_args(mrb, "&", &b); + mrb_get_args(mrb, ""); return mrb_obj_instance_eval(mrb, self); } } +static mrb_value +f_class_eval(mrb_state *mrb, mrb_value self) +{ + if (!mrb_block_given_p(mrb)) { + const char *s; + mrb_int len; + const char *file = NULL; + mrb_int line = 1; + struct RProc *proc; + + mrb_get_args(mrb, "s|zi", &s, &len, &file, &line); + proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line); + MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(self)); + mrb_assert(!MRB_PROC_CFUNC_P(proc)); + mrb_vm_ci_target_class_set(mrb->c->ci, mrb_class_ptr(self)); + return exec_irep(mrb, self, proc); + } + else { + mrb_get_args(mrb, ""); + return mrb_mod_module_eval(mrb, self); + } +} + void mrb_mruby_eval_gem_init(mrb_state* mrb) { mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3)); - mrb_define_method(mrb, mrb->kernel_module, "instance_eval", f_instance_eval, MRB_ARGS_ARG(1, 2)); + mrb_define_method_id(mrb, mrb_class_get_id(mrb, MRB_SYM(BasicObject)), MRB_SYM(instance_eval), f_instance_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK()); + mrb_define_method_id(mrb, mrb_class_get_id(mrb, MRB_SYM(Module)), MRB_SYM(module_eval), f_class_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK()); + mrb_define_method_id(mrb, mrb_class_get_id(mrb, MRB_SYM(Module)), MRB_SYM(class_eval), f_class_eval, MRB_ARGS_OPT(3)|MRB_ARGS_BLOCK()); } void |
