Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Modify class variable definition in singleton class; fix #3539
  • Loading branch information
keizo042 committed Mar 30, 2017
1 parent 898077a commit 5efe77f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/variable.c
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/t/class.rb
Expand Up @@ -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"
Expand Down

0 comments on commit 5efe77f

Please sign in to comment.