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