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