fix up the __MOP__ slot when reblessing to or from anon classes
Jesse Luehrs [Thu, 30 Sep 2010 22:36:50 +0000 (17:36 -0500)]
lib/Class/MOP/Class.pm

index 122ed9a..0c9e9d7 100644 (file)
@@ -681,6 +681,10 @@ sub _force_rebless_instance {
     $old_metaclass->rebless_instance_away($instance, $self, %params)
         if $old_metaclass;
 
+    if ($old_metaclass->is_anon_class) {
+        delete $instance->{__MOP__};
+    }
+
     my $meta_instance = $self->get_meta_instance;
 
     # rebless!
@@ -688,6 +692,19 @@ sub _force_rebless_instance {
     $meta_instance->rebless_instance_structure($_[1], $self);
 
     $self->_fixup_attributes_after_rebless($instance, $old_metaclass, %params);
+
+    # NOTE:
+    # this will only work for a HASH instance type
+    if ($self->is_anon_class) {
+        (reftype($instance) eq 'HASH')
+            || confess "Currently only HASH based instances are supported with instance of anon-classes";
+        # NOTE:
+        # At some point we should make this official
+        # as a reserved slot name, but right now I am
+        # going to keep it here.
+        # my $RESERVED_MOP_SLOT = '__MOP__';
+        $instance->{'__MOP__'} = $self;
+    }
 }
 
 sub rebless_instance {