1 package Moose::Meta::Method::Accessor::Native;
6 use Carp qw( confess );
7 use Scalar::Util qw( blessed weaken );
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
20 exists $options{curried_arguments}
21 || ( $options{curried_arguments} = [] );
23 ( $options{curried_arguments}
24 && ( 'ARRAY' eq ref $options{curried_arguments} ) )
26 'You must supply a curried_arguments which is an ARRAY reference';
28 $options{definition_context} = $options{attribute}->definition_context;
30 $options{accessor_type} = 'native';
32 return $class->$orig(%options);
37 my $options = @_ == 1 ? $_[0] : {@_};
39 return bless $options, $class;
42 sub root_types { (shift)->{'root_types'} }
44 sub _initialize_body {
47 $self->{'body'} = $self->_compile_code( [$self->_generate_method] );
52 sub _inline_curried_arguments {
55 return unless @{ $self->curried_arguments };
57 return ('unshift @_, @curried;');
60 sub _inline_check_argument_count {
65 if (my $min = $self->_minimum_arguments) {
67 'if (@_ < ' . $min . ') {',
68 $self->_inline_throw_error(
70 '"Cannot call %s without at least %s argument%s"',
71 $self->delegate_to_method,
73 ($min == 1 ? '' : 's'),
80 if (defined(my $max = $self->_maximum_arguments)) {
82 'if (@_ > ' . $max . ') {',
83 $self->_inline_throw_error(
85 '"Cannot call %s with %s argument%s"',
86 $self->delegate_to_method,
87 $max ? "more than $max" : 'any',
88 ($max == 1 ? '' : 's'),
98 sub _inline_return_value {
100 my ($slot_access, $for_writer) = @_;
103 'return ' . $self->_return_value($slot_access, $for_writer) . ';',
107 sub _minimum_arguments { 0 }
108 sub _maximum_arguments { undef }
110 override _inline_get => sub {
114 return $self->_slot_access_can_be_inlined
116 : $instance . '->$reader';
119 override _inline_store => sub {
121 my ($instance, $value) = @_;
123 return $self->_slot_access_can_be_inlined
125 : $instance . '->$writer(' . $value . ')';
128 override _eval_environment => sub {
133 $env->{'@curried'} = $self->curried_arguments;
135 return $env if $self->_slot_access_can_be_inlined;
137 my $reader = $self->associated_attribute->get_read_method_ref;
138 $reader = $reader->body if blessed $reader;
140 $env->{'$reader'} = \$reader;
142 my $writer = $self->associated_attribute->get_write_method_ref;
143 $writer = $writer->body if blessed $writer;
145 $env->{'$writer'} = \$writer;
150 sub _slot_access_can_be_inlined {
153 return $self->is_inline && $self->_instance_is_inlinable;