return q{} unless $self->_value_needs_copy;
- my $code = "my \@potential = ${$potential_ref};";
+ my $code = "my \$potential = ${$potential_ref};";
- ${$potential_ref} = '@potential';
+ ${$potential_ref} = '$potential';
return $code;
}
return $self->_inline_check_member_constraint($new_value);
}
else {
- return $self->_inline_check_coercion( '\\' . $potential_value ) . "\n"
- . $self->_inline_check_constraint( '\\' . $potential_value );
+ return $self->_inline_check_coercion($potential_value) . "\n"
+ . $self->_inline_check_constraint($potential_value);
}
}
return ''
unless $attr->should_coerce && $attr->type_constraint->has_coercion;
- # We want to break the aliasing in @_ in case the coercion tries to make a
- # destructive change to an array member.
- my $code = 'my @copy = @{ $value }';
- return '@_ = @{ $attr->type_constraint->coerce(\@copy) };';
+ return "$value = \$type_constraint_obj->coerce($value);";
}
sub _inline_check_constraint {
return $self->SUPER::_inline_check_constraint( $_[0] );
}
-sub _capture_old_value { return q{} }
+sub _inline_get_old_value_for_trigger {
+ my ( $self, $instance ) = @_;
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
+ my $attr = $self->associated_attribute;
+ return '' unless $attr->has_trigger;
+
+ my $mi = $attr->associated_class->get_meta_instance;
+ my $pred = $mi->inline_is_slot_initialized( $instance, $attr->name );
- return $self->SUPER::_inline_store(
- $inv,
- $self->_value_needs_copy ? '\\' . $new : '[' . $new . ']'
- );
+ return
+ 'my @old = '
+ . $pred . q{ ? } . '[ @{'
+ . $self->_inline_get($instance)
+ . '} ] : ()' . ";\n";
}
sub _return_value { return q{} }
$code .= "\n"
. $self->_inline_tc_code(
$new_values,
- $potential_value
+ $potential_value,
+ $slot_access,
);
$code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
- $code .= "\n" . $self->_capture_old_value($slot_access);
+ $code .= "\n" . $self->_inline_capture_return_value($slot_access);
$code
.= "\n" . $self->_inline_store( $inv, '[' . $potential_value . ']' );
sub _potential_value { return '()' }
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "$slot_access = [];";
+}
+
1;
my ( $self, $slot_access ) = @_;
return
- "( do { my \@potential = \@{ $slot_access }; splice \@potential, \$_[0], 1; \@potential } )";
+ "( do { my \@potential = \@{ $slot_access }; splice \@potential, \$_[0], 1; \\\@potential } )";
+}
+
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "splice \@{ $slot_access }, \$_[0], 1;";
}
1;
my ( $self, $slot_access ) = @_;
return
- "( do { my \@potential = \@{ $slot_access }; splice \@potential, \$_[0], 0, \$_[1]; \@potential } )";
+ "( do { my \@potential = \@{ $slot_access }; splice \@potential, \$_[0], 0, \$_[1]; \\\@potential } )";
}
sub _new_values { '$_[1]' }
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "splice \@{ $slot_access }, \$_[0], 0, \$_[1];";
+}
+
1;
sub _potential_value {
my ( $self, $slot_access ) = @_;
- return "( \@{ $slot_access } > 1 ? \@{ $slot_access }[ 0 .. \$#{ $slot_access } - 1 ] : () )";
+ return "[ \@{ $slot_access } > 1 ? \@{ $slot_access }[ 0 .. \$#{ $slot_access } - 1 ] : () ]";
}
-sub _capture_old_value {
+sub _inline_capture_return_value {
my ( $self, $slot_access ) = @_;
- if ( $self->associated_attribute->has_trigger ) {
- return 'my $old = $old[-1];';
- }
- else {
- return "my \$old = $slot_access;";
- }
+ return "my \$old = ${slot_access}->[-1];";
+}
+
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "pop \@{ $slot_access };";
}
sub _return_value {
- my ( $self, $instance, $old_value ) = @_;
+ my ( $self, $slot_access ) = @_;
- return 'return @{$old} ? $old->[-1] : undef;';
+ return 'return $old;';
}
1;
sub _potential_value {
my ( $self, $slot_access ) = @_;
- return "( \@{ $slot_access }, \@_ )";
+ return "[ \@{ $slot_access }, \@_ ]";
+}
+
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "push \@{ $slot_access }, \@_;";
}
1;
my ( $self, $slot_access ) = @_;
return
- "( do { my \@potential = \@{ $slot_access }; \$potential[ \$_[0] ] = \$_[1]; \@potential } )";
+ "( do { my \@potential = \@{ $slot_access }; \$potential[ \$_[0] ] = \$_[1]; \\\@potential } )";
}
sub _new_values { '$_[1]' }
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "${slot_access}->[ \$_[0] ] = \$_[1];";
+}
+
1;
sub _potential_value {
my ( $self, $slot_access ) = @_;
- return "( \@{ $slot_access } > 1 ? \@{ $slot_access }[ 1 .. \$#{ $slot_access } ] : () )";
+ return "[ \@{ $slot_access } > 1 ? \@{ $slot_access }[ 1 .. \$#{ $slot_access } ] : () ]";
}
-sub _capture_old_value {
+sub _inline_capture_return_value {
my ( $self, $slot_access ) = @_;
- if ( $self->associated_attribute->has_trigger ) {
- return 'my $old = $old[-1];';
- }
- else {
- return "my \$old = $slot_access;";
- }
+ return "my \$old = ${slot_access}->[0];";
+}
+
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "shift \@{ $slot_access };";
}
sub _return_value {
- my ( $self, $instance, $old_value ) = @_;
+ my ( $self, $slot_access ) = @_;
- return 'return $old->[0]';
+ return 'return $old';
}
1;
my ( $self, $slot_access ) = @_;
return
- "( \$_[0] ? sort { \$_[0]->( \$a, \$b ) } \@{ $slot_access } : sort \@{ $slot_access} )";
+ "[ \$_[0] ? sort { \$_[0]->( \$a, \$b ) } \@{ $slot_access } : sort \@{ $slot_access} ]";
}
1;
my ( $self, $slot_access ) = @_;
return "( do { my \@potential = \@{ $slot_access };"
- . 'defined $len ? ( splice @potential, $idx, $len, @_ ) : ( splice @potential, $idx ); @potential } )';
+ . 'defined $len ? ( splice @potential, $idx, $len, @_ ) : ( splice @potential, $idx ); \\@potential } )';
+}
+
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "defined \$len ? ( splice \@{ $slot_access }, \$idx, \$len, \@_ ) : ( splice \@{ $slot_access }, \$idx );";
}
1;
sub _potential_value {
my ( $self, $slot_access ) = @_;
- return "( \@_, \@{ $slot_access } )";
+ return "[ \@_, \@{ $slot_access } ]";
+}
+
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "unshift \@{ $slot_access }, \@_;";
}
1;
return "( $slot_access . \$_[0] )";
}
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "$slot_access .= \$_[0];";
+}
+
1;
return "( do { my \$val = $slot_access; chomp \$val; \$val } )";
}
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
- return $self->SUPER::_inline_set_new_value(@_)
- if $self->_value_needs_copy;
-
- my $slot_access = $self->_inline_get($inv);
-
- return "chomp ${slot_access}";
+ return "chomp $slot_access;";
}
1;
return "( do { my \$val = $slot_access; chop \$val; \$val } )";
}
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
- return $self->SUPER::_inline_set_new_value(@_)
- if $self->_value_needs_copy;
-
- my $slot_access = $self->_inline_get($inv);
-
- return "chop ${slot_access}";
+ return "chop $slot_access;";
}
1;
return "q{}";
}
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
- return $self->SUPER::_inline_set_new_value(@_)
- if $self->_value_needs_copy;
-
- my $slot_access = $self->_inline_get($inv);
-
- return "${slot_access} = q{}";
+ return "$slot_access = q{};";
}
1;
return "( do { my \$val = $slot_access; \$val++ } )";
}
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
- return $self->SUPER::_inline_set_new_value(@_)
- if $self->_value_needs_copy;
-
- my $slot_access = $self->_inline_get($inv);
-
- return "${slot_access}++";
+ return "${slot_access}++;";
}
1;
return "( \$_[0] . $slot_access )";
}
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
+
+ return "$slot_access = \$_[0] . $slot_access;";
+}
+
1;
return "( do { my \$val = $slot_access; ref \$_[1] ? \$val =~ s/\$_[0]/\$_[1]->()/e : \$val =~ s/\$_[0]/\$_[1]/; \$val } )";
}
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
-
- return $self->SUPER::_inline_set_new_value(@_)
- if $self->_value_needs_copy;
-
- my $slot_access = $self->_inline_get($inv);
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
return "if ( ref \$_[1] ) { $slot_access =~ s/\$_[0]/\$_[1]->()/e; } else { $slot_access =~ s/\$_[0]/\$_[1]/; }";
}
"( do { my \$potential = $slot_access; substr \$potential, \$offset, \$length, \$replacement; \$potential; } )";
}
-sub _inline_set_new_value {
- my ( $self, $inv, $new ) = @_;
-
- return $self->SUPER::_inline_set_new_value(@_)
- if $self->_value_needs_copy;
-
- my $slot_access = $self->_inline_get($inv);
+sub _inline_optimized_set_new_value {
+ my ( $self, $inv, $new, $slot_access ) = @_;
return "substr $slot_access, \$offset, \$length, \$replacement;";
}
);
$code .= "\n" . $self->_inline_get_old_value_for_trigger($inv);
- $code .= "\n" . $self->_capture_old_value($slot_access);
+ $code .= "\n" . $self->_inline_capture_return_value($slot_access);
$code .= "\n"
. $self->_inline_set_new_value(
$inv,
- $potential_value
+ $potential_value,
+ $slot_access,
);
$code .= "\n" . $self->_inline_trigger( $inv, $slot_access, '@old' );
- $code .= "\n" . $self->_return_value( $inv, '@old', 'for writer' );
+ $code .= "\n" . $self->_return_value( $slot_access, 'for writer' );
return $code;
}
die '_constraint_must_be_checked must be overridden by ' . ref $_[0];
}
-sub _capture_old_value { return q{} }
+sub _inline_capture_return_value { return q{} }
sub _inline_set_new_value {
my $self = shift;
- return $self->SUPER::_inline_store(@_);
+ return $self->SUPER::_inline_store(@_)
+ if $self->_value_needs_copy;
+
+ return $self->_inline_optimized_set_new_value(@_);
+}
+
+sub _inline_optimized_set_new_value {
+ my $self = shift;
+
+ return $self->SUPER::_inline_store(@_)
}
sub _return_value { return q{} }
lives_ok { $obj->unshift( 101, 22 ) }
'unshifted two values and lived';
+ is_deeply(
+ $obj->_values, [ 101, 22, 10, 12, 42, 1, 2, 3 ],
+ 'unshift changed the value of the array in the object'
+ );
+
lives_ok { $obj->unshift() }
'call to unshift without arguments lives';