From: Jesse Luehrs <doy@tozt.net>
Date: Fri, 1 Oct 2010 04:59:32 +0000 (-0500)
Subject: the __MOP__ slot should be used for any weak class, not just anon
X-Git-Tag: 1.10~6
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=18ca857d2b99665a1243a6cf310d237bf697e7b9;p=gitmo%2FClass-MOP.git

the __MOP__ slot should be used for any weak class, not just anon
---

diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm
index 6648927..a40c1c8 100644
--- a/lib/Class/MOP/Class.pm
+++ b/lib/Class/MOP/Class.pm
@@ -604,7 +604,7 @@ sub _construct_instance {
     }
     # NOTE:
     # this will only work for a HASH instance type
-    if ($class->is_anon_class) {
+    if (Class::MOP::metaclass_is_weak($class->name)) {
         (reftype($instance) eq 'HASH')
             || confess "Currently only HASH based instances are supported with instance of anon-classes";
         # NOTE:
@@ -686,7 +686,7 @@ sub _force_rebless_instance {
     $old_metaclass->rebless_instance_away($instance, $self, %params)
         if $old_metaclass;
 
-    if ($old_metaclass->is_anon_class) {
+    if (Class::MOP::metaclass_is_weak($old_metaclass->name)) {
         delete $instance->{__MOP__};
     }
 
@@ -700,7 +700,7 @@ sub _force_rebless_instance {
 
     # NOTE:
     # this will only work for a HASH instance type
-    if ($self->is_anon_class) {
+    if (Class::MOP::metaclass_is_weak($self->name)) {
         (reftype($instance) eq 'HASH')
             || confess "Currently only HASH based instances are supported with instance of anon-classes";
         # NOTE:
diff --git a/t/048_anon_class_create_init.t b/t/048_anon_class_create_init.t
index e24319c..4cda746 100644
--- a/t/048_anon_class_create_init.t
+++ b/t/048_anon_class_create_init.t
@@ -117,4 +117,20 @@ my $instance;
     }
 }
 
+{
+    my $meta = Class::MOP::Class->create(
+        'Baz',
+        weaken => 1,
+    );
+    $instance = $meta->new_object;
+}
+{
+    my $meta = Class::MOP::class_of($instance);
+    Scalar::Util::weaken($meta);
+    ok($meta, "weak class is kept alive by existing instances");
+
+    undef $instance;
+    ok(!$meta, "weak class is collected once instances go away");
+}
+
 done_testing;