make native trait inlining work
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array / splice.pm
1 package Moose::Meta::Method::Accessor::Native::Array::splice;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '1.19';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 use Moose::Role;
11
12 with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => {
13     -excludes => [
14         qw(
15             _minimum_arguments
16             _inline_process_arguments
17             _inline_check_arguments
18             _optimized_set_new_value
19             _return_value
20             )
21     ]
22 };
23
24 sub _minimum_arguments { 1 }
25
26 sub _adds_members { 1 }
27
28 sub _inline_process_arguments {
29     return (
30         'my $idx = shift;',
31         'my $len = @_ ? shift : undef;',
32     );
33 }
34
35 sub _inline_check_arguments {
36     my $self = shift;
37
38     return (
39         $self->_inline_check_var_is_valid_index('$idx'),
40         'if (defined($len) && $len !~ /^-?\d+$/) {',
41             $self->_inline_throw_error(
42                 '"The length argument passed to splice must be an integer"',
43             ) . ';',
44         '}',
45     );
46 }
47
48 sub _potential_value {
49     my $self = shift;
50     my ($slot_access) = @_;
51
52     return '(do { '
53              . 'my @potential = @{ (' . $slot_access . ') }; '
54              . '@return = defined $len '
55                  . '? (splice @potential, $idx, $len, @_) '
56                  . ': (splice @potential, $idx); '
57                  . '\@potential;'
58          . '})';
59 }
60
61 sub _optimized_set_new_value {
62     my $self = shift;
63     my ($inv, $new, $slot_access) = @_;
64
65     return '@return = defined $len '
66              . '? (splice @{ (' . $slot_access . ') }, $idx, $len, @_) '
67              . ': (splice @{ (' . $slot_access . ') }, $idx)';
68 }
69
70 sub _return_value {
71     my $self = shift;
72     my ($slot_access) = @_;
73
74     return 'wantarray ? @return : $return[-1]';
75 }
76
77 no Moose::Role;
78
79 1;