1 package Method::Generate::Accessor;
5 use base qw(Moo::Object);
8 use Scalar::Util 'blessed';
14 _maybe_load_module('Class::XSAccessor')
16 (eval { Class::XSAccessor->VERSION('1.07') })
21 my ($self, $into, $name, $spec, $quote_opts) = @_;
23 die "Must have an is" unless my $is = $spec->{is};
25 $spec->{reader} = $name unless exists $spec->{reader};
26 } elsif ($is eq 'rw') {
27 $spec->{accessor} = $name unless exists $spec->{accessor};
28 } elsif ($is eq 'lazy') {
29 $spec->{reader} = $name unless exists $spec->{reader};
31 $spec->{builder} ||= '_build_'.$name unless $spec->{default};
32 } elsif ($is eq 'rwp') {
33 $spec->{reader} = $name unless exists $spec->{reader};
34 $spec->{writer} = "_set_${name}" unless exists $spec->{writer};
35 } elsif ($is ne 'bare') {
36 die "Unknown is ${is}";
38 $spec->{builder} = '_build_'.$name if ($spec->{builder}||0) eq 1;
39 die "Invalid builder for $into->$name - not a valid method name"
40 if exists $spec->{builder} and (ref $spec->{builder}
41 or $spec->{builder} !~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/);
42 if (($spec->{predicate}||0) eq 1) {
43 $spec->{predicate} = $name =~ /^_/ ? "_has${name}" : "has_${name}";
45 if (($spec->{clearer}||0) eq 1) {
46 $spec->{clearer} = $name =~ /^_/ ? "_clear${name}" : "clear_${name}";
48 if (($spec->{trigger}||0) eq 1) {
49 $spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
52 for my $setting (qw( default coerce )) {
53 next if !exists $spec->{$setting};
54 my $value = $spec->{$setting};
55 my $invalid = "Invalid $setting '" . overload::StrVal($value)
56 . "' for $into->$name - not a coderef";
57 die "$invalid or code-convertible object"
58 unless ref $value and (ref $value eq 'CODE' or blessed($value));
59 die "$invalid and could not be converted to a coderef: $@"
60 if !eval { \&$value };
64 if (my $reader = $spec->{reader}) {
65 if (our $CAN_HAZ_XS && $self->is_simple_get($name, $spec)) {
66 $methods{$reader} = $self->_generate_xs(
67 getters => $into, $reader, $name, $spec
70 $self->{captures} = {};
72 quote_sub "${into}::${reader}"
73 => ' die "'.$reader.' is a read-only accessor" if @_ > 1;'."\n"
74 .$self->_generate_get($name, $spec)
75 => delete $self->{captures}
79 if (my $accessor = $spec->{accessor}) {
82 && $self->is_simple_get($name, $spec)
83 && $self->is_simple_set($name, $spec)
85 $methods{$accessor} = $self->_generate_xs(
86 accessors => $into, $accessor, $name, $spec
89 $self->{captures} = {};
91 quote_sub "${into}::${accessor}"
92 => $self->_generate_getset($name, $spec)
93 => delete $self->{captures}
97 if (my $writer = $spec->{writer}) {
100 && $self->is_simple_set($name, $spec)
102 $methods{$writer} = $self->_generate_xs(
103 setters => $into, $writer, $name, $spec
106 $self->{captures} = {};
108 quote_sub "${into}::${writer}"
109 => $self->_generate_set($name, $spec)
110 => delete $self->{captures}
114 if (my $pred = $spec->{predicate}) {
116 quote_sub "${into}::${pred}" =>
117 ' '.$self->_generate_simple_has('$_[0]', $name, $spec)."\n"
120 if (my $cl = $spec->{clearer}) {
122 quote_sub "${into}::${cl}" =>
123 $self->_generate_simple_clear('$_[0]', $name, $spec)."\n"
126 if (my $hspec = $spec->{handles}) {
127 my $asserter = $spec->{asserter} ||= '_assert_'.$name;
129 if (ref($hspec) eq 'ARRAY') {
130 map [ $_ => $_ ], @$hspec;
131 } elsif (ref($hspec) eq 'HASH') {
132 map [ $_ => ref($hspec->{$_}) ? @{$hspec->{$_}} : $hspec->{$_} ],
134 } elsif (!ref($hspec)) {
135 map [ $_ => $_ ], Role::Tiny->methods_provided_by($hspec);
137 die "You gave me a handles of ${hspec} and I have no idea why";
140 foreach my $spec (@specs) {
141 my ($proxy, $target, @args) = @$spec;
142 $self->{captures} = {};
144 quote_sub "${into}::${proxy}" =>
145 $self->_generate_delegation($asserter, $target, \@args),
146 delete $self->{captures}
150 if (my $asserter = $spec->{asserter}) {
151 $self->{captures} = {};
152 $methods{$asserter} =
153 quote_sub "${into}::${asserter}" =>
154 'do { '.$self->_generate_get($name, $spec).qq! }||die "Attempted to access '${name}' but it is not set"!,
155 delete $self->{captures}
161 sub is_simple_attribute {
162 my ($self, $name, $spec) = @_;
163 # clearer doesn't have to be listed because it doesn't
164 # affect whether defined/exists makes a difference
166 qw(lazy default builder coerce isa trigger predicate weak_ref);
170 my ($self, $name, $spec) = @_;
171 !($spec->{lazy} and ($spec->{default} or $spec->{builder}));
175 my ($self, $name, $spec) = @_;
176 !grep $spec->{$_}, qw(coerce isa trigger weak_ref);
179 sub has_eager_default {
180 my ($self, $name, $spec) = @_;
181 (!$spec->{lazy} and ($spec->{default} or $spec->{builder}));
185 my ($self, $name, $spec) = @_;
186 my $simple = $self->_generate_simple_get('$_[0]', $name, $spec);
187 if ($self->is_simple_get($name, $spec)) {
190 'do { '.$self->_generate_use_default(
191 '$_[0]', $name, $spec,
192 $self->_generate_simple_has('$_[0]', $name, $spec),
195 ?($self->_generate_isa_check($name, $simple, $spec->{isa}).'; ')
202 sub _generate_simple_has {
203 my ($self, $me, $name) = @_;
204 "exists ${me}->{${\perlstring $name}}";
207 sub _generate_simple_clear {
208 my ($self, $me, $name) = @_;
209 " delete ${me}->{${\perlstring $name}}\n"
212 sub generate_get_default {
214 $self->{captures} = {};
215 my $code = $self->_generate_get_default(@_);
216 ($code, delete $self->{captures});
219 sub _generate_use_default {
220 my ($self, $me, $name, $spec, $test) = @_;
221 my $get_value = $self->_generate_get_default($me, $name, $spec);
222 if ($spec->{coerce}) {
223 $get_value = $self->_generate_coerce(
228 $self->_generate_simple_set(
229 $me, $name, $spec, $get_value
233 sub _generate_get_default {
234 my ($self, $me, $name, $spec) = @_;
236 ? $self->_generate_call_code($name, 'default', $me, $spec->{default})
237 : "${me}->${\$spec->{builder}}"
240 sub generate_simple_get {
241 my ($self, @args) = @_;
242 $self->_generate_simple_get(@args);
245 sub _generate_simple_get {
246 my ($self, $me, $name) = @_;
247 my $name_str = perlstring $name;
248 "${me}->{${name_str}}";
252 my ($self, $name, $spec) = @_;
253 if ($self->is_simple_set($name, $spec)) {
254 $self->_generate_simple_set('$_[0]', $name, $spec, '$_[1]');
256 my ($coerce, $trigger, $isa_check) = @{$spec}{qw(coerce trigger isa)};
257 my $simple = $self->_generate_simple_set('$self', $name, $spec, '$value');
258 my $code = "do { my (\$self, \$value) = \@_;\n";
262 .$self->_generate_coerce($name, '$value', $coerce).";\n";
266 " ".$self->_generate_isa_check($name, '$value', $isa_check).";\n";
269 my $fire = $self->_generate_trigger($name, '$self', '$value', $trigger);
271 " ".$simple.";\n ".$fire.";\n"
274 $code .= " ".$simple.";\n";
281 sub generate_coerce {
283 $self->{captures} = {};
284 my $code = $self->_generate_coerce(@_);
285 ($code, delete $self->{captures});
288 sub _generate_coerce {
289 my ($self, $name, $value, $coerce) = @_;
290 $self->_generate_die_prefix(
291 "coercion for ${\perlstring($name)} failed: ",
292 $self->_generate_call_code($name, 'coerce', "${value}", $coerce)
296 sub generate_trigger {
298 $self->{captures} = {};
299 my $code = $self->_generate_trigger(@_);
300 ($code, delete $self->{captures});
303 sub _generate_trigger {
304 my ($self, $name, $obj, $value, $trigger) = @_;
305 $self->_generate_call_code($name, 'trigger', "${obj}, ${value}", $trigger);
308 sub generate_isa_check {
309 my ($self, @args) = @_;
310 $self->{captures} = {};
311 my $code = $self->_generate_isa_check(@args);
312 ($code, delete $self->{captures});
315 sub _generate_die_prefix {
316 my ($self, $prefix, $inside) = @_;
318 .' my $sig_die = $SIG{__DIE__} || sub { die $_[0] };'."\n"
319 .' local $SIG{__DIE__} = sub {'."\n"
320 .' $sig_die->(ref($_[0]) ? $_[0] : '.perlstring($prefix).'.$_[0]);'."\n"
326 sub _generate_isa_check {
327 my ($self, $name, $value, $check) = @_;
328 $self->_generate_die_prefix(
329 "isa check for ${\perlstring($name)} failed: ",
330 $self->_generate_call_code($name, 'isa_check', $value, $check)
334 sub _generate_call_code {
335 my ($self, $name, $type, $values, $sub) = @_;
336 if (my $quoted = quoted_from_sub($sub)) {
337 my $code = $quoted->[1];
338 if (my $captures = $quoted->[2]) {
339 my $cap_name = qq{\$${type}_captures_for_${name}};
340 $self->{captures}->{$cap_name} = \$captures;
341 Sub::Quote::inlinify(
342 $code, $values, Sub::Quote::capture_unroll($cap_name, $captures, 6), 1
345 Sub::Quote::inlinify($code, $values, undef, 1);
348 my $cap_name = qq{\$${type}_for_${name}};
349 $self->{captures}->{$cap_name} = \$sub;
350 "${cap_name}->(${values})";
354 sub generate_populate_set {
356 $self->{captures} = {};
357 my $code = $self->_generate_populate_set(@_);
358 ($code, delete $self->{captures});
361 sub _generate_populate_set {
362 my ($self, $me, $name, $spec, $source, $test) = @_;
363 if ($self->has_eager_default($name, $spec)) {
364 my $get_indent = ' ' x ($spec->{isa} ? 6 : 4);
365 my $get_default = $self->_generate_get_default(
369 defined($spec->{init_arg})
370 ? "(\n${get_indent} ${test}\n${get_indent} ? ${source}\n${get_indent} : "
374 if ($spec->{coerce}) {
375 $get_value = $self->_generate_coerce(
381 ? " {\n my \$value = ".$get_value.";\n "
382 .$self->_generate_isa_check(
383 $name, '$value', $spec->{isa}
385 .' '.$self->_generate_simple_set($me, $name, $spec, '$value').";\n"
387 : ' '.$self->_generate_simple_set($me, $name, $spec, $get_value).";\n"
391 .$self->_generate_trigger(
392 $name, $me, $self->_generate_simple_get($me, $name, $spec),
401 .$self->_generate_coerce(
409 .$self->_generate_isa_check(
410 $name, $source, $spec->{isa}
414 ." ".$self->_generate_simple_set($me, $name, $spec, $source).";\n"
417 .$self->_generate_trigger(
418 $name, $me, $self->_generate_simple_get($me, $name, $spec),
427 sub _generate_core_set {
428 my ($self, $me, $name, $spec, $value) = @_;
429 my $name_str = perlstring $name;
430 "${me}->{${name_str}} = ${value}";
433 sub _generate_simple_set {
434 my ($self, $me, $name, $spec, $value) = @_;
435 my $name_str = perlstring $name;
437 if ($spec->{weak_ref}) {
438 $value = '$preserve = '.$value;
439 my $simple = $self->_generate_core_set($me, $name, $spec, $value);
440 require Scalar::Util;
442 # Perl < 5.8.3 can't weaken refs to readonly vars
443 # (e.g. string constants). This *can* be solved by:
445 #Internals::SetReadWrite($foo);
446 #Scalar::Util::weaken ($foo);
447 #Internals::SetReadOnly($foo);
449 # but requires XS and is just too damn crazy
450 # so simply throw a better exception
451 my $weak_simple = "my \$preserve; Scalar::Util::weaken(${simple}); no warnings 'void'; \$preserve";
452 Moo::_Utils::lt_5_8_3() ? <<"EOC" : $weak_simple;
455 eval { Scalar::Util::weaken($simple); 1 }
456 ? do { no warnings 'void'; \$preserve; }
458 if( \$@ =~ /Modification of a read-only value attempted/) {
460 Carp::croak( sprintf (
461 'Reference to readonly value in "%s" can not be weakened on Perl < 5.8.3',
470 $self->_generate_core_set($me, $name, $spec, $value);
474 sub _generate_getset {
475 my ($self, $name, $spec) = @_;
476 q{(@_ > 1}."\n ? ".$self->_generate_set($name, $spec)
477 ."\n : ".$self->_generate_get($name, $spec)."\n )";
480 sub _generate_delegation {
481 my ($self, $asserter, $target, $args) = @_;
482 my $arg_string = do {
484 # I could, I reckon, linearise out non-refs here using perlstring
485 # plus something to check for numbers but I'm unsure if it's worth it
486 $self->{captures}{'@curries'} = $args;
492 "shift->${asserter}->${target}(${arg_string});";
496 my ($self, $type, $into, $name, $slot) = @_;
497 Class::XSAccessor->import(
499 $type => { $name => $slot },
505 sub default_construction_string { '{}' }