inhale Mouse
[gitmo/Moo.git] / lib / Moo / HandleMoose.pm
1 package Moo::HandleMoose;
2
3 use strictures 1;
4 use Moo::_Utils;
5 use B qw(perlstring);
6
7 our %TYPE_MAP;
8
9 our $SETUP_DONE;
10
11 sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; }
12
13 sub inject_all {
14   require Class::MOP;
15   inject_fake_metaclass_for($_)
16     for grep $_ ne 'Moo::Object', do { no warnings 'once'; keys %Moo::MAKERS };
17   inject_fake_metaclass_for($_) for keys %Moo::Role::INFO;
18   require Moose::Meta::Method::Constructor;
19   @Moo::HandleMoose::FakeConstructor::ISA = 'Moose::Meta::Method::Constructor';
20 }
21
22 sub maybe_reinject_fake_metaclass_for {
23   my ($name) = @_;
24   our %DID_INJECT;
25   if (delete $DID_INJECT{$name}) {
26     inject_fake_metaclass_for($name);
27   }
28 }
29
30 sub inject_fake_metaclass_for {
31   my ($name) = @_;
32   require Class::MOP;
33   Class::MOP::store_metaclass_by_name(
34     $name, bless({ name => $name }, 'Moo::HandleMoose::FakeMetaClass')
35   );
36 }
37
38 {
39   package Moo::HandleMoose::FakeConstructor;
40
41   sub _uninlined_body { \&Moose::Object::new }
42 }
43     
44
45 sub inject_real_metaclass_for {
46   my ($name) = @_;
47   our %DID_INJECT;
48   return Class::MOP::get_metaclass_by_name($name) if $DID_INJECT{$name};
49   require Moose; require Moo; require Moo::Role;
50   Class::MOP::remove_metaclass_by_name($name);
51   my ($am_role, $meta, $attr_specs, $attr_order) = do {
52     if (my $info = $Moo::Role::INFO{$name}) {
53       my @attr_info = @{$info->{attributes}||[]};
54       (1, Moose::Meta::Role->initialize($name),
55        { @attr_info },
56        [ @attr_info[grep !($_ % 2), 0..$#attr_info] ]
57       )
58     } else {
59       my $specs = Moo->_constructor_maker_for($name)->all_attribute_specs;
60       (0, Moose::Meta::Class->initialize($name), $specs,
61        [ sort { $specs->{$a}{index} <=> $specs->{$b}{index} } keys %$specs ]
62       );
63     }
64   };
65     
66   my %methods = %{Role::Tiny->_concrete_methods_of($name)};
67   # needed to ensure the method body is stable and get things named
68   Sub::Defer::undefer_sub($_) for grep defined, values %methods;
69   my @attrs;
70   {
71     # This local is completely not required for roles but harmless
72     local @{_getstash($name)}{keys %methods};
73     my %seen_name;
74     foreach my $name (@$attr_order) {
75       $seen_name{$name} = 1;
76       my %spec = %{$attr_specs->{$name}};
77       delete $spec{index};
78       $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
79       delete $spec{asserter};
80       if (my $isa = $spec{isa}) {
81         my $tc = $spec{isa} = do {
82           if (my $mapped = $TYPE_MAP{$isa}) {
83             $mapped->();
84           } else {
85             Moose::Meta::TypeConstraint->new(
86               constraint => sub { eval { &$isa; 1 } }
87             );
88           }
89         };
90         if (my $coerce = $spec{coerce}) {
91           $tc->coercion(Moose::Meta::TypeCoercion->new)
92              ->_compiled_type_coercion($coerce);
93           $spec{coerce} = 1;
94         }
95       } elsif (my $coerce = $spec{coerce}) {
96         my $attr = perlstring($name);
97         my $tc = Moose::Meta::TypeConstraint->new(
98                    constraint => sub { die "This is not going to work" },
99                    inlined => sub {
100                       'my $r = $_[42]{'.$attr.'}; $_[42]{'.$attr.'} = 1; $r'
101                    },
102                  );
103         $tc->coercion(Moose::Meta::TypeCoercion->new)
104            ->_compiled_type_coercion($coerce);
105         $spec{isa} = $tc;
106         $spec{coerce} = 1;
107       }
108       push @attrs, $meta->add_attribute($name => %spec);
109     }
110     foreach my $mouse (do { our %MOUSE; @{$MOUSE{$name}||[]} }) {
111       foreach my $attr ($mouse->get_all_attributes) {
112         my %spec = %{$attr};
113         delete @spec{qw(
114           associated_class associated_methods __METACLASS__
115           provides curries
116         )};
117         my $name = delete $spec{name};
118         next if $seen_name{$name}++;
119         push @attrs, $meta->add_attribute($name => %spec);
120       }
121     }
122   }
123   if ($am_role) {
124     my $info = $Moo::Role::INFO{$name};
125     $meta->add_required_methods(@{$info->{requires}});
126     foreach my $modifier (@{$info->{modifiers}}) {
127       my ($type, @args) = @$modifier;
128       $meta->${\"add_${type}_method_modifier"}(@args);
129     }
130   } else {
131     foreach my $attr (@attrs) {
132       foreach my $method (@{$attr->associated_methods}) {
133         $method->{body} = $name->can($method->name);
134       }
135     }
136     bless(
137       $meta->find_method_by_name('new'),
138       'Moo::HandleMoose::FakeConstructor',
139     );
140   }
141   $meta->add_role(Class::MOP::class_of($_))
142     for grep !/\|/ && $_ ne $name, # reject Foo|Bar and same-role-as-self
143       do { no warnings 'once'; keys %{$Role::Tiny::APPLIED_TO{$name}} };
144   $DID_INJECT{$name} = 1;
145   $meta;
146 }
147
148 {
149   package Moo::HandleMoose::FakeMetaClass;
150
151   sub DESTROY { }
152
153   sub AUTOLOAD {
154     my ($meth) = (our $AUTOLOAD =~ /([^:]+)$/);
155     Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->$meth(@_)
156   }
157   sub can {
158     Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->can(@_)
159   }
160   sub isa {
161     Moo::HandleMoose::inject_real_metaclass_for((shift)->{name})->isa(@_)
162   }
163 }
164
165 1;