summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-eval/src
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-eval/src')
-rw-r--r--mrbgems/mruby-eval/src/eval.c115
1 files changed, 89 insertions, 26 deletions
diff --git a/mrbgems/mruby-eval/src/eval.c b/mrbgems/mruby-eval/src/eval.c
index 08ef8d937..d85c829d7 100644
--- a/mrbgems/mruby-eval/src/eval.c
+++ b/mrbgems/mruby-eval/src/eval.c
@@ -6,12 +6,13 @@
#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_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p, mrb_func_t posthook);
mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
-
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, const char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line)
@@ -19,12 +20,36 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
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;
+ 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(mrb, "Binding")
+ || !mrb_obj_is_kind_of(mrb, binding, mrb_class_get(mrb, "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);
@@ -33,8 +58,7 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
mrbc_filename(mrb, cxt, file ? file : "(eval)");
cxt->capture_errors = TRUE;
cxt->no_optimize = TRUE;
- ci = (mrb->c->ci > mrb->c->cibase) ? mrb->c->ci - 1 : mrb->c->cibase;
- cxt->upper = ci->proc && MRB_PROC_CFUNC_P(ci->proc) ? NULL : ci->proc;
+ cxt->upper = scope && MRB_PROC_CFUNC_P(scope) ? NULL : scope;
p = mrb_parse_nstring(mrb, s, len, cxt);
@@ -70,28 +94,29 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
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);
+ ci = c->cibase;
}
- if (ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
- if ((e = mrb_vm_ci_env(ci)) != NULL) {
- /* do nothing, because e is assigned already */
- }
- else {
- e = mrb_env_new(mrb, mrb->c, ci, ci->proc->body.irep->nlocals, ci->stack, target_class);
- ci->u.env = e;
+ 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;
+ proc->upper = scope;
mrb_vm_ci_target_class_set(mrb->c->ci, target_class);
/* mrb_codedump_all(mrb, proc); */
@@ -102,7 +127,7 @@ create_proc_from_string(mrb_state *mrb, const char *s, mrb_int len, mrb_value bi
}
static mrb_value
-exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
+exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc, mrb_func_t posthook)
{
/* no argument passed from eval() */
mrb->c->ci->argc = 0;
@@ -117,7 +142,38 @@ exec_irep(mrb_state *mrb, mrb_value self, struct RProc *proc)
}
/* clear block */
mrb->c->ci->stack[1] = mrb_nil_value();
- return mrb_exec_irep(mrb, self, proc);
+ return mrb_exec_irep(mrb, self, proc, posthook);
+}
+
+static void
+eval_merge_lvar(mrb_state *mrb, mrb_irep *irep, struct REnv *env, int num, const mrb_sym *lv, const mrb_value *stack)
+{
+ mrb_assert(mrb->c->stend >= stack + num);
+ mrb_proc_merge_lvar(mrb, irep, env, num, lv, stack);
+}
+
+static mrb_value
+eval_merge_lvar_hook(mrb_state *mrb, mrb_value dummy_self)
+{
+ const mrb_callinfo *orig_ci = &mrb->c->ci[1];
+ const struct RProc *orig_proc = orig_ci->proc;
+ const mrb_irep *orig_irep = orig_proc->body.irep;
+ int orig_nlocals = orig_irep->nlocals;
+
+ if (orig_nlocals > 1) {
+ struct RProc *proc = (struct RProc *)orig_proc->upper;
+ struct REnv *env = MRB_PROC_ENV(orig_proc);
+ eval_merge_lvar(mrb, (mrb_irep *)proc->body.irep, env,
+ orig_nlocals - 1, orig_irep->lv,
+ mrb->c->ci->stack + 3 /* hook proc + exc + ret val */);
+ }
+
+ mrb_value exc = mrb->c->ci->stack[1];
+ if (!mrb_nil_p(exc)) {
+ mrb_exc_raise(mrb, exc);
+ }
+
+ return mrb->c->ci->stack[2];
}
static mrb_value
@@ -129,12 +185,19 @@ f_eval(mrb_state *mrb, mrb_value self)
const char *file = NULL;
mrb_int line = 1;
struct RProc *proc;
+ mrb_func_t posthook = NULL;
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));
+ if (mrb_env_p(mrb_iv_get(mrb, binding, MRB_SYM(env)))) {
+ posthook = eval_merge_lvar_hook;
+ }
+ }
mrb_assert(!MRB_PROC_CFUNC_P(proc));
- return exec_irep(mrb, self, proc);
+ return exec_irep(mrb, self, proc, posthook);
}
static mrb_value
@@ -159,7 +222,7 @@ f_instance_eval(mrb_state *mrb, mrb_value self)
MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
mrb_assert(!MRB_PROC_CFUNC_P(proc));
mrb_vm_ci_target_class_set(mrb->c->ci, mrb_class_ptr(cv));
- return exec_irep(mrb, self, proc);
+ return exec_irep(mrb, self, proc, NULL);
}
else {
mrb_get_args(mrb, "&", &b);