summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/vm.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/vm.c b/src/vm.c
index 9758ccf1e..b1e2b624b 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -217,13 +217,24 @@ uvenv(mrb_state *mrb, int up)
struct RProc *proc = mrb->c->ci->proc;
struct REnv *e;
- do {
- e = MRB_PROC_ENV(proc);
- if (!e) return NULL;
+ while (up--) {
proc = proc->upper;
if (!proc) return NULL;
- } while (up--);
- return e;
+ }
+ e = MRB_PROC_ENV(proc);
+ if (e) return e; /* proc has enclosed env */
+ else {
+ mrb_callinfo *ci = mrb->c->ci;
+ mrb_callinfo *cb = mrb->c->cibase;
+
+ while (cb <= ci) {
+ if (ci->proc == proc) {
+ return ci->env;
+ }
+ ci--;
+ }
+ }
+ return NULL;
}
static inline struct RProc*