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