summaryrefslogtreecommitdiffhomepage
path: root/src/variable.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/variable.c')
-rw-r--r--src/variable.c44
1 files changed, 20 insertions, 24 deletions
diff --git a/src/variable.c b/src/variable.c
index 50fc70682..c82f2a822 100644
--- a/src/variable.c
+++ b/src/variable.c
@@ -632,19 +632,18 @@ mrb_cv_defined(mrb_state *mrb, mrb_value mod, mrb_sym sym)
mrb_value
mrb_vm_cv_get(mrb_state *mrb, mrb_sym sym)
{
- struct RClass *c = mrb->c->ci->proc->target_class;
-
- if (!c) c = mrb->c->ci->target_class;
+ struct RClass *c;
+ c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
return mrb_mod_cv_get(mrb, c, sym);
}
void
mrb_vm_cv_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
{
- struct RClass *c = mrb->c->ci->proc->target_class;
+ struct RClass *c;
- if (!c) c = mrb->c->ci->target_class;
+ c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
mrb_mod_cv_set(mrb, c, sym, v);
}
@@ -663,17 +662,18 @@ mod_const_check(mrb_state *mrb, mrb_value mod)
}
static mrb_value
-const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym)
+const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym, mrb_bool top)
{
struct RClass *c = base;
mrb_value v;
iv_tbl *t;
mrb_bool retry = FALSE;
mrb_value name;
+ struct RClass *oclass = mrb->object_class;
L_RETRY:
while (c) {
- if (c->iv) {
+ if (c->iv && (top || c != oclass || base == oclass)) {
t = c->iv;
if (iv_get(mrb, t, sym, &v))
return v;
@@ -693,20 +693,18 @@ MRB_API mrb_value
mrb_const_get(mrb_state *mrb, mrb_value mod, mrb_sym sym)
{
mod_const_check(mrb, mod);
- return const_get(mrb, mrb_class_ptr(mod), sym);
+ return const_get(mrb, mrb_class_ptr(mod), sym, FALSE);
}
mrb_value
mrb_vm_const_get(mrb_state *mrb, mrb_sym sym)
{
- struct RClass *c = mrb->c->ci->proc->target_class;
+ struct RClass *c;
struct RClass *c2;
mrb_value v;
- mrb_irep *irep;
-
- if (!c) c = mrb->c->ci->target_class;
- mrb_assert(c != NULL);
+ struct RProc *proc;
+ c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
if (c->iv && iv_get(mrb, c->iv, sym, &v)) {
return v;
}
@@ -719,17 +717,15 @@ mrb_vm_const_get(mrb_state *mrb, mrb_sym sym)
}
if (c2->tt == MRB_TT_CLASS || c2->tt == MRB_TT_MODULE) c = c2;
mrb_assert(!MRB_PROC_CFUNC_P(mrb->c->ci->proc));
- irep = mrb->c->ci->proc->body.irep;
- while (irep) {
- if (irep->target_class) {
- c2 = irep->target_class;
- if (c2->iv && iv_get(mrb, c2->iv, sym, &v)) {
- return v;
- }
+ proc = mrb->c->ci->proc;
+ while (proc) {
+ c2 = MRB_PROC_TARGET_CLASS(proc);
+ if (c2 && c2->iv && iv_get(mrb, c2->iv, sym, &v)) {
+ return v;
}
- irep = irep->outer;
+ proc = proc->upper;
}
- return const_get(mrb, c, sym);
+ return const_get(mrb, c, sym, TRUE);
}
MRB_API void
@@ -745,9 +741,9 @@ mrb_const_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
void
mrb_vm_const_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
{
- struct RClass *c = mrb->c->ci->proc->target_class;
+ struct RClass *c;
- if (!c) c = mrb->c->ci->target_class;
+ c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
mrb_obj_iv_set(mrb, (struct RObject*)c, sym, v);
}