mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #3570 from keizo042/fix_cv_definition_in_singleton
Modify class variable definition in singleton class; fix #3539
This commit is contained in:
+14
-4
@@ -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;
|
||||
}
|
||||
|
||||
mrb_write_barrier(mrb, (struct RBasic*)cls);
|
||||
iv_put(mrb, cls->iv, sym, v);
|
||||
if (!c->iv) {
|
||||
c->iv = iv_new(mrb);
|
||||
}
|
||||
|
||||
mrb_write_barrier(mrb, (struct RBasic*)c);
|
||||
iv_put(mrb, c->iv, sym, v);
|
||||
}
|
||||
|
||||
MRB_API void
|
||||
|
||||
@@ -384,6 +384,19 @@ assert('class variable and class << self style class method') do
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user