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=0c929a764471cbe6196615edacda92f86f9c77aa;hp=c0cf03e9c43f3e094d552a5c0dc6725a7096eeb0;hb=e128626c409797822ffd8a4079f833eb3dc0fd37;hpb=e8ba7b267968d4ca0e94d03276ca4c33fed40482 diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index c0cf03e..0c929a7 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -1,168 +1,245 @@ package Mouse::Meta::Method::Constructor; -use strict; -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 $buildall = $class->_generate_BUILDALL($meta); - my $buildargs = $class->_generate_BUILDARGS(); - my $processattrs = $class->_generate_processattrs($meta, \@attrs); - - my $code = <<"..."; - sub { - my \$class = shift; - my \$args = $buildargs; - my \$instance = bless {}, \$class; - $processattrs; - $buildall; - return \$instance; - } -... +use Mouse::Util qw(:meta); # enables strict and warnings - warn $code if $ENV{DEBUG}; +sub _inline_slot{ + my(undef, $self_var, $attr_name) = @_; + return sprintf '%s->{q{%s}}', $self_var, $attr_name; +} - local $@; - my $res = eval $code; - die $@ if $@; - $res; +sub _generate_constructor { + my ($class, $metaclass, $args) = @_; + + 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 @checks = map { $_ && $_->_compiled_type_constraint } + map { $_->type_constraint } @attrs; + + my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"..."; + sub \{ + my \$class = shift; + return \$class->Mouse::Object::new(\@_) + if \$class ne q{$associated_metaclass_name}; + # BUILDARGS + $buildargs; + my \$instance = bless {}, \$class; + # process attributes + $processattrs; + # BUILDALL + $buildall; + return \$instance; + } +... + #warn $source; + my $code; + my $e = do{ + local $@; + $code = eval $source; + $@; + }; + die $e if $e; + return $code; } sub _generate_processattrs { - my ($class, $meta, $attrs) = @_; + my ($method_class, $metaclass, $attrs) = @_; my @res; - for my $index (0..scalar(@$attrs)-1) { + + my $has_triggers; + my $strict_constructor = $metaclass->__strict_constructor; + + + if($strict_constructor){ + push @res, 'my $used = 0;'; + } + + for my $index (0 .. @$attrs - 1) { + my $code = ''; + my $attr = $attrs->[$index]; - my $from = $attr->init_arg; my $key = $attr->name; - my $part1 = do { - my @code; + my $init_arg = $attr->init_arg; + my $type_constraint = $attr->type_constraint; + my $is_weak_ref = $attr->is_weak_ref; + my $need_coercion; - push @code, "my \$value = \$args->{'$from'};"; + my $instance_slot = $method_class->_inline_slot('$instance', $key); + my $attr_var = "\$attrs[$index]"; + my $constraint_var; - if ($attr->should_coerce) { - push @code, "\$value = \$attr->coerce_constraint( \$value );"; - } + if(defined $type_constraint){ + $constraint_var = "$attr_var\->{type_constraint}"; + $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion); + } + + $code .= "# initialize $key\n"; - if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint( \$value );"; + my $post_process = ''; + if(defined $type_constraint){ + $post_process .= "\$checks[$index]->($instance_slot)"; + $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n"; + } + if($is_weak_ref){ + $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n"; + } + + if (defined $init_arg) { + my $value = "\$args->{q{$init_arg}}"; + + $code .= "if (exists $value) {\n"; + + if($need_coercion){ + $value = "$constraint_var->coerce($value)"; } - push @code, "\$instance->{'$key'} = \$value;"; + $code .= "$instance_slot = $value;\n"; + $code .= $post_process; - if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; + if ($attr->has_trigger) { + $has_triggers++; + $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n"; } - if ( $attr->has_trigger ) { - push @code, "\$attr->trigger->( \$instance, \$value, \$attr );"; + if ($strict_constructor){ + $code .= '$used++;' . "\n"; } - join "\n", @code; - }; - - my $part2 = do { - my @code; - - if ( $attr->has_default || $attr->has_builder ) { - unless ( $attr->is_lazy ) { - my $default = $attr->default; - my $builder = $attr->builder; - - push @code, "my \$value = "; - - if ($attr->should_coerce) { - push @code, "\$attr->coerce_constraint("; - } - - 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, ");"; - } - else { - push @code, ";"; - } - - if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint(\$value);"; - } - - push @code, "\$instance->{'$key'} = \$value;"; - - if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; - } + $code .= "\n} else {\n"; # $value exists + } + + if ($attr->has_default || $attr->has_builder) { + unless ($attr->is_lazy) { + my $default = $attr->default; + my $builder = $attr->builder; + + my $value; + if (defined($builder)) { + $value = "\$instance->$builder()"; } - join "\n", @code; - } - else { - if ( $attr->is_required ) { - qq{Carp::confess("Attribute ($key) is required");}; - } else { - "" + elsif (ref($default) eq 'CODE') { + $value = "$attr_var\->{default}->(\$instance)"; } - } - }; - my $code = <<"..."; - { - my \$attr = \$attrs[$index]; - if (exists(\$args->{'$from'})) { - $part1; - } else { - $part2; + elsif (defined($default)) { + $value = "$attr_var\->{default}"; + } + else { + $value = 'undef'; + } + + if($need_coercion){ + $value = "$constraint_var->coerce($value)"; + } + + $code .= "$instance_slot = $value;\n"; + if($is_weak_ref){ + $code .= "Scalar::Util::weaken($instance_slot);\n"; } } -... + } + elsif ($attr->is_required) { + $code .= "Carp::confess('Attribute ($key) is required');"; + } + + $code .= "}\n" if defined $init_arg; + push @res, $code; } + + if($strict_constructor){ + push @res, q{if($used < keys %{$args})} + . q{{ Mouse::Meta::Method::Constructor::_report_unknown_args($metaclass, \@attrs, $instance, $args) }}; + } + + if($metaclass->is_anon_class){ + push @res, q{$instance->{__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 { - <<'...'; - do { + my(undef, $metaclass) = @_; + + my $class = $metaclass->name; + if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::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 = +{@_}; } - }; ... } 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 $class ($meta->linearized_isa) { - if (*{ $class . '::BUILD' }{CODE}) { - push @code, qq{${class}::BUILD->(\$instance, \$args);}; + my (undef, $metaclass) = @_; + + return '' unless $metaclass->name->can('BUILD'); + + my @code; + for my $class ($metaclass->linearized_isa) { + if (Mouse::Util::get_code_ref($class, 'BUILD')) { + unshift @code, qq{${class}::BUILD(\$instance, \$args);}; } } return join "\n", @code; } +sub _report_unknown_args { + my($metaclass, $attrs, $instance, $args) = @_; + + my @unknowns; + my %init_args; + foreach my $attr(@{$attrs}){ + my $init_arg = $attr->init_arg; + if(defined $init_arg){ + $init_args{$init_arg}++; + } + } + + while(my $key = each %{$args}){ + if(!exists $init_args{$key}){ + push @unknowns, $key; + } + } + + $metaclass->throw_error( sprintf + "Unknown attribute passed to the constructor of %s: %s", + ref($instance), join ', ', @unknowns + ); +} + 1; +__END__ + +=head1 NAME + +Mouse::Meta::Method::Constructor - A Mouse method generator for constructors + +=head1 VERSION + +This document describes Mouse version 0.50_01 + +=head1 SEE ALSO + +L + +=cut