1 package Method::Generate::Accessor;
5 use base qw(Moo::Object);
12 _maybe_load_module('Class::XSAccessor')
14 (eval { Class::XSAccessor->VERSION('1.07') })
19 my ($self, $into, $name, $spec, $quote_opts) = @_;
20 die "Must have an is" unless my $is = $spec->{is};
22 $spec->{reader} = $name unless exists $spec->{reader};
23 } elsif ($is eq 'rw') {
24 $spec->{accessor} = $name unless exists $spec->{accessor};
25 } elsif ($is eq 'lazy') {
26 $spec->{init_arg} = undef unless exists $spec->{init_arg};
27 $spec->{reader} = $name unless exists $spec->{reader};
29 $spec->{builder} ||= '_build_'.$name unless $spec->{default};
30 } elsif ($is ne 'bare') {
31 die "Unknown is ${is}";
34 if (my $reader = $spec->{reader}) {
35 if (our $CAN_HAZ_XS && $self->is_simple_get($name, $spec)) {
36 $methods{$reader} = $self->_generate_xs(
37 getters => $into, $reader, $name
40 $self->{captures} = {};
42 quote_sub "${into}::${reader}"
43 => ' die "'.$reader.' is a read-only accessor" if @_ > 1;'."\n"
44 .$self->_generate_get($name, $spec)
45 => delete $self->{captures}
49 if (my $accessor = $spec->{accessor}) {
52 && $self->is_simple_get($name, $spec)
53 && $self->is_simple_set($name, $spec)
55 $methods{$accessor} = $self->_generate_xs(
56 accessors => $into, $accessor, $name
59 $self->{captures} = {};
61 quote_sub "${into}::${accessor}"
62 => $self->_generate_getset($name, $spec)
63 => delete $self->{captures}
67 if (my $writer = $spec->{writer}) {
70 && $self->is_simple_set($name, $spec)
72 $methods{$writer} = $self->_generate_xs(
73 setters => $into, $writer, $name
76 $self->{captures} = {};
78 quote_sub "${into}::${writer}"
79 => $self->_generate_set($name, $spec)
80 => delete $self->{captures}
84 if (my $pred = $spec->{predicate}) {
86 quote_sub "${into}::${pred}" =>
87 ' '.$self->_generate_simple_has('$_[0]', $name)."\n"
90 if (my $cl = $spec->{clearer}) {
92 quote_sub "${into}::${cl}" =>
93 " delete \$_[0]->{${\perlstring $name}}\n"
96 if (my $hspec = $spec->{handles}) {
97 my $asserter = $spec->{asserter} ||= '_assert_'.$name;
99 if (ref($hspec) eq 'ARRAY') {
100 map [ $_ => $_ ], @$hspec;
101 } elsif (ref($hspec) eq 'HASH') {
102 map [ $_ => ref($hspec->{$_}) ? @{$hspec->{$_}} : $hspec->{$_} ],
104 } elsif (!ref($hspec)) {
105 map [ $_ => $_ ], Role::Tiny->methods_provided_by($hspec);
107 die "You gave me a handles of ${hspec} and I have no idea why";
110 foreach my $spec (@specs) {
111 my ($proxy, $target, @args) = @$spec;
112 $self->{captures} = {};
114 quote_sub "${into}::${proxy}" =>
115 $self->_generate_delegation($asserter, $target, \@args),
116 delete $self->{captures}
120 if (my $asserter = $spec->{asserter}) {
121 $self->{captures} = {};
122 $methods{$asserter} =
123 quote_sub "${into}::${asserter}" =>
124 'do { '.$self->_generate_get($name, $spec).qq! }||die "Attempted to access '${name}' but it is not set"!,
125 delete $self->{captures}
131 sub is_simple_attribute {
132 my ($self, $name, $spec) = @_;
133 # clearer doesn't have to be listed because it doesn't
134 # affect whether defined/exists makes a difference
136 qw(lazy default builder coerce isa trigger predicate weak_ref);
140 my ($self, $name, $spec) = @_;
141 !($spec->{lazy} and ($spec->{default} or $spec->{builder}));
145 my ($self, $name, $spec) = @_;
146 !grep $spec->{$_}, qw(coerce isa trigger weak_ref);
149 sub has_eager_default {
150 my ($self, $name, $spec) = @_;
151 (!$spec->{lazy} and ($spec->{default} or $spec->{builder}));
155 my ($self, $name, $spec) = @_;
156 my $simple = $self->_generate_simple_get('$_[0]', $name);
157 if ($self->is_simple_get($name, $spec)) {
160 'do { '.$self->_generate_use_default(
161 '$_[0]', $name, $spec,
162 $self->_generate_simple_has('$_[0]', $name),
167 sub _generate_simple_has {
168 my ($self, $me, $name) = @_;
169 "exists ${me}->{${\perlstring $name}}";
172 sub generate_get_default {
174 $self->{captures} = {};
175 my $code = $self->_generate_get_default(@_);
176 ($code, delete $self->{captures});
179 sub _generate_use_default {
180 my ($self, $me, $name, $spec, $test) = @_;
181 my $get_value = $self->_generate_get_default($me, $name, $spec);
182 if ($spec->{coerce}) {
183 $get_value = $self->_generate_coerce(
184 $name, $me, $get_value,
188 $self->_generate_simple_set(
189 $me, $name, $spec, $get_value
193 sub _generate_get_default {
194 my ($self, $me, $name, $spec) = @_;
196 ? $self->_generate_call_code($name, 'default', $me, $spec->{default})
197 : "${me}->${\$spec->{builder}}"
200 sub generate_simple_get {
201 my ($self, @args) = @_;
202 $self->_generate_simple_get(@args);
205 sub _generate_simple_get {
206 my ($self, $me, $name) = @_;
207 my $name_str = perlstring $name;
208 "${me}->{${name_str}}";
212 my ($self, $name, $spec) = @_;
213 if ($self->is_simple_set($name, $spec)) {
214 $self->_generate_simple_set('$_[0]', $name, $spec, '$_[1]');
216 my ($coerce, $trigger, $isa_check) = @{$spec}{qw(coerce trigger isa)};
217 my $simple = $self->_generate_simple_set('$self', $name, $spec, '$value');
218 my $code = "do { my (\$self, \$value) = \@_;\n";
222 .$self->_generate_coerce($name, '$self', '$value', $coerce).";\n";
226 " ".$self->_generate_isa_check($name, '$value', $isa_check).";\n";
229 my $fire = $self->_generate_trigger($name, '$self', '$value', $trigger);
231 " ".$simple.";\n ".$fire.";\n"
234 $code .= " ".$simple.";\n";
241 sub generate_coerce {
243 $self->{captures} = {};
244 my $code = $self->_generate_coerce(@_);
245 ($code, delete $self->{captures});
248 sub _generate_coerce {
249 my ($self, $name, $obj, $value, $coerce) = @_;
250 $self->_generate_call_code($name, 'coerce', "${value}", $coerce);
253 sub generate_trigger {
255 $self->{captures} = {};
256 my $code = $self->_generate_trigger(@_);
257 ($code, delete $self->{captures});
260 sub _generate_trigger {
261 my ($self, $name, $obj, $value, $trigger) = @_;
262 $self->_generate_call_code($name, 'trigger', "${obj}, ${value}", $trigger);
265 sub generate_isa_check {
266 my ($self, @args) = @_;
267 $self->{captures} = {};
268 my $code = $self->_generate_isa_check(@args);
269 ($code, delete $self->{captures});
272 sub _generate_isa_check {
273 my ($self, $name, $value, $check) = @_;
274 $self->_generate_call_code($name, 'isa_check', $value, $check);
277 sub _generate_call_code {
278 my ($self, $name, $type, $values, $sub) = @_;
279 if (my $quoted = quoted_from_sub($sub)) {
280 my $code = $quoted->[1];
281 my $at_ = '@_ = ('.$values.');';
282 if (my $captures = $quoted->[2]) {
283 my $cap_name = qq{\$${type}_captures_for_${name}};
284 $self->{captures}->{$cap_name} = \$captures;
285 Sub::Quote::inlinify(
286 $code, $values, Sub::Quote::capture_unroll($cap_name, $captures, 6)
289 Sub::Quote::inlinify($code, $values);
292 my $cap_name = qq{\$${type}_for_${name}};
293 $self->{captures}->{$cap_name} = \$sub;
294 "${cap_name}->(${values})";
298 sub generate_populate_set {
300 $self->{captures} = {};
301 my $code = $self->_generate_populate_set(@_);
302 ($code, delete $self->{captures});
305 sub _generate_populate_set {
306 my ($self, $me, $name, $spec, $source, $test) = @_;
307 if ($self->has_eager_default($name, $spec)) {
308 my $get_indent = ' ' x ($spec->{isa} ? 6 : 4);
309 my $get_default = $self->_generate_get_default(
313 defined($spec->{init_arg})
314 ? "(\n${get_indent} ${test}\n${get_indent} ? ${source}\n${get_indent} : "
318 if ( $spec->{coerce} ) {
319 $get_value = $self->_generate_coerce(
320 $name, $me, $get_value,
325 ? " {\n my \$value = ".$get_value.";\n "
326 .$self->_generate_isa_check(
327 $name, '$value', $spec->{isa}
329 .' '.$self->_generate_simple_set($me, $name, $spec, '$value').";\n"
331 : ' '.$self->_generate_simple_set($me, $name, $spec, $get_value).";\n"
335 .$self->_generate_trigger(
336 $name, $me, $self->_generate_simple_get($me, $name),
345 .$self->_generate_coerce(
353 .$self->_generate_isa_check(
354 $name, $source, $spec->{isa}
358 ." ".$self->_generate_simple_set($me, $name, $spec, $source).";\n"
361 .$self->_generate_trigger(
362 $name, $me, $self->_generate_simple_get($me, $name),
371 sub generate_multi_set {
372 my ($self, $me, $to_set, $from) = @_;
373 "\@{${me}}{qw(${\join ' ', @$to_set})} = $from";
376 sub _generate_simple_set {
377 my ($self, $me, $name, $spec, $value) = @_;
378 my $name_str = perlstring $name;
379 my $simple = "${me}->{${name_str}} = ${value}";
381 if ($spec->{weak_ref}) {
382 require Scalar::Util;
384 # Perl < 5.8.3 can't weaken refs to readonly vars
385 # (e.g. string constants). This *can* be solved by:
387 #Internals::SetReadWrite($foo);
388 #Scalar::Util::weaken ($foo);
389 #Internals::SetReadOnly($foo);
391 # but requires XS and is just too damn crazy
392 # so simply throw a better exception
393 Moo::_Utils::lt_5_8_3() ? <<"EOC" : "Scalar::Util::weaken(${simple})";
395 eval { Scalar::Util::weaken($simple); 1 } or do {
396 if( \$@ =~ /Modification of a read-only value attempted/) {
398 Carp::croak( sprintf (
399 'Reference to readonly value in "%s" can not be weakened on Perl < 5.8.3',
412 sub _generate_getset {
413 my ($self, $name, $spec) = @_;
414 q{(@_ > 1}."\n ? ".$self->_generate_set($name, $spec)
415 ."\n : ".$self->_generate_get($name, $spec)."\n )";
418 sub _generate_delegation {
419 my ($self, $asserter, $target, $args) = @_;
420 my $arg_string = do {
422 # I could, I reckon, linearise out non-refs here using perlstring
423 # plus something to check for numbers but I'm unsure if it's worth it
424 $self->{captures}{'@curries'} = $args;
430 "shift->${asserter}->${target}(${arg_string});";
434 my ($self, $type, $into, $name, $slot) = @_;
435 Class::XSAccessor->import(
437 $type => { $name => $slot }