generate DESTROY method for performance improvement
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
CommitLineData
fc1d8369 1package Mouse::Meta::Method::Constructor;
2use strict;
3use warnings;
4
5sub generate_constructor_method_inline {
41cdacce 6 my ($class, $meta) = @_;
24ad3f66 7
c12edd9a 8 my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code
fc1d8369 9 my $buildall = $class->_generate_BUILDALL($meta);
10 my $buildargs = $class->_generate_BUILDARGS();
c12edd9a 11 my $processattrs = $class->_generate_processattrs($meta, \@attrs);
fc1d8369 12
24ad3f66 13 my $code = <<"...";
fc1d8369 14 sub {
15 my \$class = shift;
16 my \$args = $buildargs;
24ad3f66 17 my \$instance = bless {}, \$class;
fc1d8369 18 $processattrs;
19 $buildall;
20 return \$instance;
21 }
22...
24ad3f66 23
24 warn $code if $ENV{DEBUG};
25
26 local $@;
27 my $res = eval $code;
28 die $@ if $@;
29 $res;
fc1d8369 30}
31
32sub _generate_processattrs {
c12edd9a 33 my ($class, $meta, $attrs) = @_;
fc1d8369 34 my @res;
c12edd9a 35 for my $index (0..scalar(@$attrs)-1) {
36 my $attr = $attrs->[$index];
fc1d8369 37 my $from = $attr->init_arg;
38 my $key = $attr->name;
c91862e8 39
fc1d8369 40 my $part1 = do {
41 my @code;
c91862e8 42
43 push @code, "my \$value = \$args->{'$from'};";
44
fc1d8369 45 if ($attr->should_coerce) {
c91862e8 46 push @code, "\$value = \$attr->coerce_constraint( \$value );";
fc1d8369 47 }
c91862e8 48
fc1d8369 49 if ($attr->has_type_constraint) {
c91862e8 50 push @code, "\$attr->verify_type_constraint( \$value );";
fc1d8369 51 }
c91862e8 52
53 push @code, "\$instance->{'$key'} = \$value;";
54
41cdacce 55 if ($attr->is_weak_ref) {
c91862e8 56 push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );";
41cdacce 57 }
c91862e8 58
fc1d8369 59 if ( $attr->has_trigger ) {
c91862e8 60 push @code, "\$attr->trigger->( \$instance, \$value, \$attr );";
fc1d8369 61 }
c91862e8 62
fc1d8369 63 join "\n", @code;
64 };
c91862e8 65
fc1d8369 66 my $part2 = do {
67 my @code;
68 if ( $attr->has_default || $attr->has_builder ) {
69 unless ( $attr->is_lazy ) {
70 my $default = $attr->default;
71 my $builder = $attr->builder;
72 if ($attr->has_builder) {
73 push @code, "my \$value = \$instance->$builder;";
74 } elsif (ref($default) eq 'CODE') {
75 push @code, "my \$value = \$attr->default()->();";
76 } else {
77 push @code, "my \$value = \$attr->default();";
78 }
79 if ($attr->should_coerce) {
80 push @code, "\$value = \$attr->coerce_constraint(\$value);";
81 }
82 if ($attr->has_type_constraint) {
83 push @code, "\$attr->verify_type_constraint(\$value);";
84 }
8dcbf65d 85 push @code, "\$instance->{'$key'} = \$value;";
fc1d8369 86 if ($attr->is_weak_ref) {
8dcbf65d 87 push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
fc1d8369 88 }
89 }
90 join "\n", @code;
91 }
92 else {
93 if ( $attr->is_required ) {
95e0838c 94 qq{Carp::confess("Attribute ($key) is required");};
fc1d8369 95 } else {
96 ""
97 }
98 }
99 };
100 my $code = <<"...";
101 {
c12edd9a 102 my \$attr = \$attrs[$index];
95e0838c 103 if (exists(\$args->{'$from'})) {
fc1d8369 104 $part1;
105 } else {
106 $part2;
107 }
108 }
109...
110 push @res, $code;
111 }
112 return join "\n", @res;
113}
114
115sub _generate_BUILDARGS {
116 <<'...';
117 do {
118 if ( scalar @_ == 1 ) {
119 if ( defined $_[0] ) {
120 ( ref( $_[0] ) eq 'HASH' )
121 || Carp::confess "Single parameters to new() must be a HASH ref";
122 +{ %{ $_[0] } };
123 }
124 else {
125 +{};
126 }
127 }
128 else {
129 +{@_};
130 }
131 };
132...
133}
134
135sub _generate_BUILDALL {
136 my ($class, $meta) = @_;
137 return '' unless $meta->name->can('BUILD');
138
139 my @code = ();
140 push @code, q{no strict 'refs';};
141 push @code, q{no warnings 'once';};
142 no strict 'refs';
143 for my $class ($meta->linearized_isa) {
144 if (*{ $class . '::BUILD' }{CODE}) {
145 push @code, qq{${class}::BUILD->(\$instance, \$args);};
146 }
147 }
148 return join "\n", @code;
149}
150
1511;