Permalink
Browse files

Modify class variable definition in singleton class; fix #3539

  • Loading branch information...
keizo042 committed Mar 30, 2017
1 parent 898077a commit 5efe77fede8c3d57f8be57f5e5b26ab77b8d9c9c
Showing with 27 additions and 4 deletions.
  1. +14 −4 src/variable.c
  2. +13 −0 test/t/class.rb
View
@@ -819,12 +819,22 @@ mrb_mod_cv_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value v)
c = c->super;
}
if (!cls->iv) {
cls->iv = iv_new(mrb);
if (cls && cls->tt == MRB_TT_SCLASS) {
mrb_value klass;
klass = mrb_obj_iv_get(mrb, (struct RObject*)cls,
mrb_intern_lit(mrb, "__attached__"));
c = mrb_class_ptr(klass);
}else{
c = cls;
}
if (!c->iv) {
c->iv = iv_new(mrb);
}
mrb_write_barrier(mrb, (struct RBasic*)cls);
iv_put(mrb, cls->iv, sym, v);
mrb_write_barrier(mrb, (struct RBasic*)c);
iv_put(mrb, c->iv, sym, v);
}
MRB_API void
View
@@ -384,6 +384,19 @@ def class_variable
assert_equal("value", ClassVariableTest.class_variable)
end
assert('class variable definition in singleton_class') do
class ClassVariableDefinitionInSingletonTest
class << self
@@class_variable = "value"
end
def class_variable
@@class_variable
end
end
assert_equal("value", ClassVariableDefinitionInSingletonTest.new.class_variable)
end
assert('class variable in module and class << self style class method') do
module ClassVariableInModuleTest
@@class_variable = "value"

0 comments on commit 5efe77f

Please sign in to comment.