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