fix bugs in XS support
[gitmo/Role-Tiny.git] / lib / Role / Tiny.pm
1 package Role::Tiny;
2
3 use strictures 1;
4 use Class::Tiny::_Utils;
5
6 our %INFO;
7 our %APPLIED_TO;
8 our %COMPOSED;
9
10 sub import {
11   my $target = caller;
12   strictures->import;
13   # get symbol table reference
14   my $stash = do { no strict 'refs'; \%{"${target}::"} };
15   # install before/after/around subs
16   foreach my $type (qw(before after around)) {
17     *{_getglob "${target}::${type}"} = sub {
18       push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
19     };
20   }
21   *{_getglob "${target}::requires"} = sub {
22     push @{$INFO{$target}{requires}||=[]}, @_;
23   };
24   *{_getglob "${target}::with"} = sub {
25     die "Only one role supported at a time by with" if @_ > 1;
26     Role::Tiny->apply_role_to_package($_[0], $target);
27   };
28   *{_getglob "${target}::has"} = sub {
29     my ($name, %spec) = @_;
30     ($INFO{$target}{accessor_maker} ||= do {
31       require Method::Generate::Accessor;
32       Method::Generate::Accessor->new
33     })->generate_method($target, $name, \%spec);
34     $INFO{$target}{attributes}{$name} = \%spec;
35   };
36   # grab all *non-constant* (ref eq 'SCALAR') subs present
37   # in the symbol table and store their refaddrs (no need to forcibly
38   # inflate constant subs into real subs) - also add '' to here (this
39   # is used later)
40   @{$INFO{$target}{not_methods}={}}{
41     '', map { *$_{CODE}||() } grep !(ref eq 'SCALAR'), values %$stash
42   } = ();
43   # a role does itself
44   $APPLIED_TO{$target} = { $target => undef };
45 }
46
47 sub apply_role_to_package {
48   my ($me, $role, $to) = @_;
49
50   die "This is apply_role_to_package" if ref($to);
51   die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
52
53   $me->_check_requires($to, $role, @{$info->{requires}||[]});
54
55   $me->_install_methods($to, $role);
56
57   $me->_install_modifiers($to, $info->{modifiers});
58
59   # only add does() method to classes and only if they don't have one
60   if (not $INFO{$to} and not $to->can('does')) {
61     *{_getglob "${to}::does"} = \&does_role;
62   }
63
64   $me->_handle_constructor($to, $info->{attributes});
65
66   # copy our role list into the target's
67   @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
68 }
69
70 sub apply_roles_to_object {
71   my ($me, $object, @roles) = @_;
72   die "No roles supplied!" unless @roles;
73   my $class = ref($object);
74   bless($object, $me->create_class_with_roles($class, @roles));
75   $object;
76 }
77
78 sub create_class_with_roles {
79   my ($me, $superclass, @roles) = @_;
80
81   my $new_name = join('+', $superclass, my $compose_name = join '+', @roles);
82   return $new_name if $COMPOSED{class}{$new_name};
83
84   foreach my $role (@roles) {
85     die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
86   }
87
88   require Class::Tiny::_mro;
89   require Sub::Quote;
90
91   my @composable = map $me->_composable_package_for($_), reverse @roles;
92
93   *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ];
94
95   my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles;
96
97   $me->_check_requires(
98     $new_name, $compose_name,
99     do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h }
100   );
101   $me->_handle_constructor(
102     $new_name, { map %{$_->{attr_info}||{}}, @info }
103   );
104
105   *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does');
106
107   @{$APPLIED_TO{$new_name}||={}}{
108     map keys %{$APPLIED_TO{$_}}, @roles
109   } = ();
110
111   $COMPOSED{class}{$new_name} = 1;
112   return $new_name;
113 }
114
115 sub _composable_package_for {
116   my ($me, $role) = @_;
117   my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
118   return $composed_name if $COMPOSED{role}{$composed_name};
119   $me->_install_methods($composed_name, $role);
120   my $base_name = $composed_name.'::_BASE';
121   *{_getglob("${composed_name}::ISA")} = [ $base_name ];
122   my $modifiers = $INFO{$role}{modifiers}||[];
123   foreach my $modified (
124     do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
125   ) {
126     Sub::Quote::quote_sub(
127       "${base_name}::${modified}" => q{ shift->next::method(@_) }
128     );
129   }
130   $me->_install_modifiers($composed_name, $modifiers);
131   $COMPOSED{role}{$composed_name} = 1;
132   return $composed_name;
133 }
134
135 sub _check_requires {
136   my ($me, $to, $name, @requires) = @_;
137   if (my @requires_fail = grep !$to->can($_), @requires) {
138     # role -> role, add to requires, role -> class, error out
139     if (my $to_info = $INFO{$to}) {
140       push @{$to_info->{requires}||=[]}, @requires_fail;
141     } else {
142       die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
143     }
144   }
145 }
146
147 sub _install_methods {
148   my ($me, $to, $role) = @_;
149
150   my $info = $INFO{$role};
151
152   my $methods = $info->{methods} ||= do {
153     # grab role symbol table
154     my $stash = do { no strict 'refs'; \%{"${role}::"}};
155     my $not_methods = $info->{not_methods};
156     +{
157       # grab all code entries that aren't in the not_methods list
158       map {
159         my $code = *{$stash->{$_}}{CODE};
160         # rely on the '' key we added in import for "no code here"
161         exists $not_methods->{$code||''} ? () : ($_ => $code)
162       } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash
163     };
164   };
165
166   # grab target symbol table
167   my $stash = do { no strict 'refs'; \%{"${to}::"}};
168
169   # determine already extant methods of target
170   my %has_methods;
171   @has_methods{grep
172     +((ref($stash->{$_}) eq 'SCALAR') || (*{$stash->{$_}}{CODE})),
173     keys %$stash
174   } = ();
175
176   foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
177     no warnings 'once';
178     *{_getglob "${to}::${i}"} = $methods->{$i};
179   }
180 }
181
182 sub _install_modifiers {
183   my ($me, $to, $modifiers) = @_;
184   foreach my $modifier (@{$modifiers||[]}) {
185     _install_modifier($to, @{$modifier});
186   }
187 }
188
189 sub _handle_constructor {
190   my ($me, $to, $attr_info) = @_;
191   return unless $attr_info && keys %$attr_info;
192   if ($INFO{$to}) {
193     @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info;
194   } else {
195     # only fiddle with the constructor if the target is a Class::Tiny class
196     if ($INC{"Class/Tiny.pm"}
197         and my $con = Class::Tiny->_constructor_maker_for($to)) {
198       $con->register_attribute_specs(%$attr_info);
199     }
200   }
201 }
202
203 sub does_role {
204   my ($package, $role) = @_;
205   return exists $APPLIED_TO{$package}{$role};
206 }
207
208 1;