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