From d75a907edf761b823d83aa658025de52508b8966 Mon Sep 17 00:00:00 2001 From: fleuria Date: Sun, 3 Nov 2013 11:32:59 +0800 Subject: introduce mrb_context_run() currently there are two scnenario to call mrb_run(), the first is calling a proc, in this case mrb should create a new environment, discarding all the variables except args, reciever and block. the second is calling the newly generated irep, like in mirb. in this case, the variables should be kept after mrb_run(). so we introduce mrb_context_run() to handle this seperately. --- src/load.c | 4 ++-- src/vm.c | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/load.c b/src/load.c index e2f74d504..3d7b54eef 100644 --- a/src/load.c +++ b/src/load.c @@ -510,7 +510,7 @@ mrb_load_irep(mrb_state *mrb, const uint8_t *bin) irep_error(mrb, n); return mrb_nil_value(); } - return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + return mrb_context_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); } #ifdef ENABLE_STDIO @@ -760,6 +760,6 @@ mrb_load_irep_file(mrb_state *mrb, FILE* fp) irep_error(mrb, n); return mrb_nil_value(); } - return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + return mrb_context_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); } #endif /* ENABLE_STDIO */ diff --git a/src/vm.c b/src/vm.c index 1099db8a1..d15358d42 100644 --- a/src/vm.c +++ b/src/vm.c @@ -546,8 +546,8 @@ void mrb_gv_val_set(mrb_state *mrb, mrb_sym sym, mrb_value val); #define CALL_MAXARGS 127 -mrb_value -mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) +static mrb_value +run_proc(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep) { /* mrb_assert(mrb_proc_cfunc_p(proc)) */ mrb_irep *irep = proc->body.irep; @@ -595,7 +595,7 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) if (!mrb->c->stack) { stack_init(mrb); } - stack_extend(mrb, irep->nregs, mrb->c->ci->argc + 2); /* argc + 2 (receiver and block) */ + stack_extend(mrb, irep->nregs, stack_keep); mrb->c->ci->err = pc; mrb->c->ci->proc = proc; mrb->c->ci->nregs = irep->nregs + 1; @@ -2145,6 +2145,18 @@ mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) END_DISPATCH; } +mrb_value +mrb_run(mrb_state *mrb, struct RProc *proc, mrb_value self) +{ + return run_proc(mrb, proc, self, mrb->c->ci->argc + 2); /* argc + 2 (receiver and block) */ +} + +mrb_value +mrb_context_run(mrb_state *mrb, struct RProc *proc, mrb_value self) +{ + return run_proc(mrb, proc, self, proc->body.irep->nregs); +} + void mrb_longjmp(mrb_state *mrb) { -- cgit v1.2.3