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 $options{definition_context} = $options{attribute}->definition_context;
25 $options{accessor_type} = 'native';
27 return $class->$orig(%options);
32 my $options = @_ == 1 ? $_[0] : {@_};
34 return bless $options, $class;
37 sub root_types { (shift)->{'root_types'} }
39 sub _initialize_body {
42 $self->{'body'} = $self->_compile_code( [$self->_generate_method] );
47 sub _inline_curried_arguments {
50 return unless @{ $self->curried_arguments };
52 return 'unshift @_, @curried;';
55 sub _inline_check_argument_count {
60 if (my $min = $self->_minimum_arguments) {
62 'if (@_ < ' . $min . ') {',
63 $self->_inline_throw_error(
65 '"Cannot call %s without at least %s argument%s"',
66 $self->delegate_to_method,
68 ($min == 1 ? '' : 's'),
75 if (defined(my $max = $self->_maximum_arguments)) {
77 'if (@_ > ' . $max . ') {',
78 $self->_inline_throw_error(
80 '"Cannot call %s with %s argument%s"',
81 $self->delegate_to_method,
82 $max ? "more than $max" : 'any',
83 ($max == 1 ? '' : 's'),
93 sub _inline_return_value {
95 my ($slot_access, $for_writer) = @_;
97 return 'return ' . $self->_return_value($slot_access, $for_writer) . ';';
100 sub _minimum_arguments { 0 }
101 sub _maximum_arguments { undef }
103 override _get_value => sub {
107 return $self->_slot_access_can_be_inlined
109 : $instance . '->$reader';
112 override _inline_store_value => sub {
114 my ($instance, $value) = @_;
116 return $self->_slot_access_can_be_inlined
118 : $instance . '->$writer(' . $value . ');';
121 override _eval_environment => sub {
126 $env->{'@curried'} = $self->curried_arguments;
128 return $env if $self->_slot_access_can_be_inlined;
130 my $reader = $self->associated_attribute->get_read_method_ref;
131 $reader = $reader->body if blessed $reader;
133 $env->{'$reader'} = \$reader;
135 my $writer = $self->associated_attribute->get_write_method_ref;
136 $writer = $writer->body if blessed $writer;
138 $env->{'$writer'} = \$writer;
143 sub _slot_access_can_be_inlined {
146 return $self->is_inline && $self->_instance_is_inlinable;