1 package Moose::Meta::Method::Accessor::Native;
6 use Carp qw( confess );
7 use Scalar::Util qw( blessed weaken );
16 $options{curried_arguments} = []
17 unless exists $options{curried_arguments};
19 confess 'You must supply a curried_arguments which is an ARRAY reference'
20 unless $options{curried_arguments}
21 && ref($options{curried_arguments}) eq 'ARRAY';
23 my $attr_context = $options{attribute}->definition_context;
24 my $desc = 'native delegation method ';
25 $desc .= $options{attribute}->associated_class->name;
26 $desc .= '::' . $options{name};
27 $desc .= " ($options{delegate_to_method})";
28 $desc .= " of attribute " . $options{attribute}->name;
29 $options{definition_context} = {
30 %{ $attr_context || {} },
34 $options{accessor_type} = 'native';
36 return $class->$orig(%options);
41 my $options = @_ == 1 ? $_[0] : {@_};
43 return bless $options, $class;
46 sub root_types { (shift)->{'root_types'} }
48 sub _initialize_body {
51 $self->{'body'} = $self->_compile_code( [$self->_generate_method] );
56 sub _inline_curried_arguments {
59 return unless @{ $self->curried_arguments };
61 return 'unshift @_, @curried;';
64 sub _inline_check_argument_count {
69 if (my $min = $self->_minimum_arguments) {
71 'if (@_ < ' . $min . ') {',
72 $self->_inline_throw_error(
74 '"Cannot call %s without at least %s argument%s"',
75 $self->delegate_to_method,
77 ($min == 1 ? '' : 's'),
84 if (defined(my $max = $self->_maximum_arguments)) {
86 'if (@_ > ' . $max . ') {',
87 $self->_inline_throw_error(
89 '"Cannot call %s with %s argument%s"',
90 $self->delegate_to_method,
91 $max ? "more than $max" : 'any',
92 ($max == 1 ? '' : 's'),
102 sub _inline_return_value {
104 my ($slot_access, $for_writer) = @_;
106 return 'return ' . $self->_return_value($slot_access, $for_writer) . ';';
109 sub _minimum_arguments { 0 }
110 sub _maximum_arguments { undef }
112 override _get_value => sub {
116 return $self->_slot_access_can_be_inlined
118 : $instance . '->$reader';
121 override _inline_store_value => sub {
123 my ($instance, $value) = @_;
125 return $self->_slot_access_can_be_inlined
127 : $instance . '->$writer(' . $value . ');';
130 override _eval_environment => sub {
135 $env->{'@curried'} = $self->curried_arguments;
137 return $env if $self->_slot_access_can_be_inlined;
139 my $reader = $self->associated_attribute->get_read_method_ref;
140 $reader = $reader->body if blessed $reader;
142 $env->{'$reader'} = \$reader;
144 my $writer = $self->associated_attribute->get_write_method_ref;
145 $writer = $writer->body if blessed $writer;
147 $env->{'$writer'} = \$writer;
152 sub _slot_access_can_be_inlined {
155 return $self->is_inline && $self->_instance_is_inlinable;