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