All native array methods are being inlined.
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array / Writer.pm
CommitLineData
f7fd22b6 1package Moose::Meta::Method::Accessor::Native::Array::Writer;
2
3use strict;
4use warnings;
5
6our $VERSION = '1.13';
7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
10use base 'Moose::Meta::Method::Accessor::Native::Array';
11
a7821be5 12sub _generate_method {
13 my $self = shift;
14
15 my $inv = '$self';
16
17 my $slot_access = $self->_inline_get($inv);
18
19 my $code = 'sub {';
20 $code .= "\n" . $self->_inline_pre_body(@_);
21
22 $code .= "\n" . 'my $self = shift;';
23
24 $code .= "\n" . $self->_inline_check_lazy($inv);
25
26 $code .= "\n" . $self->_inline_curried_arguments;
27
28 $code .= "\n" . $self->_inline_check_argument_count;
29
30 $code .= "\n" . $self->_inline_process_arguments;
31
32 $code .= "\n" . $self->_inline_check_arguments;
33
34 my $new_values = $self->_new_values($slot_access);
35 my $potential_value = $self->_potential_value($slot_access);
36
37 $code .= "\n"
38 . $self->_inline_tc_code(
39 $new_values,
40 $potential_value
41 );
42
43 $code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
44 $code .= "\n" . $self->_capture_old_value($slot_access);
45
46 $code .= "\n" . $self->_inline_store( $inv, '[' . $potential_value . ']' );
47
48 $code .= "\n" . $self->_inline_post_body(@_);
49 $code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' );
50
51 $code .= "\n" . $self->_return_value( $inv, '@old' );
52
53 $code .= "\n}";
54
55 return $code;
56}
57
58sub _inline_process_arguments { q{} }
59
60sub _inline_check_arguments { q{} }
61
62sub _new_values { '@_' }
63
64sub _inline_tc_code {
65 my ( $self, $new_value, $potential_value ) = @_;
66
67 return q{} unless $self->_constraint_must_be_checked;
68
69 if ( $self->_check_new_members_only ) {
70 return q{} unless $self->_adds_members;
71
72 return $self->_inline_check_member_constraint($new_value);
73 }
74 else {
75 return $self->_inline_check_coercion($potential_value) . "\n"
76 . $self->_inline_check_constraint($potential_value);
77 }
78}
79
80sub _constraint_must_be_checked {
81 my $self = shift;
82
83 my $attr = $self->associated_attribute;
84
85 return $attr->has_type_constraint
86 && ( $attr->type_constraint->name ne 'ArrayRef'
87 || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) );
88}
89
90sub _check_new_members_only {
91 my $self = shift;
92
93 my $attr = $self->associated_attribute;
94
95 my $tc = $attr->type_constraint;
96
97 # If we have a coercion, we could come up with an entirely new value after
98 # coercing, so we need to check everything,
99 return 0 if $attr->should_coerce && $tc->has_coercion;
100
101 # If the parent is ArrayRef, that means we can just check the new members
102 # of the collection, because we know that we will always be generating an
103 # ArrayRef.
104 return 1 if $tc->parent->name eq 'ArrayRef';
105
106 # If our parent is something else ( subtype 'Foo' as 'ArrayRef[Str]' )
107 # then there may be additional constraints on the whole value, as opposed
108 # to constraints just on the members.
109 return 0;
110}
111
112sub _inline_check_member_constraint {
113 my ( $self, $new_value ) = @_;
114
115 my $attr_name = $self->associated_attribute->name;
116
117 return '$member_tc->($_) || '
118 . $self->_inline_throw_error(
119 qq{"A new member value for '$attr_name' does not pass its type constraint because: "}
120 . ' . $member_tc->get_message($_)',
121 "data => \$_"
122 ) . " for $new_value;";
123}
124
125sub _inline_check_coercion {
126 my ( $self, $value ) = @_;
127
128 my $attr = $self->associated_attribute;
129
130 return ''
131 unless $attr->should_coerce && $attr->type_constraint->has_coercion;
132
133 # We want to break the aliasing in @_ in case the coercion tries to make a
134 # destructive change to an array member.
135 my $code = 'my @copy = @{ $value }';
136 return '@_ = @{ $attr->type_constraint->coerce(\@copy) };';
137}
138
139sub _inline_check_constraint {
140 my $self = shift;
141
142 return q{} unless $self->_constraint_must_be_checked;
143
144 return $self->SUPER::_inline_check_constraint(@_);
145}
146
147sub _capture_old_value { return q{} }
148sub _return_value { return q{} }
149
150sub _eval_environment {
151 my $self = shift;
152
153 my $env = $self->SUPER::_eval_environment;
154
155 return $env
156 unless $self->_constraint_must_be_checked
157 and $self->_check_new_members_only;
158
159 $env->{'$member_tc'}
160 = \( $self->associated_attribute->type_constraint->type_parameter
161 ->_compiled_type_constraint );
162
163 return $env;
164}
165
f7fd22b6 1661;