Fix issues on 5.6.2
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
1 package Mouse::Meta::Method::Constructor;
2 use strict;
3 use warnings;
4
5 sub _generate_constructor_method {
6     my ($class, $metaclass, $args) = @_;
7
8     my $associated_metaclass_name = $metaclass->name;
9     my @attrs         = $metaclass->get_all_attributes;
10
11     my $buildall      = $class->_generate_BUILDALL($metaclass);
12     my $buildargs     = $class->_generate_BUILDARGS($metaclass);
13     my $processattrs  = $class->_generate_processattrs($metaclass, \@attrs);
14
15     my @compiled_constraints = map { $_ ? $_->_compiled_type_constraint : undef }
16                                map { $_->type_constraint } @attrs;
17
18
19
20     my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
21         sub \{
22             my \$class = shift;
23             return \$class->Mouse::Object::new(\@_)
24                 if \$class ne q{$associated_metaclass_name};
25             $buildargs;
26             my \$instance = bless {}, \$class;
27             $processattrs;
28             $buildall;
29             return \$instance;
30         }
31 ...
32
33     my $code;
34     my $e = do{
35         local $@;
36         $code = eval $source;
37         $@;
38     };
39     die $e if $e;
40
41     $metaclass->add_method($args->{constructor_name} => $code);
42     return;
43 }
44
45 sub _generate_processattrs {
46     my ($class, $metaclass, $attrs) = @_;
47     my @res;
48
49     my $has_triggers;
50
51     for my $index (0 .. @$attrs - 1) {
52         my $attr = $attrs->[$index];
53         my $key  = $attr->name;
54         my $code = '';
55
56         if (defined $attr->init_arg) {
57             my $from = $attr->init_arg;
58
59             $code .= "if (exists \$args->{q{$from}}) {\n";
60
61             my $value = "\$args->{q{$from}}";
62             if(my $type_constraint = $attr->type_constraint){
63                 if($attr->should_coerce && $type_constraint->has_coercion){
64                     $code .= "my \$value = \$attrs[$index]->{type_constraint}->coerce(\$args->{q{$from}});\n";
65                     $value = '$value';
66                 }
67
68                 $code .= "\$compiled_constraints[$index]->($value)\n";
69                 $code .= "  or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
70             }
71
72             $code .= "\$instance->{q{$key}} = $value;\n";
73
74             if ($attr->is_weak_ref) {
75                 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
76             }
77
78             if ($attr->has_trigger) {
79                 $has_triggers++;
80                 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
81             }
82
83             $code .= "\n} else {\n";
84         }
85
86         if ($attr->has_default || $attr->has_builder) {
87             unless ($attr->is_lazy) {
88                 my $default = $attr->default;
89                 my $builder = $attr->builder;
90
91                 $code .= "my \$value = ";
92
93                 if ($attr->should_coerce && $attr->type_constraint) {
94                     $code .= "\$attrs[$index]->_coerce_and_verify(";
95                 }
96
97                 if ($attr->has_builder) {
98                     $code .= "\$instance->$builder()";
99                 }
100                 elsif (ref($default) eq 'CODE') {
101                     $code .= "\$attrs[$index]->{default}->(\$instance)";
102                 }
103                 elsif (!defined($default)) {
104                     $code .= 'undef';
105                 }
106                 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
107                     $code .= $default;
108                 }
109                 else {
110                     $code .= "'$default'";
111                 }
112
113                 if ($attr->should_coerce) {
114                     $code .= ");\n";
115                 }
116                 else {
117                     $code .= ";\n";
118                 }
119
120                 if ($attr->has_type_constraint) {
121                     $code .= "{
122                         unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
123                             \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
124                         }
125                     }";
126                 }
127
128                 $code .= "\$instance->{q{$key}} = \$value;\n";
129
130                 if ($attr->is_weak_ref) {
131                     $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
132                 }
133             }
134         }
135         elsif ($attr->is_required) {
136             $code .= "Carp::confess('Attribute ($key) is required');";
137         }
138
139         $code .= "}\n" if defined $attr->init_arg;
140
141         push @res, $code;
142     }
143
144     if($metaclass->is_anon_class){
145         push @res, q{$instnace->{__METACLASS__} = $metaclass;};
146     }
147
148     if($has_triggers){
149         unshift @res, q{my @triggers;};
150         push    @res,  q{$_->[0]->($instance, $_->[1]) for @triggers;};
151     }
152
153     return join "\n", @res;
154 }
155
156 sub _generate_BUILDARGS {
157     my($self, $metaclass) = @_;
158
159     if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
160         return 'my $args = $class->BUILDARGS(@_)';
161     }
162
163     return <<'...';
164         my $args;
165         if ( scalar @_ == 1 ) {
166             ( ref( $_[0] ) eq 'HASH' )
167                 || Carp::confess "Single parameters to new() must be a HASH ref";
168             $args = +{ %{ $_[0] } };
169         }
170         else {
171             $args = +{@_};
172         }
173 ...
174 }
175
176 sub _generate_BUILDALL {
177     my ($class, $metaclass) = @_;
178
179     return '' unless $metaclass->name->can('BUILD');
180
181     my @code;
182     for my $class ($metaclass->linearized_isa) {
183         no strict 'refs';
184         no warnings 'once';
185
186         if (*{ $class . '::BUILD' }{CODE}) {
187             unshift  @code, qq{${class}::BUILD(\$instance, \$args);};
188         }
189     }
190     return join "\n", @code;
191 }
192
193 1;