scope makes slower!mouse's constructer is faster than moose with this commit :)
[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_inline {
6     my ($class, $meta) = @_;
7
8     my @attrs = $meta->compute_all_applicable_attributes;
9     my $buildall = $class->_generate_BUILDALL($meta);
10     my $buildargs = $class->_generate_BUILDARGS($meta);
11     my $processattrs = $class->_generate_processattrs($meta, \@attrs);
12     my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs;
13
14     my $code = <<"...";
15     sub {
16         my \$class = shift;
17         $buildargs;
18         my \$instance = bless {}, \$class;
19         $processattrs;
20         $buildall;
21         return \$instance;
22     }
23 ...
24
25     local $@;
26     # warn $code;
27     my $res = eval $code;
28     die $@ if $@;
29     $res;
30 }
31
32 sub _generate_processattrs {
33     my ($class, $meta, $attrs) = @_;
34     my @res;
35
36     for my $index (0 .. @$attrs - 1) {
37         my $attr = $attrs->[$index];
38         my $key  = $attr->name;
39         my $code = '';
40
41         if (defined $attr->init_arg) {
42             my $from = $attr->init_arg;
43
44             $code .= "if (exists \$args->{'$from'}) {\n";
45
46             if ($attr->should_coerce && $attr->type_constraint) {
47                 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
48             }
49             else {
50                 $code .= "my \$value = \$args->{'$from'};\n";
51             }
52
53             if ($attr->has_type_constraint) {
54                 if ($attr->type_constraint->{_compiled_type_constraint}) {
55                     $code .= "unless (\$compiled_constraints[$index](\$value)) {";
56                 } else {
57                     $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
58                 }
59                 $code .= "
60                         \$attrs[$index]->verify_type_constraint_error(
61                             '$key', \$_, \$attrs[$index]->type_constraint
62                         )
63                     }
64                 ";
65             }
66
67             $code .= "\$instance->{'$key'} = \$value;\n";
68
69             if ($attr->is_weak_ref) {
70                 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
71             }
72
73             if ($attr->has_trigger) {
74                 $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value );\n";
75             }
76
77             $code .= "\n} else {\n";
78         }
79
80         if ($attr->has_default || $attr->has_builder) {
81             unless ($attr->is_lazy) {
82                 my $default = $attr->default;
83                 my $builder = $attr->builder;
84
85                 $code .= "my \$value = ";
86
87                 if ($attr->should_coerce && $attr->type_constraint) {
88                     $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
89                 }
90
91                     if ($attr->has_builder) {
92                         $code .= "\$instance->$builder";
93                     }
94                     elsif (ref($default) eq 'CODE') {
95                         $code .= "\$attrs[$index]->{default}->(\$instance)";
96                     }
97                     elsif (!defined($default)) {
98                         $code .= 'undef';
99                     }
100                     elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
101                         $code .= $default;
102                     }
103                     else {
104                         $code .= "'$default'";
105                     }
106
107                 if ($attr->should_coerce) {
108                     $code .= ");\n";
109                 }
110                 else {
111                     $code .= ";\n";
112                 }
113
114                 if ($attr->has_type_constraint) {
115                     $code .= "{
116                         unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
117                             \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
118                         }
119                     }";
120                 }
121
122                 $code .= "\$instance->{'$key'} = \$value;\n";
123
124                 if ($attr->is_weak_ref) {
125                     $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
126                 }
127             }
128         }
129         elsif ($attr->is_required) {
130             $code .= "Carp::confess('Attribute ($key) is required');";
131         }
132
133         $code .= "}\n" if defined $attr->init_arg;
134
135         push @res, $code;
136     }
137
138     return join "\n", @res;
139 }
140
141 sub _generate_BUILDARGS {
142     my $self = shift;
143     my $meta = shift;
144
145     if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
146         return 'my $args = $class->BUILDARGS(@_)';
147     }
148
149     return <<'...';
150         my $args;
151         if ( scalar @_ == 1 ) {
152             ( ref( $_[0] ) eq 'HASH' )
153                 || Carp::confess "Single parameters to new() must be a HASH ref";
154             $args = +{ %{ $_[0] } };
155         }
156         else {
157             $args = +{@_};
158         }
159 ...
160 }
161
162 sub _generate_BUILDALL {
163     my ($class, $meta) = @_;
164     return '' unless $meta->name->can('BUILD');
165
166     my @code = ();
167     push @code, q{no strict 'refs';};
168     push @code, q{no warnings 'once';};
169     no strict 'refs';
170     no warnings 'once';
171     for my $klass ($meta->linearized_isa) {
172         if (*{ $klass . '::BUILD' }{CODE}) {
173             push  @code, qq{${klass}::BUILD(\$instance, \$args);};
174         }
175     }
176     return join "\n", @code;
177 }
178
179 1;