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