Make mouse_accessor_get_self() "static inline"
[gitmo/Mouse.git] / lib / Mouse / PurePerl.pm
CommitLineData
ccb38d0b 1package Mouse::PurePerl;
2
a52cca04 3require Mouse::Util;
4
df6dd016 5package
6 Mouse::Util;
7
8use strict;
9use warnings;
10
11use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
12
13use B ();
14
15sub is_class_loaded {
16 my $class = shift;
17
18 return 0 if ref($class) || !defined($class) || !length($class);
19
20 # walk the symbol table tree to avoid autovififying
21 # \*{${main::}{"Foo::"}} == \*main::Foo::
22
23 my $pack = \%::;
24 foreach my $part (split('::', $class)) {
25 my $entry = \$pack->{$part . '::'};
26 return 0 if ref($entry) ne 'GLOB';
27 $pack = *{$entry}{HASH} or return 0;
28 }
29
30 # check for $VERSION or @ISA
31 return 1 if exists $pack->{VERSION}
32 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
33 return 1 if exists $pack->{ISA}
34 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
35
36 # check for any method
37 foreach my $name( keys %{$pack} ) {
38 my $entry = \$pack->{$name};
39 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
40 }
41
42 # fail
43 return 0;
44}
45
46
47# taken from Sub::Identify
48sub get_code_info {
49 my ($coderef) = @_;
50 ref($coderef) or return;
51
52 my $cv = B::svref_2object($coderef);
53 $cv->isa('B::CV') or return;
54
55 my $gv = $cv->GV;
56 $gv->isa('B::GV') or return;
57
58 return ($gv->STASH->NAME, $gv->NAME);
59}
60
61sub get_code_package{
62 my($coderef) = @_;
63
64 my $cv = B::svref_2object($coderef);
65 $cv->isa('B::CV') or return '';
66
67 my $gv = $cv->GV;
68 $gv->isa('B::GV') or return '';
69
70 return $gv->STASH->NAME;
71}
72
7d96ae4d 73sub get_code_ref{
74 my($package, $name) = @_;
75 no strict 'refs';
76 no warnings 'once';
77 use warnings FATAL => 'uninitialized';
78 return *{$package . '::' . $name}{CODE};
79}
80
e3540312 81sub generate_isa_predicate_for {
1d5ecd5f 82 my($for_class, $name) = @_;
83
f48920c1 84 my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
1d5ecd5f 85
86 if(defined $name){
87 no strict 'refs';
88 *{ caller() . '::' . $name } = $predicate;
89 return;
90 }
91
92 return $predicate;
93}
94
95
f48920c1 96package
97 Mouse::Util::TypeConstraints;
98
99use Scalar::Util qw(blessed looks_like_number openhandle);
100
7d96ae4d 101sub Any { 1 }
102sub Item { 1 }
7d96ae4d 103
104sub Bool { $_[0] ? $_[0] eq '1' : 1 }
105sub Undef { !defined($_[0]) }
106sub Defined { defined($_[0]) }
107sub Value { defined($_[0]) && !ref($_[0]) }
108sub Num { !ref($_[0]) && looks_like_number($_[0]) }
109sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
110sub Str { defined($_[0]) && !ref($_[0]) }
111
112sub Ref { ref($_[0]) }
113sub ScalarRef { ref($_[0]) eq 'SCALAR' }
114sub ArrayRef { ref($_[0]) eq 'ARRAY' }
115sub HashRef { ref($_[0]) eq 'HASH' }
116sub CodeRef { ref($_[0]) eq 'CODE' }
117sub RegexpRef { ref($_[0]) eq 'Regexp' }
118sub GlobRef { ref($_[0]) eq 'GLOB' }
119
120sub FileHandle {
121 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
122}
123
124sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
125
126sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
127sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
128
619338ac 129sub _parameterize_ArrayRef_for {
130 my($type_parameter) = @_;
131 my $check = $type_parameter->_compiled_type_constraint;
132
133 return sub {
134 foreach my $value (@{$_}) {
135 return undef unless $check->($value);
136 }
137 return 1;
138 }
139}
140
141sub _parameterize_HashRef_for {
142 my($type_parameter) = @_;
143 my $check = $type_parameter->_compiled_type_constraint;
144
145 return sub {
146 foreach my $value(values %{$_}){
147 return undef unless $check->($value);
148 }
149 return 1;
150 };
151}
152
153# 'Maybe' type accepts 'Any', so it requires parameters
154sub _parameterize_Maybe_for {
155 my($type_parameter) = @_;
156 my $check = $type_parameter->_compiled_type_constraint;
157
158 return sub{
159 return !defined($_) || $check->($_);
160 };
161};
162
163
7d96ae4d 164
43165725 165package
166 Mouse::Meta::Module;
167
926404f2 168sub name { $_[0]->{package} }
169
170sub _method_map { $_[0]->{methods} }
c5f6ad05 171sub _attribute_map{ $_[0]->{attributes} }
43165725 172
2591e962 173sub namespace{
174 my $name = $_[0]->{package};
175 no strict 'refs';
176 return \%{ $name . '::' };
177}
178
3e44140b 179sub add_method {
180 my($self, $name, $code) = @_;
181
182 if(!defined $name){
183 $self->throw_error('You must pass a defined name');
184 }
185 if(!defined $code){
186 $self->throw_error('You must pass a defined code');
187 }
188
189 if(ref($code) ne 'CODE'){
190 $code = \&{$code}; # coerce
191 }
192
193 $self->{methods}->{$name} = $code; # Moose stores meta object here.
194
195 my $pkg = $self->name;
196 no strict 'refs';
197 no warnings 'redefine', 'once';
198 *{ $pkg . '::' . $name } = $code;
199 return;
200}
201
202
43165725 203package
204 Mouse::Meta::Class;
205
a5c683f6 206sub constructor_class() { 'Mouse::Meta::Method::Constructor' }
207sub destructor_class() { 'Mouse::Meta::Method::Destructor' }
4e7e3250 208
43165725 209sub is_anon_class{
210 return exists $_[0]->{anon_serial_id};
211}
212
213sub roles { $_[0]->{roles} }
214
cccb83de 215sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
216
da4432f3 217sub get_all_attributes {
218 my($self) = @_;
047d7af0 219 my %attrs = map { %{ $self->initialize($_)->{attributes} } } reverse $self->linearized_isa;
da4432f3 220 return values %attrs;
221}
222
4e7e3250 223sub _initialize_object{
224 my($self, $object, $args, $ignore_triggers) = @_;
225
226 my @triggers_queue;
227
228 foreach my $attribute ($self->get_all_attributes) {
229 my $init_arg = $attribute->init_arg;
230 my $slot = $attribute->name;
231
232 if (defined($init_arg) && exists($args->{$init_arg})) {
233 $object->{$slot} = $attribute->_coerce_and_verify($args->{$init_arg}, $object);
234
235 weaken($object->{$slot})
236 if ref($object->{$slot}) && $attribute->is_weak_ref;
237
238 if ($attribute->has_trigger) {
239 push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
240 }
241 }
242 else { # no init arg
243 if ($attribute->has_default || $attribute->has_builder) {
244 if (!$attribute->is_lazy) {
245 my $default = $attribute->default;
246 my $builder = $attribute->builder;
247 my $value = $builder ? $object->$builder()
248 : ref($default) eq 'CODE' ? $object->$default()
249 : $default;
250
251 $object->{$slot} = $attribute->_coerce_and_verify($value, $object);
252
253 weaken($object->{$slot})
254 if ref($object->{$slot}) && $attribute->is_weak_ref;
255 }
256 }
257 elsif($attribute->is_required) {
258 $self->throw_error("Attribute (".$attribute->name.") is required");
259 }
260 }
261 }
262
263 if(!$ignore_triggers){
264 foreach my $trigger_and_value(@triggers_queue){
265 my($trigger, $value) = @{$trigger_and_value};
266 $trigger->($object, $value);
267 }
268 }
269
270 if($self->is_anon_class){
271 $object->{__METACLASS__} = $self;
272 }
273
274 return;
275}
276
277
43165725 278package
279 Mouse::Meta::Role;
280
281sub is_anon_role{
282 return exists $_[0]->{anon_serial_id};
283}
284
285sub get_roles { $_[0]->{roles} }
286
287package
288 Mouse::Meta::Attribute;
289
d7d8d49b 290use Mouse::Meta::Method::Accessor;
43165725 291
292# readers
293
294sub name { $_[0]->{name} }
295sub associated_class { $_[0]->{associated_class} }
296
297sub accessor { $_[0]->{accessor} }
298sub reader { $_[0]->{reader} }
299sub writer { $_[0]->{writer} }
300sub predicate { $_[0]->{predicate} }
301sub clearer { $_[0]->{clearer} }
302sub handles { $_[0]->{handles} }
303
304sub _is_metadata { $_[0]->{is} }
305sub is_required { $_[0]->{required} }
306sub default { $_[0]->{default} }
307sub is_lazy { $_[0]->{lazy} }
308sub is_lazy_build { $_[0]->{lazy_build} }
309sub is_weak_ref { $_[0]->{weak_ref} }
310sub init_arg { $_[0]->{init_arg} }
311sub type_constraint { $_[0]->{type_constraint} }
312
313sub trigger { $_[0]->{trigger} }
314sub builder { $_[0]->{builder} }
315sub should_auto_deref { $_[0]->{auto_deref} }
316sub should_coerce { $_[0]->{coerce} }
317
d899d3e7 318sub documentation { $_[0]->{documentation} }
319
43165725 320# predicates
321
322sub has_accessor { exists $_[0]->{accessor} }
323sub has_reader { exists $_[0]->{reader} }
324sub has_writer { exists $_[0]->{writer} }
325sub has_predicate { exists $_[0]->{predicate} }
326sub has_clearer { exists $_[0]->{clearer} }
327sub has_handles { exists $_[0]->{handles} }
328
329sub has_default { exists $_[0]->{default} }
330sub has_type_constraint { exists $_[0]->{type_constraint} }
331sub has_trigger { exists $_[0]->{trigger} }
332sub has_builder { exists $_[0]->{builder} }
333
d899d3e7 334sub has_documentation { exists $_[0]->{documentation} }
335
93540011 336sub accessor_metaclass(){ 'Mouse::Meta::Method::Accessor' }
337
43165725 338package
339 Mouse::Meta::TypeConstraint;
340
341sub name { $_[0]->{name} }
342sub parent { $_[0]->{parent} }
343sub message { $_[0]->{message} }
344
345sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
346
93540011 347sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
df6dd016 348
93540011 349sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
df6dd016 350
f790c46b 351
352sub compile_type_constraint{
353 my($self) = @_;
354
355 # add parents first
356 my @checks;
357 for(my $parent = $self->{parent}; defined $parent; $parent = $parent->{parent}){
358 if($parent->{hand_optimized_type_constraint}){
359 unshift @checks, $parent->{hand_optimized_type_constraint};
360 last; # a hand optimized constraint must include all the parents
361 }
362 elsif($parent->{constraint}){
363 unshift @checks, $parent->{constraint};
364 }
365 }
366
367 # then add child
368 if($self->{constraint}){
369 push @checks, $self->{constraint};
370 }
371
372 if($self->{type_constraints}){ # Union
373 my @types = map{ $_->{compiled_type_constraint} } @{ $self->{type_constraints} };
374 push @checks, sub{
375 foreach my $c(@types){
376 return 1 if $c->($_[0]);
377 }
378 return 0;
379 };
380 }
381
382 if(@checks == 0){
383 $self->{compiled_type_constraint} = \&Mouse::Util::TypeConstraints::Any;
384 }
385 else{
386 $self->{compiled_type_constraint} = sub{
387 my(@args) = @_;
388 local $_ = $args[0];
389 foreach my $c(@checks){
390 return undef if !$c->(@args);
391 }
392 return 1;
393 };
394 }
395 return;
396}
397
aa2d2e2c 398package
399 Mouse::Object;
400
401
402sub BUILDARGS {
403 my $class = shift;
f790c46b 404
aa2d2e2c 405 if (scalar @_ == 1) {
406 (ref($_[0]) eq 'HASH')
407 || $class->meta->throw_error("Single parameters to new() must be a HASH ref");
408
409 return {%{$_[0]}};
410 }
411 else {
412 return {@_};
413 }
414}
f790c46b 415
af04626d 416sub new {
417 my $class = shift;
418
419 $class->meta->throw_error('Cannot call new() on an instance') if ref $class;
420
421 my $args = $class->BUILDARGS(@_);
422
423 my $meta = Mouse::Meta::Class->initialize($class);
424 my $self = $meta->new_object($args);
425
426 # BUILDALL
427 if( $self->can('BUILD') ) {
428 for my $class (reverse $meta->linearized_isa) {
429 my $build = Mouse::Util::get_code_ref($class, 'BUILD')
430 || next;
431
432 $self->$build($args);
433 }
434 }
435
436 return $self;
437}
438
a5c683f6 439sub DESTROY {
440 my $self = shift;
441
442 return unless $self->can('DEMOLISH'); # short circuit
443
444 local $?;
445
446 my $e = do{
447 local $@;
448 eval{
449
450 # DEMOLISHALL
451
452 # We cannot count on being able to retrieve a previously made
453 # metaclass, _or_ being able to make a new one during global
454 # destruction. However, we should still be able to use mro at
455 # that time (at least tests suggest so ;)
456
457 foreach my $class (@{ Mouse::Util::get_linear_isa(ref $self) }) {
458 my $demolish = Mouse::Util::get_code_ref($class, 'DEMOLISH')
459 || next;
460
461 $self->$demolish();
462 }
463 };
464 $@;
465 };
466
467 no warnings 'misc';
468 die $e if $e; # rethrow
469}
470
df6dd016 4711;
472__END__
ccb38d0b 473
474=head1 NAME
475
476Mouse::PurePerl - A Mouse guts in pure Perl
477
478=head1 VERSION
479
1e582397 480This document describes Mouse version 0.40_06
ccb38d0b 481
482=head1 SEE ALSO
483
484L<Mouse::XS>
485
486=cut