1 package Moo::HandleMoose;
11 sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; }
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';
22 sub maybe_reinject_fake_metaclass_for {
25 if (delete $DID_INJECT{$name}) {
26 inject_fake_metaclass_for($name);
30 sub inject_fake_metaclass_for {
33 require Moo::HandleMoose::FakeMetaClass;
34 Class::MOP::store_metaclass_by_name(
35 $name, bless({ name => $name }, 'Moo::HandleMoose::FakeMetaClass')
40 package Moo::HandleMoose::FakeConstructor;
42 sub _uninlined_body { \&Moose::Object::new }
46 sub inject_real_metaclass_for {
49 return Class::MOP::get_metaclass_by_name($name) if $DID_INJECT{$name};
50 require Moose; require Moo; require Moo::Role;
51 Class::MOP::remove_metaclass_by_name($name);
52 my ($am_role, $meta, $attr_specs, $attr_order) = do {
53 if (my $info = $Moo::Role::INFO{$name}) {
54 my @attr_info = @{$info->{attributes}||[]};
55 (1, Moose::Meta::Role->initialize($name),
57 [ @attr_info[grep !($_ % 2), 0..$#attr_info] ]
60 my $specs = Moo->_constructor_maker_for($name)->all_attribute_specs;
61 (0, Moose::Meta::Class->initialize($name), $specs,
62 [ sort { $specs->{$a}{index} <=> $specs->{$b}{index} } keys %$specs ]
67 my %methods = %{Role::Tiny->_concrete_methods_of($name)};
68 # needed to ensure the method body is stable and get things named
69 Sub::Defer::undefer_sub($_) for grep defined, values %methods;
72 # This local is completely not required for roles but harmless
73 local @{_getstash($name)}{keys %methods};
75 foreach my $name (@$attr_order) {
76 $seen_name{$name} = 1;
77 my %spec = %{$attr_specs->{$name}};
79 $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
80 delete $spec{asserter};
81 if (my $isa = $spec{isa}) {
82 my $tc = $spec{isa} = do {
83 if (my $mapped = $TYPE_MAP{$isa}) {
86 Moose::Meta::TypeConstraint->new(
87 constraint => sub { eval { &$isa; 1 } }
91 if (my $coerce = $spec{coerce}) {
92 $tc->coercion(Moose::Meta::TypeCoercion->new)
93 ->_compiled_type_coercion($coerce);
96 } elsif (my $coerce = $spec{coerce}) {
97 my $attr = perlstring($name);
98 my $tc = Moose::Meta::TypeConstraint->new(
99 constraint => sub { die "This is not going to work" },
101 'my $r = $_[42]{'.$attr.'}; $_[42]{'.$attr.'} = 1; $r'
104 $tc->coercion(Moose::Meta::TypeCoercion->new)
105 ->_compiled_type_coercion($coerce);
109 push @attrs, $meta->add_attribute($name => %spec);
111 foreach my $mouse (do { our %MOUSE; @{$MOUSE{$name}||[]} }) {
112 foreach my $attr ($mouse->get_all_attributes) {
115 associated_class associated_methods __METACLASS__
118 my $name = delete $spec{name};
119 next if $seen_name{$name}++;
120 push @attrs, $meta->add_attribute($name => %spec);
125 my $info = $Moo::Role::INFO{$name};
126 $meta->add_required_methods(@{$info->{requires}});
127 foreach my $modifier (@{$info->{modifiers}}) {
128 my ($type, @args) = @$modifier;
129 $meta->${\"add_${type}_method_modifier"}(@args);
132 foreach my $attr (@attrs) {
133 foreach my $method (@{$attr->associated_methods}) {
134 $method->{body} = $name->can($method->name);
138 $meta->find_method_by_name('new'),
139 'Moo::HandleMoose::FakeConstructor',
142 $meta->add_role(Class::MOP::class_of($_))
143 for grep !/\|/ && $_ ne $name, # reject Foo|Bar and same-role-as-self
144 do { no warnings 'once'; keys %{$Role::Tiny::APPLIED_TO{$name}} };
145 $DID_INJECT{$name} = 1;