X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=ea374196297150edff12e9bfe76ccefefcbae58a;hp=ba7b28be16f2a29cdf5ce7af23e8b3b1b0b7fdd6;hb=7ca5c5fb6e084d9c57bc022b336458afc74c6847;hpb=7756897fd756804423cafb8f8806443b768269f0 diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index ba7b28b..ea37419 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -3,17 +3,23 @@ use strict; use warnings; sub generate_constructor_method_inline { - my ($class, $meta) = @_; + my ($class, $metaclass) = @_; - my @attrs = $meta->compute_all_applicable_attributes; - my $buildall = $class->_generate_BUILDALL($meta); - my $buildargs = $class->_generate_BUILDARGS($meta); - my $processattrs = $class->_generate_processattrs($meta, \@attrs); + my $associated_metaclass_name = $metaclass->name; + my @attrs = $metaclass->get_all_attributes; + + my $buildall = $class->_generate_BUILDALL($metaclass); + my $buildargs = $class->_generate_BUILDARGS($metaclass); + my $processattrs = $class->_generate_processattrs($metaclass, \@attrs); + + my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs; my $code = <<"..."; sub { my \$class = shift; - my \$args = $buildargs; + return \$class->Mouse::Object::new(\@_) + if \$class ne q{$associated_metaclass_name}; + $buildargs; my \$instance = bless {}, \$class; $processattrs; $buildall; @@ -28,9 +34,11 @@ sub generate_constructor_method_inline { } sub _generate_processattrs { - my ($class, $meta, $attrs) = @_; + my ($class, $metaclass, $attrs) = @_; my @res; + my $has_triggers; + for my $index (0 .. @$attrs - 1) { my $attr = $attrs->[$index]; my $key = $attr->name; @@ -42,29 +50,35 @@ sub _generate_processattrs { $code .= "if (exists \$args->{'$from'}) {\n"; if ($attr->should_coerce && $attr->type_constraint) { - $code .= "my \$value = Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n"; + $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n"; } else { $code .= "my \$value = \$args->{'$from'};\n"; } if ($attr->has_type_constraint) { - $code .= "{ - local \$_ = \$value; - unless (\$attrs[$index]->{find_type_constraint}->(\$_)) { - \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint) + if ($attr->type_constraint->{_compiled_type_constraint}) { + $code .= "unless (\$compiled_constraints[$index](\$value)) {"; + } else { + $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {"; + } + $code .= " + \$attrs[$index]->verify_type_constraint_error( + q{$key}, \$value, \$attrs[$index]->type_constraint + ) } - }"; + "; } - $code .= "\$instance->{'$key'} = \$value;\n"; + $code .= "\$instance->{q{$key}} = \$value;\n"; if ($attr->is_weak_ref) { - $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n"; + $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n"; } if ($attr->has_trigger) { - $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );\n"; + $has_triggers++; + $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n"; } $code .= "\n} else {\n"; @@ -78,7 +92,7 @@ sub _generate_processattrs { $code .= "my \$value = "; if ($attr->should_coerce && $attr->type_constraint) { - $code .= "Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, "; + $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, "; } if ($attr->has_builder) { @@ -106,17 +120,16 @@ sub _generate_processattrs { if ($attr->has_type_constraint) { $code .= "{ - local \$_ = \$value; - unless (\$attrs[$index]->{find_type_constraint}->(\$_)) { - \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint) + unless (\$attrs[$index]->{type_constraint}->check(\$value)) { + \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint) } }"; } - $code .= "\$instance->{'$key'} = \$value;\n"; + $code .= "\$instance->{q{$key}} = \$value;\n"; if ($attr->is_weak_ref) { - $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n"; + $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n"; } } } @@ -129,47 +142,49 @@ sub _generate_processattrs { push @res, $code; } + if($metaclass->is_anon_class){ + push @res, q{$instnace->{__METACLASS__} = $metaclass;}; + } + + if($has_triggers){ + unshift @res, q{my @triggers;}; + push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; + } + return join "\n", @res; } sub _generate_BUILDARGS { - my $self = shift; - my $meta = shift; + my($self, $metaclass) = @_; - if ($meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) { - return '$class->BUILDARGS(@_)'; + if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) { + return 'my $args = $class->BUILDARGS(@_)'; } return <<'...'; - do { + my $args; if ( scalar @_ == 1 ) { - if ( defined $_[0] ) { - ( ref( $_[0] ) eq 'HASH' ) + ( ref( $_[0] ) eq 'HASH' ) || Carp::confess "Single parameters to new() must be a HASH ref"; - +{ %{ $_[0] } }; - } - else { - +{}; - } + $args = +{ %{ $_[0] } }; } else { - +{@_}; + $args = +{@_}; } - }; ... } sub _generate_BUILDALL { - my ($class, $meta) = @_; - return '' unless $meta->name->can('BUILD'); - - my @code = (); - push @code, q{no strict 'refs';}; - push @code, q{no warnings 'once';}; - no strict 'refs'; - for my $klass ($meta->linearized_isa) { - if (*{ $klass . '::BUILD' }{CODE}) { - push @code, qq{${klass}::BUILD(\$instance, \$args);}; + my ($class, $metaclass) = @_; + + return '' unless $metaclass->name->can('BUILD'); + + my @code; + for my $class ($metaclass->linearized_isa) { + no strict 'refs'; + + if (*{ $class . '::BUILD' }{CODE}) { + unshift @code, qq{${class}::BUILD(\$instance, \$args);}; } } return join "\n", @code;