Add a benchmark for new_object()
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
3
4 sub _inline_slot{
5     my(undef, $self_var, $attr_name) = @_;
6     return sprintf '%s->{q{%s}}', $self_var, $attr_name;
7 }
8
9 sub _generate_constructor {
10     my ($class, $metaclass, $args) = @_;
11
12     my $associated_metaclass_name = $metaclass->name;
13
14     my $buildall      = $class->_generate_BUILDALL($metaclass);
15     my $buildargs     = $class->_generate_BUILDARGS($metaclass);
16
17     my $source = sprintf(<<'EOT', __LINE__, __FILE__, $metaclass->name, $buildargs, $buildall);
18 #line %d %s
19         package %s;
20         sub {
21             my $class = shift;
22             return $class->Mouse::Object::new(@_)
23                 if $class ne __PACKAGE__;
24             # BUILDARGS
25             %s;
26             my $instance = bless {}, $class;
27             $metaclass->_initialize_object($instance, $args, 0);
28             # BUILDALL
29             %s;
30             return $instance;
31         }
32 EOT
33     #warn $source;
34     my $body;
35     my $e = do{
36         local $@;
37         $body = eval $source;
38         $@;
39     };
40     die $e if $e;
41     return $body;
42 }
43
44 sub _generate_initialize_object {
45     my ($method_class, $metaclass) = @_;
46     my @attrs  = $metaclass->get_all_attributes;
47
48     my @checks = map { $_ && $_->_compiled_type_constraint }
49                  map { $_->type_constraint } @attrs;
50
51     my @res;
52
53     my $has_triggers;
54     my $strict = $metaclass->strict_constructor;
55
56     if($strict){
57         push @res, 'my $used = 0;';
58     }
59
60     for my $index (0 .. @attrs - 1) {
61         my $code = '';
62
63         my $attr = $attrs[$index];
64         my $key  = $attr->name;
65
66         my $init_arg        = $attr->init_arg;
67         my $type_constraint = $attr->type_constraint;
68         my $is_weak_ref     = $attr->is_weak_ref;
69         my $need_coercion;
70
71         my $instance_slot  = $method_class->_inline_slot('$instance', $key);
72         my $attr_var       = "\$attrs[$index]";
73         my $constraint_var;
74
75         if(defined $type_constraint){
76              $constraint_var = "$attr_var\->{type_constraint}";
77              $need_coercion  = ($attr->should_coerce && $type_constraint->has_coercion);
78         }
79
80         $code .= "# initialize $key\n";
81
82         my $post_process = '';
83         if(defined $type_constraint){
84             $post_process .= "\$checks[$index]->($instance_slot)\n";
85             $post_process .= "  or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
86         }
87         if($is_weak_ref){
88             $post_process  = "Scalar::Util::weaken($instance_slot) "
89                              . "if ref $instance_slot;\n";
90         }
91
92         # build cde for an attribute
93         if (defined $init_arg) {
94             my $value = "\$args->{q{$init_arg}}";
95
96             $code .= "if (exists $value) {\n";
97
98             if($need_coercion){
99                 $value = "$constraint_var->coerce($value)";
100             }
101
102             $code .= "$instance_slot = $value;\n";
103             $code .= $post_process;
104
105             if ($attr->has_trigger) {
106                 $has_triggers++;
107                 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
108             }
109
110             if ($strict){
111                 $code .= '++$used;' . "\n";
112             }
113
114             $code .= "\n} else {\n"; # $value exists
115         }
116
117         if ($attr->has_default || $attr->has_builder) {
118             unless ($attr->is_lazy) {
119                 my $default = $attr->default;
120                 my $builder = $attr->builder;
121
122                 my $value;
123                 if (defined($builder)) {
124                     $value = "\$instance->$builder()";
125                 }
126                 elsif (ref($default) eq 'CODE') {
127                     $value = "$attr_var\->{default}->(\$instance)";
128                 }
129                 elsif (defined($default)) {
130                     $value = "$attr_var\->{default}";
131                 }
132                 else {
133                     $value = 'undef';
134                 }
135
136                 if($need_coercion){
137                     $value = "$constraint_var->coerce($value)";
138                 }
139
140                 $code .= "$instance_slot = $value;\n";
141                 $code .= $post_process;
142             }
143         }
144         elsif ($attr->is_required) {
145             $code .= "\$meta->throw_error('Attribute ($key) is required')";
146             $code .= "    unless \$is_cloning;\n";
147         }
148
149         $code .= "}\n" if defined $init_arg;
150
151         push @res, $code;
152     }
153
154     if($strict){
155         push @res, q{if($used < keys %{$args})}
156             . q{{ $meta->_report_unknown_args(\@attrs, $args) }};
157     }
158
159     if($metaclass->is_anon_class){
160         push @res, q{$instance->{__METACLASS__} = $meta;};
161     }
162
163     if($has_triggers){
164         unshift @res, q{my @triggers;};
165         push    @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
166     }
167
168     my $source = sprintf <<'EOT', __LINE__, __FILE__, $metaclass->name, join "\n", @res;
169 #line %d %s
170     package %s;
171     sub {
172         my($meta, $instance, $args, $is_cloning) = @_;
173         %s;
174         return $instance;
175     }
176 EOT
177     warn $source if $ENV{MOUSE_DEBUG};
178     my $body;
179     my $e = do {
180         local $@;
181         $body = eval $source;
182         $@;
183     };
184     die $e if $e;
185     return $body;
186 }
187
188 sub _generate_BUILDARGS {
189     my(undef, $metaclass) = @_;
190
191     my $class = $metaclass->name;
192     if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
193         return 'my $args = $class->BUILDARGS(@_)';
194     }
195
196     return <<'...';
197         my $args;
198         if ( scalar @_ == 1 ) {
199             ( ref( $_[0] ) eq 'HASH' )
200                 || Carp::confess "Single parameters to new() must be a HASH ref";
201             $args = +{ %{ $_[0] } };
202         }
203         else {
204             $args = +{@_};
205         }
206 ...
207 }
208
209 sub _generate_BUILDALL {
210     my (undef, $metaclass) = @_;
211
212     return '' unless $metaclass->name->can('BUILD');
213
214     my @code;
215     for my $class ($metaclass->linearized_isa) {
216         if (Mouse::Util::get_code_ref($class, 'BUILD')) {
217             unshift  @code, qq{${class}::BUILD(\$instance, \$args);};
218         }
219     }
220     return join "\n", @code;
221 }
222
223 1;
224 __END__
225
226 =head1 NAME
227
228 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
229
230 =head1 VERSION
231
232 This document describes Mouse version 0.71
233
234 =head1 SEE ALSO
235
236 L<Moose::Meta::Method::Constructor>
237
238 =cut