Commit | Line | Data |
3a63a2e7 |
1 | package Mouse::Meta::Module; |
7d96ae4d |
2 | use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings |
3a63a2e7 |
3 | |
b397e301 |
4 | use Carp (); |
5 | use Scalar::Util (); |
fce211ae |
6 | |
fe1221ad |
7 | my %METAS; |
8536d351 |
8 | |
fd168725 |
9 | if(Mouse::Util::_MOUSE_XS){ |
10 | # register meta storage for performance |
11 | Mouse::Util::__register_metaclass_storage(\%METAS, 0); |
12 | |
13 | # ensure thread safety |
14 | *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) }; |
15 | } |
16 | |
deb9a0f3 |
17 | sub _metaclass_cache { # DEPRECATED |
739525d0 |
18 | my($class, $name) = @_; |
fe1221ad |
19 | return $METAS{$name}; |
739525d0 |
20 | } |
8536d351 |
21 | |
739525d0 |
22 | sub initialize { |
23 | my($class, $package_name, @args) = @_; |
8536d351 |
24 | |
739525d0 |
25 | ($package_name && !ref($package_name)) |
26 | || $class->throw_error("You must pass a package name and it cannot be blessed"); |
8536d351 |
27 | |
fe1221ad |
28 | return $METAS{$package_name} |
739525d0 |
29 | ||= $class->_construct_meta(package => $package_name, @args); |
30 | } |
8536d351 |
31 | |
542f20ad |
32 | sub reinitialize { |
33 | my($class, $package_name, @args) = @_; |
34 | |
35 | ($package_name && !ref($package_name)) |
36 | || $class->throw_error("You must pass a package name and it cannot be blessed"); |
37 | |
38 | delete $METAS{$package_name}; |
39 | return $class->initialize($package_name, @args); |
40 | } |
41 | |
42 | sub _class_of{ |
739525d0 |
43 | my($class_or_instance) = @_; |
44 | return undef unless defined $class_or_instance; |
23274b88 |
45 | return $METAS{ ref($class_or_instance) || $class_or_instance }; |
8536d351 |
46 | } |
47 | |
739525d0 |
48 | # Means of accessing all the metaclasses that have |
49 | # been initialized thus far |
542f20ad |
50 | #sub _get_all_metaclasses { %METAS } |
51 | sub _get_all_metaclass_instances { values %METAS } |
52 | sub _get_all_metaclass_names { keys %METAS } |
53 | sub _get_metaclass_by_name { $METAS{$_[0]} } |
54 | #sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] } |
55 | #sub _weaken_metaclass { weaken($METAS{$_[0]}) } |
56 | #sub _does_metaclass_exist { defined $METAS{$_[0]} } |
57 | #sub _remove_metaclass_by_name { delete $METAS{$_[0]} } |
739525d0 |
58 | |
43165725 |
59 | sub name; |
3a63a2e7 |
60 | |
2591e962 |
61 | sub namespace; |
62 | |
739525d0 |
63 | # The followings are Class::MOP specific methods |
64 | |
65 | #sub version { no strict 'refs'; ${shift->name.'::VERSION'} } |
66 | #sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} } |
67 | #sub identifier { |
68 | # my $self = shift; |
69 | # return join '-' => ( |
70 | # $self->name, |
71 | # ($self->version || ()), |
72 | # ($self->authority || ()), |
73 | # ); |
74 | #} |
23264b5b |
75 | |
8536d351 |
76 | # add_attribute is an abstract method |
77 | |
deb9a0f3 |
78 | sub get_attribute_map { # DEPRECATED |
3a683350 |
79 | Carp::cluck('get_attribute_map() has been deprecated'); |
80 | return $_[0]->{attributes}; |
81 | } |
82 | |
8536d351 |
83 | sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } |
84 | sub get_attribute { $_[0]->{attributes}->{$_[1]} } |
6cfa1e5e |
85 | sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} } |
23264b5b |
86 | |
047d7af0 |
87 | sub get_attribute_list{ keys %{$_[0]->{attributes}} } |
88 | |
89 | |
01afd8ff |
90 | # XXX: for backward compatibility |
91 | my %foreign = map{ $_ => undef } qw( |
92 | Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints |
81bc0675 |
93 | Carp Scalar::Util List::Util |
01afd8ff |
94 | ); |
95 | sub _code_is_mine{ |
81bc0675 |
96 | # my($self, $code) = @_; |
8e64d0fa |
97 | |
81bc0675 |
98 | return !exists $foreign{ get_code_package($_[1]) }; |
3a63a2e7 |
99 | } |
100 | |
3e44140b |
101 | sub add_method; |
102 | |
3a63a2e7 |
103 | sub has_method { |
104 | my($self, $method_name) = @_; |
105 | |
78dc5ed1 |
106 | defined($method_name) |
107 | or $self->throw_error('You must define a method name'); |
108 | |
81bc0675 |
109 | return defined($self->{methods}{$method_name}) || do{ |
110 | my $code = get_code_ref($self->{package}, $method_name); |
111 | $code && $self->_code_is_mine($code); |
112 | }; |
3a63a2e7 |
113 | } |
114 | |
81bc0675 |
115 | sub get_method_body { |
71e7b544 |
116 | my($self, $method_name) = @_; |
117 | |
118 | defined($method_name) |
119 | or $self->throw_error('You must define a method name'); |
120 | |
121 | return $self->{methods}{$method_name} ||= do{ |
7d96ae4d |
122 | my $code = get_code_ref($self->{package}, $method_name); |
81bc0675 |
123 | $code && $self->_code_is_mine($code) ? $code : undef; |
71e7b544 |
124 | }; |
125 | } |
126 | |
8536d351 |
127 | sub get_method{ |
6cfa1e5e |
128 | my($self, $method_name) = @_; |
129 | |
81bc0675 |
130 | if(my $code = $self->get_method_body($method_name)){ |
6cfa1e5e |
131 | my $method_metaclass = $self->method_metaclass; |
132 | load_class($method_metaclass); |
133 | |
9010458d |
134 | return $method_metaclass->wrap( |
81bc0675 |
135 | body => $code, |
9010458d |
136 | name => $method_name, |
60e446e8 |
137 | package => $self->name, |
9010458d |
138 | associated_metaclass => $self, |
6cfa1e5e |
139 | ); |
140 | } |
141 | |
142 | return undef; |
8536d351 |
143 | } |
3a63a2e7 |
144 | |
8e64d0fa |
145 | sub get_method_list { |
3a63a2e7 |
146 | my($self) = @_; |
8e64d0fa |
147 | |
148 | return grep { $self->has_method($_) } keys %{ $self->namespace }; |
3a63a2e7 |
149 | } |
150 | |
7a50b450 |
151 | { |
152 | my $ANON_SERIAL = 0; |
7a50b450 |
153 | |
154 | my %IMMORTALS; |
155 | |
156 | sub create { |
7eb3a8d5 |
157 | my($self, $package_name, %options) = @_; |
7a50b450 |
158 | |
7eb3a8d5 |
159 | my $class = ref($self) || $self; |
160 | $self->throw_error('You must pass a package name') if @_ < 2; |
7a50b450 |
161 | |
8f40866c |
162 | my $superclasses; |
7a50b450 |
163 | if(exists $options{superclasses}){ |
f48920c1 |
164 | if(Mouse::Util::is_a_metarole($self)){ |
7a50b450 |
165 | delete $options{superclasses}; |
166 | } |
8f40866c |
167 | else{ |
168 | $superclasses = delete $options{superclasses}; |
169 | (ref $superclasses eq 'ARRAY') |
7eb3a8d5 |
170 | || $self->throw_error("You must pass an ARRAY ref of superclasses"); |
8f40866c |
171 | } |
7a50b450 |
172 | } |
173 | |
8f40866c |
174 | my $attributes = delete $options{attributes}; |
175 | if(defined $attributes){ |
176 | (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH') |
7eb3a8d5 |
177 | || $self->throw_error("You must pass an ARRAY ref of attributes"); |
8f40866c |
178 | } |
179 | my $methods = delete $options{methods}; |
180 | if(defined $methods){ |
181 | (ref $methods eq 'HASH') |
7eb3a8d5 |
182 | || $self->throw_error("You must pass a HASH ref of methods"); |
8f40866c |
183 | } |
184 | my $roles = delete $options{roles}; |
185 | if(defined $roles){ |
186 | (ref $roles eq 'ARRAY') |
7eb3a8d5 |
187 | || $self->throw_error("You must pass an ARRAY ref of roles"); |
8f40866c |
188 | } |
7a50b450 |
189 | my $mortal; |
190 | my $cache_key; |
191 | |
192 | if(!defined $package_name){ # anonymous |
193 | $mortal = !$options{cache}; |
194 | |
195 | # anonymous but immortal |
196 | if(!$mortal){ |
8e64d0fa |
197 | # something like Super::Class|Super::Class::2=Role|Role::1 |
198 | $cache_key = join '=' => ( |
8f40866c |
199 | join('|', @{$superclasses || []}), |
200 | join('|', sort @{$roles || []}), |
7a50b450 |
201 | ); |
202 | return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key}; |
203 | } |
8f40866c |
204 | $options{anon_serial_id} = ++$ANON_SERIAL; |
205 | $package_name = $class . '::__ANON__::' . $ANON_SERIAL; |
7a50b450 |
206 | } |
207 | |
208 | # instantiate a module |
209 | { |
210 | no strict 'refs'; |
211 | ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version}; |
212 | ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority}; |
213 | } |
214 | |
7eb3a8d5 |
215 | my $meta = $self->initialize( $package_name, %options); |
7a50b450 |
216 | |
b397e301 |
217 | Scalar::Util::weaken $METAS{$package_name} |
7a50b450 |
218 | if $mortal; |
219 | |
7eb3a8d5 |
220 | $meta->add_method(meta => sub{ |
221 | $self->initialize(ref($_[0]) || $_[0]); |
7a50b450 |
222 | }); |
223 | |
8f40866c |
224 | $meta->superclasses(@{$superclasses}) |
225 | if defined $superclasses; |
7a50b450 |
226 | |
227 | # NOTE: |
228 | # process attributes first, so that they can |
229 | # install accessors, but locally defined methods |
230 | # can then overwrite them. It is maybe a little odd, but |
231 | # I think this should be the order of things. |
232 | if (defined $attributes) { |
233 | if(ref($attributes) eq 'ARRAY'){ |
8f40866c |
234 | # array of Mouse::Meta::Attribute |
7a50b450 |
235 | foreach my $attr (@{$attributes}) { |
8f40866c |
236 | $meta->add_attribute($attr); |
7a50b450 |
237 | } |
238 | } |
239 | else{ |
8f40866c |
240 | # hash map of name and attribute spec pairs |
7a50b450 |
241 | while(my($name, $attr) = each %{$attributes}){ |
242 | $meta->add_attribute($name => $attr); |
243 | } |
244 | } |
245 | } |
8f40866c |
246 | if (defined $methods) { |
247 | while(my($method_name, $method_body) = each %{$methods}){ |
248 | $meta->add_method($method_name, $method_body); |
7a50b450 |
249 | } |
250 | } |
8f40866c |
251 | if (defined $roles){ |
252 | Mouse::Util::apply_all_roles($package_name, @{$roles}); |
7a50b450 |
253 | } |
254 | |
8f40866c |
255 | if($cache_key){ |
7a50b450 |
256 | $IMMORTALS{$cache_key} = $meta; |
257 | } |
258 | |
259 | return $meta; |
260 | } |
261 | |
262 | sub DESTROY{ |
263 | my($self) = @_; |
264 | |
265 | my $serial_id = $self->{anon_serial_id}; |
266 | |
267 | return if !$serial_id; |
268 | |
e75f21db |
269 | # @ISA is a magical variable, so we clear it manually. |
270 | @{$self->{superclasses}} = () if exists $self->{superclasses}; |
271 | |
272 | # Then, clear the symbol table hash |
273 | %{$self->namespace} = (); |
7a50b450 |
274 | |
8f40866c |
275 | my $name = $self->name; |
276 | delete $METAS{$name}; |
277 | |
e75f21db |
278 | $name =~ s/ $serial_id \z//xms; |
7a50b450 |
279 | |
280 | no strict 'refs'; |
8f40866c |
281 | delete ${$name}{ $serial_id . '::' }; |
7a50b450 |
282 | |
283 | return; |
284 | } |
285 | } |
286 | |
8536d351 |
287 | sub throw_error{ |
288 | my($class, $message, %args) = @_; |
289 | |
fce211ae |
290 | local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0); |
8e64d0fa |
291 | local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though |
3a63a2e7 |
292 | |
8536d351 |
293 | if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0 |
294 | Carp::croak($message); |
295 | } |
296 | else{ |
297 | Carp::confess($message); |
298 | } |
299 | } |
3a63a2e7 |
300 | |
301 | 1; |
302 | |
303 | __END__ |
304 | |
305 | =head1 NAME |
306 | |
1820fffe |
307 | Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role |
3a63a2e7 |
308 | |
a25ca8d6 |
309 | =head1 VERSION |
310 | |
1e582397 |
311 | This document describes Mouse version 0.40_06 |
a25ca8d6 |
312 | |
b995be6a |
313 | =head1 SEE ALSO |
314 | |
31c5194b |
315 | L<Class::MOP::Class> |
316 | |
317 | L<Class::MOP::Module> |
318 | |
319 | L<Class::MOP::Package> |
b995be6a |
320 | |
3a63a2e7 |
321 | =cut |
2cea7a5f |
322 | |