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