From: Jesse Luehrs Date: Fri, 1 Oct 2010 02:37:07 +0000 (-0500) Subject: add Class::MOP::metaclass_is_weak X-Git-Tag: 1.10~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=73dad89f199f7fd5272004d750c0b2acb24ba450;p=gitmo%2FClass-MOP.git add Class::MOP::metaclass_is_weak --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 3059968..7e6b01d 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -9,7 +9,7 @@ use 5.008; use MRO::Compat; use Carp 'confess'; -use Scalar::Util 'weaken', 'reftype', 'blessed'; +use Scalar::Util 'weaken', 'isweak', 'reftype', 'blessed'; use Data::OptList; use Try::Tiny; @@ -51,6 +51,7 @@ XSLoader::load( __PACKAGE__, $XS_VERSION ); sub get_metaclass_by_name { $METAS{$_[0]} } sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] } sub weaken_metaclass { weaken($METAS{$_[0]}) } + sub metaclass_is_weak { isweak($METAS{$_[0]}) } sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} } sub remove_metaclass_by_name { delete $METAS{$_[0]}; return } @@ -1068,6 +1069,11 @@ store a weakened reference in the metaclass cache. This function will weaken the reference to the metaclass stored in C<$name>. +=item B + +Returns true if the metaclass for C<$name> has been weakened +(via C). + =item B This will return true of there exists a metaclass stored in the diff --git a/t/048_anon_class_create_init.t b/t/048_anon_class_create_init.t index a1cbb23..e24319c 100644 --- a/t/048_anon_class_create_init.t +++ b/t/048_anon_class_create_init.t @@ -89,10 +89,23 @@ my $instance; { my $meta_name; { - $meta_name = Class::MOP::Class->create_anon_class( + my $meta = Class::MOP::Class->create_anon_class( + superclasses => ['Class::MOP::Class'], + ); + $meta_name = $meta->name; + ok(Class::MOP::metaclass_is_weak($meta_name), + "default is for anon metaclasses to be weakened"); + } + ok(!Class::MOP::class_of($meta_name), + "and weak metaclasses go away when all refs do"); + { + my $meta = Class::MOP::Class->create_anon_class( superclasses => ['Class::MOP::Class'], weaken => 0, - )->name; + ); + $meta_name = $meta->name; + ok(!Class::MOP::metaclass_is_weak($meta_name), + "anon classes can be told not to weaken"); } ok(Class::MOP::class_of($meta_name), "metaclass still exists"); {