Skip to content
Browse files

when including a module into a class, copy the module's class variables

  • Loading branch information...
1 parent fa884b7 commit ee78786256b1b96f559dad53ef87759093d6e2af Laurent Sansonetti committed Aug 30, 2011
Showing with 24 additions and 2 deletions.
  1. +5 −2 class.c
  2. +1 −0 class.h
  3. +18 −0 variable.c
View
7 class.c
@@ -687,12 +687,15 @@ rb_include_module2(VALUE klass, VALUE orig_klass, VALUE module, bool check,
selInitialize);
Method m2 = class_getInstanceMethod((Class)klass, selInitialize);
if (m != NULL && m2 != NULL
- && method_getImplementation(m) == (IMP)rb_objc_init
- && method_getImplementation(m2) != (IMP)rb_objc_init) {
+ && method_getImplementation(m) == (IMP)rb_objc_init
+ && method_getImplementation(m2) != (IMP)rb_objc_init) {
rb_vm_copy_method((Class)orig_klass, m2);
}
}
}
+
+ // Copy the module's class variables.
+ rb_class_merge_ivar_dicts(module, klass);
}
void
View
1 class.h
@@ -130,6 +130,7 @@ rb_class_set_flags(Class k, unsigned long flags)
CFMutableDictionaryRef rb_class_ivar_dict(VALUE);
CFMutableDictionaryRef rb_class_ivar_dict_or_create(VALUE);
void rb_class_ivar_set_dict(VALUE, CFMutableDictionaryRef);
+void rb_class_merge_ivar_dicts(VALUE orig_class, VALUE dest_class);
typedef enum {
SCOPE_DEFAULT = 0, // public for everything but Object
View
18 variable.c
@@ -946,6 +946,24 @@ rb_class_ivar_set_dict(VALUE mod, CFMutableDictionaryRef dict)
generic_ivar_dict_set(mod, dict);
}
+void
+merge_ivars(const void *key, const void *val, void *ctx)
+{
+ CFMutableDictionaryRef dest_dict = (CFMutableDictionaryRef)ctx;
+ CFDictionarySetValue(dest_dict, key, val);
+}
+
+void
+rb_class_merge_ivar_dicts(VALUE orig_class, VALUE dest_class)
+{
+ CFMutableDictionaryRef orig_dict = rb_class_ivar_dict(orig_class);
+ if (orig_dict != NULL) {
+ CFMutableDictionaryRef dest_dict =
+ rb_class_ivar_dict_or_create(dest_class);
+ CFDictionaryApplyFunction(orig_dict, merge_ivars, dest_dict);
+ }
+}
+
CFMutableDictionaryRef
rb_class_ivar_dict_or_create(VALUE mod)
{

0 comments on commit ee78786

Please sign in to comment.
Something went wrong with that request. Please try again.