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=cff5fc3c97da6ed653ab56811e28d55dfeda6e2a;hp=103cd319b3c07fa09c273e0d041afd492a9d1dbc;hb=53c495ce524c95ee0fc1ee13f20edeeb382ef89f;hpb=7bf66a9ba9b5cb3c3b10bdd24e27c1170674088a diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 103cd31..cff5fc3 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -5,15 +5,19 @@ use warnings; sub generate_constructor_method_inline { my ($class, $meta) = @_; - my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code + my $associated_metaclass_name = $meta->name; + my @attrs = $meta->get_all_attributes; my $buildall = $class->_generate_BUILDALL($meta); - my $buildargs = $class->_generate_BUILDARGS(); + my $buildargs = $class->_generate_BUILDARGS($meta); my $processattrs = $class->_generate_processattrs($meta, \@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 '$associated_metaclass_name'; + $buildargs; my \$instance = bless {}, \$class; $processattrs; $buildall; @@ -21,9 +25,8 @@ sub generate_constructor_method_inline { } ... - warn $code if $ENV{DEBUG}; - local $@; + #warn $code; my $res = eval $code; die $@ if $@; $res; @@ -32,121 +35,130 @@ sub generate_constructor_method_inline { sub _generate_processattrs { my ($class, $meta, $attrs) = @_; my @res; - for my $index (0..scalar(@$attrs)-1) { + + for my $index (0 .. @$attrs - 1) { my $attr = $attrs->[$index]; - my $from = $attr->init_arg; my $key = $attr->name; + my $code = ''; + + if (defined $attr->init_arg) { + my $from = $attr->init_arg; - my $part1 = do { - my @code; + $code .= "if (exists \$args->{'$from'}) {\n"; - if ($attr->should_coerce) { - push @code, "my \$value = \$attr->coerce_constraint( \$args->{'$from'});"; + if ($attr->should_coerce && $attr->type_constraint) { + $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n"; } else { - push @code, "my \$value = \$args->{'$from'};"; + $code .= "my \$value = \$args->{'$from'};\n"; } if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint( \$value );"; + 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 + ) + } + "; } - push @code, "\$instance->{'$key'} = \$value;"; + $code .= "\$instance->{q{$key}} = \$value;\n"; if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; + $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n"; } - if ( $attr->has_trigger ) { - push @code, "\$attr->trigger->( \$instance, \$value, \$attr );"; + if ($attr->has_trigger) { + $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n"; } - join "\n", @code; - }; + $code .= "\n} else {\n"; + } - my $part2 = do { - my @code; + if ($attr->has_default || $attr->has_builder) { + unless ($attr->is_lazy) { + my $default = $attr->default; + my $builder = $attr->builder; - if ( $attr->has_default || $attr->has_builder ) { - unless ( $attr->is_lazy ) { - my $default = $attr->default; - my $builder = $attr->builder; + $code .= "my \$value = "; - push @code, "my \$value = "; + if ($attr->should_coerce && $attr->type_constraint) { + $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, "; + } - if ($attr->should_coerce) { - push @code, "\$attr->coerce_constraint("; + if ($attr->has_builder) { + $code .= "\$instance->$builder"; } - - if ($attr->has_builder) { - push @code, "\$instance->$builder"; - } - elsif (ref($default) eq 'CODE') { - push @code, "\$attr->default()->()"; - } - else { - push @code, "\$attr->default()"; - } - - if ($attr->should_coerce) { - push @code, ");"; + elsif (ref($default) eq 'CODE') { + $code .= "\$attrs[$index]->{default}->(\$instance)"; } - else { - push @code, ";"; + elsif (!defined($default)) { + $code .= 'undef'; } - - if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint(\$value);"; + elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) { + $code .= $default; } - - push @code, "\$instance->{'$key'} = \$value;"; - - if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; + else { + $code .= "'$default'"; } + + if ($attr->should_coerce) { + $code .= ");\n"; } - join "\n", @code; - } - else { - if ( $attr->is_required ) { - qq{Carp::confess("Attribute ($key) is required");}; - } else { - "" + else { + $code .= ";\n"; } - } - }; - my $code = <<"..."; - { - my \$attr = \$attrs[$index]; - if (exists(\$args->{'$from'})) { - $part1; - } else { - $part2; + + if ($attr->has_type_constraint) { + $code .= "{ + unless (\$attrs[$index]->{type_constraint}->check(\$value)) { + \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint) + } + }"; + } + + $code .= "\$instance->{q{$key}} = \$value;\n"; + + if ($attr->is_weak_ref) { + $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n"; } } -... + } + elsif ($attr->is_required) { + $code .= "Carp::confess('Attribute ($key) is required');"; + } + + $code .= "}\n" if defined $attr->init_arg; + push @res, $code; } - return join "\n", @res; + + return join "\n", q{my @triggers;}, @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; } sub _generate_BUILDARGS { - <<'...'; - do { + my $self = shift; + my $meta = shift; + + if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) { + return 'my $args = $class->BUILDARGS(@_)'; + } + + return <<'...'; + 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 = +{@_}; } - }; ... } @@ -158,9 +170,10 @@ sub _generate_BUILDALL { push @code, q{no strict 'refs';}; push @code, q{no warnings 'once';}; no strict 'refs'; - for my $class ($meta->linearized_isa) { - if (*{ $class . '::BUILD' }{CODE}) { - push @code, qq{${class}::BUILD->(\$instance, \$args);}; + no warnings 'once'; + for my $klass ($meta->linearized_isa) { + if (*{ $klass . '::BUILD' }{CODE}) { + unshift @code, qq{${klass}::BUILD(\$instance, \$args);}; } } return join "\n", @code;