X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FMethod%2FConstructor.pm;h=29017edb231b9081de289c746166aac50bf3e068;hb=d004c8d565f9b314da7652e9368aeb4587ffaa3d;hp=05c8d925c62dd9f7c092794354468bab3e0d398b;hpb=15961c86cfd845e6f46b6c362cc1a4b94ffb45db;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index 05c8d92..29017ed 100644 --- a/lib/Class/MOP/Method/Constructor.pm +++ b/lib/Class/MOP/Method/Constructor.pm @@ -8,7 +8,7 @@ use Carp 'confess'; use Scalar::Util 'blessed', 'weaken'; use Try::Tiny; -our $VERSION = '1.11'; +our $VERSION = '1.12'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -75,7 +75,10 @@ sub associated_metaclass { (shift)->{'associated_metaclass'} } sub _attributes { my $self = shift; - $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ] + $self->{'attributes'} ||= [ + sort { $a->name cmp $b->name } + $self->associated_metaclass->get_all_attributes + ] } ## method @@ -89,6 +92,14 @@ sub _initialize_body { $self->{'body'} = $self->$method_name; } +sub _eval_environment { + my $self = shift; + my $defaults = [map { $_->default } @{ $self->_attributes }]; + return { + '$defaults' => \$defaults, + }; +} + sub _generate_constructor_method { return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } } @@ -96,106 +107,27 @@ sub _generate_constructor_method { sub _generate_constructor_method_inline { my $self = shift; - my $defaults = [map { $_->default } @{ $self->_attributes }]; + my $meta = $self->associated_metaclass; - my $close_over = { - '$defaults' => \$defaults, - }; - - my $source = 'sub {'; - $source .= "\n" . 'my $class = shift;'; + my @source = ( + 'sub {', + $meta->_inline_new_object, + '}', + ); - $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)'; - $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; - - $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};'; - - $source .= "\n" . 'my $instance = ' . $self->associated_metaclass->inline_create_instance('$class'); - my $idx = 0; - $source .= ";\n" . (join ";\n" => map { - $self->_generate_slot_initializer($_, $idx++) - } @{ $self->_attributes }); - if (Class::MOP::metaclass_is_weak($self->associated_metaclass->name)) { - $source .= ";\n" . $self->associated_metaclass->_inline_set_mop_slot('$instance', 'Class::MOP::class_of($class)'); - } - $source .= ";\n" . 'return $instance'; - $source .= ";\n" . '}'; - warn $source if $self->options->{debug}; + warn join("\n", @source) if $self->options->{debug}; my $code = try { - $self->_compile_code( - source => $source, - environment => $close_over, - ); + $self->_compile_code(\@source); } catch { + my $source = join("\n", @source); confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_"; }; return $code; } -sub _generate_slot_initializer { - my $self = shift; - my $attr = shift; - my $idx = shift; - - my $default; - if ($attr->has_default) { - $default = $self->_generate_default_value($attr, $idx); - } elsif( $attr->has_builder ) { - $default = '$instance->'.$attr->builder; - } - - if ( defined( my $init_arg = $attr->init_arg ) ) { - return ( - 'if(exists $params->{\'' - . $init_arg . '\'}){' . "\n" - . $attr->inline_set( - '$instance', - '$params->{\'' . $init_arg . '\'}' - ) - . "\n" . '} ' - . ( - !defined $default ? '' : 'else {' . "\n" - . $attr->inline_set( - '$instance', - $default - ) - . "\n" . '}' - ) - ); - } - elsif ( defined $default ) { - return ( - $attr->inline_set( - '$instance', - $default - ) - . "\n" - ); - } - else { - return ''; - } -} - -sub _generate_default_value { - my ($self, $attr, $index) = @_; - # NOTE: - # default values can either be CODE refs - # in which case we need to call them. Or - # they can be scalars (strings/numbers) - # in which case we can just deal with them - # in the code we eval. - if ($attr->is_default_a_coderef) { - return '$defaults->[' . $index . ']->($instance)'; - } - else { - return '$defaults->[' . $index . ']'; - } -} - 1; __END__