Fixed UPVAR gotchas; fix #3835

Both `uvenv` function and `env` generation in `create_proc_from_string`
function have bugs to handling enclosed environment objects.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-10-28 21:09:25 +09:00
parent 57dad6e3fd
commit c7c9543bed
2 changed files with 46 additions and 33 deletions
+16 -5
View File
@@ -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*