Add some tests
[gitmo/Mouse.git] / lib / Mouse / Util.pm
index 9d5346c..c3f1f39 100644 (file)
@@ -1,51 +1,96 @@
 package Mouse::Util;
 use Mouse::Exporter; # enables strict and warnings
 
-use Carp qw(confess);
-use Scalar::Util qw(blessed);
-use B ();
+sub get_linear_isa($;$); # must be here
 
-use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE};
+BEGIN{
+    # This is used in Mouse::PurePerl
+    Mouse::Exporter->setup_import_methods(
+        as_is => [qw(
+            find_meta
+            does_role
+            resolve_metaclass_alias
+            apply_all_roles
+            english_list
+
+            load_class
+            is_class_loaded
+
+            get_linear_isa
+            get_code_info
+
+            get_code_package
+            get_code_ref
+
+            not_supported
+
+            does meta dump
+        )],
+        groups => {
+            default => [], # export no functions by default
+
+            # The ':meta' group is 'use metaclass' for Mouse
+            meta    => [qw(does meta dump)],
+        },
+    );
+
+
+    # Because Mouse::Util is loaded first in all the Mouse sub-modules,
+    # XS loader is placed here, not in Mouse.pm.
+
+    our $VERSION = '0.40_08';
+
+    my $xs = !(exists $INC{'Mouse/PurePerl.pm'} || $ENV{MOUSE_PUREPERL});
 
-Mouse::Exporter->setup_import_methods(
-    as_is => [qw(
-        find_meta
-        does_role
-        resolve_metaclass_alias
-        apply_all_roles
-        english_list
+    if($xs){
+        local $@;
+
+        # XXX: XSLoader tries to get the object path from caller's file name
+        #      $hack_mouse_file fools its mechanism
+
+        (my $hack_mouse_file = __FILE__) =~ s/.Util//; # .../Mouse/Util.pm -> .../Mouse.pm
+        $xs = eval sprintf("#line %d %s\n", __LINE__, $hack_mouse_file) . q{
+            require XSLoader;
+            XSLoader::load('Mouse', $VERSION);
+
+            *Mouse::Meta::Method::Constructor::XS::meta = \&meta;
+            *Mouse::Meta::Method::Destructor::XS::meta  = \&meta;
+        };
+        #warn $@ if $@;
+    }
 
-        load_class
-        is_class_loaded
+    if(!$xs){
+        require 'Mouse/PurePerl.pm'; # we don't want to create its namespace
+    }
 
-        get_linear_isa
-        get_code_info
+    *MOUSE_XS = sub(){ $xs };
+}
 
-        get_code_package
 
-        not_supported
+use Carp         ();
+use Scalar::Util ();
 
-        does meta dump
-        _MOUSE_VERBOSE
-    )],
-    groups => {
-        default => [], # export no functions by default
-        meta    => [qw(does meta dump _MOUSE_VERBOSE)],
-    },
-    _export_to_main => 1,
-);
+use constant _MOUSE_VERBOSE => !!$ENV{MOUSE_VERBOSE};
 
 # aliases as public APIs
 # it must be 'require', not 'use', because Mouse::Meta::Module depends on Mouse::Util
 require Mouse::Meta::Module; # for the entities of metaclass cache utilities
 
 BEGIN {
-    *class_of                    = \&Mouse::Meta::Module::class_of;
-    *get_metaclass_by_name       = \&Mouse::Meta::Module::get_metaclass_by_name;
-    *get_all_metaclass_instances = \&Mouse::Meta::Module::get_all_metaclass_instances;
-    *get_all_metaclass_names     = \&Mouse::Meta::Module::get_all_metaclass_names;
+    *class_of                    = \&Mouse::Meta::Module::_class_of;
+    *get_metaclass_by_name       = \&Mouse::Meta::Module::_get_metaclass_by_name;
+    *get_all_metaclass_instances = \&Mouse::Meta::Module::_get_all_metaclass_instances;
+    *get_all_metaclass_names     = \&Mouse::Meta::Module::_get_all_metaclass_names;
+
+    # is-a predicates
+    generate_isa_predicate_for('Mouse::Meta::TypeConstraint' => 'is_a_type_constraint');
+    generate_isa_predicate_for('Mouse::Meta::Class'          => 'is_a_metaclass');
+    generate_isa_predicate_for('Mouse::Meta::Role'           => 'is_a_metarole');
 }
 
+our $in_global_destruction = 0;
+END{ $in_global_destruction = 1 }
+
 # Moose::Util compatible utilities
 
 sub find_meta{
@@ -64,10 +109,10 @@ sub does_role{
 }
 
 BEGIN {
-    my $impl;
+    my $get_linear_isa;
     if ($] >= 5.009_005) {
         require mro;
-        $impl = \&mro::get_linear_isa;
+        $get_linear_isa = \&mro::get_linear_isa;
     } else {
         my $e = do {
             local $@;
@@ -75,11 +120,11 @@ BEGIN {
             $@;
         };
         if (!$e) {
-            $impl = \&mro::get_linear_isa;
+            $get_linear_isa = \&mro::get_linear_isa;
         } else {
 #       VVVVV   CODE TAKEN FROM MRO::COMPAT   VVVVV
             my $_get_linear_isa_dfs; # this recurses so it isn't pretty
-            $_get_linear_isa_dfs = sub {
+            $_get_linear_isa_dfs = sub ($;$){
                 no strict 'refs';
 
                 my $classname = shift;
@@ -97,41 +142,13 @@ BEGIN {
                 return \@lin;
             };
 #       ^^^^^   CODE TAKEN FROM MRO::COMPAT   ^^^^^
-            $impl = $_get_linear_isa_dfs;
+            $get_linear_isa = $_get_linear_isa_dfs;
         }
     }
 
-
-    no warnings 'once';
-    *get_linear_isa = $impl;
+    *get_linear_isa = $get_linear_isa;
 }
 
-{ # taken from Sub::Identify
-    sub get_code_info($) {
-        my ($coderef) = @_;
-        ref($coderef) or return;
-
-        my $cv = B::svref_2object($coderef);
-        $cv->isa('B::CV') or return;
-
-        my $gv = $cv->GV;
-        $gv->isa('B::GV') or return;
-
-        return ($gv->STASH->NAME, $gv->NAME);
-    }
-
-    sub get_code_package{
-        my($coderef) = @_;
-
-        my $cv = B::svref_2object($coderef);
-        $cv->isa('B::CV') or return '';
-
-        my $gv = $cv->GV;
-        $gv->isa('B::GV') or return '';
-
-        return $gv->STASH->NAME;
-    }
-}
 
 # taken from Mouse::Util (0.90)
 {
@@ -162,6 +179,8 @@ BEGIN {
 
 # Utilities from Class::MOP
 
+sub get_code_info;
+sub get_code_package;
 
 # taken from Class/MOP.pm
 sub is_valid_class_name {
@@ -170,7 +189,7 @@ sub is_valid_class_name {
     return 0 if ref($class);
     return 0 unless defined($class);
 
-    return 1 if $class =~ /^\w+(?:::\w+)*$/;
+    return 1 if $class =~ /\A \w+ (?: :: \w+ )* \z/xms;
 
     return 0;
 }
@@ -193,7 +212,7 @@ sub load_first_existing_class {
     }
 
     # not found
-    confess join(
+    Carp::confess join(
         "\n",
         map {
             sprintf( "Could not load class (%s) because : %s",
@@ -209,7 +228,7 @@ sub _try_load_one_class {
 
     unless ( is_valid_class_name($class) ) {
         my $display = defined($class) ? $class : 'undef';
-        confess "Invalid class name ($display)";
+        Carp::confess "Invalid class name ($display)";
     }
 
     return undef if $is_class_loaded_cache{$class} ||= is_class_loaded($class);
@@ -228,46 +247,18 @@ sub _try_load_one_class {
 sub load_class {
     my $class = shift;
     my $e = _try_load_one_class($class);
-    confess "Could not load class ($class) because : $e" if $e;
+    Carp::confess "Could not load class ($class) because : $e" if $e;
 
     return 1;
 }
 
-
-sub is_class_loaded {
-    my $class = shift;
-
-    return 0 if ref($class) || !defined($class) || !length($class);
-
-    # walk the symbol table tree to avoid autovififying
-    # \*{${main::}{"Foo::"}} == \*main::Foo::
-
-    my $pack = \%::;
-    foreach my $part (split('::', $class)) {
-        my $entry = \$pack->{$part . '::'};
-        return 0 if ref($entry) ne 'GLOB';
-        $pack = *{$entry}{HASH} or return 0;
-    }
-
-    # check for $VERSION or @ISA
-    return 1 if exists $pack->{VERSION}
-             && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
-    return 1 if exists $pack->{ISA}
-             && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
-
-    # check for any method
-    foreach my $name( keys %{$pack} ) {
-        my $entry = \$pack->{$name};
-        return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
-    }
-
-    # fail
-    return 0;
-}
+sub is_class_loaded;
 
 
 sub apply_all_roles {
-    my $applicant = blessed($_[0]) ? shift : Mouse::Meta::Class->initialize(shift);
+    my $applicant = Scalar::Util::blessed($_[0])
+        ?                                shift   # instance
+        : Mouse::Meta::Class->initialize(shift); # class or role name
 
     my @roles;
 
@@ -281,9 +272,9 @@ sub apply_all_roles {
         }
         my $role_name = $roles[-1][0];
         load_class($role_name);
-        my $metarole = get_metaclass_by_name($role_name);
-        ( $metarole && $metarole->isa('Mouse::Meta::Role') )
-            || $applicant->meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role");
+
+        is_a_metarole( get_metaclass_by_name($role_name) )
+            || $applicant->meta->throw_error("You can only consume roles, $role_name is not a Mouse role");
     }
 
     if ( scalar @roles == 1 ) {
@@ -321,11 +312,13 @@ sub not_supported{
     Carp::confess("Mouse does not currently support $feature");
 }
 
-sub meta{
+# general meta() method
+sub meta :method{
     return Mouse::Meta::Class->initialize(ref($_[0]) || $_[0]);
 }
 
-sub dump { 
+# general dump() method
+sub dump :method {
     my($self, $maxdepth) = @_;
 
     require 'Data/Dumper.pm'; # we don't want to create its namespace
@@ -335,6 +328,7 @@ sub dump {
     return $dd->Dump();
 }
 
+# general does() method
 sub does :method;
 *does = \&does_role; # alias
 
@@ -346,6 +340,10 @@ __END__
 
 Mouse::Util - Features, with or without their dependencies
 
+=head1 VERSION
+
+This document describes Mouse version 0.40_08
+
 =head1 IMPLEMENTATIONS FOR
 
 =head2 Moose::Util