Fix a bit
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
index 82459c5..d68e958 100644 (file)
@@ -1,36 +1,52 @@
 package Mouse::Meta::Method::Constructor;
-use strict;
-use warnings;
-
-sub generate_constructor_method_inline {
-    my ($class, $meta) = @_;
-
-    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 $code = <<"...";
-    sub {
-        my \$class = shift;
-        my \$args = $buildargs;
-        my \$instance = bless {}, \$class;
-        $processattrs;
-        $buildall;
-        return \$instance;
-    }
+use Mouse::Util; # enables strict and warnings
+
+sub _generate_constructor_method {
+    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 @compiled_constraints = map { $_ ? $_->_compiled_type_constraint : undef }
+                               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;
+            my \$instance = bless {}, \$class;
+            $processattrs;
+            $buildall;
+            return \$instance;
+        }
 ...
 
-    local $@;
-    my $res = eval $code;
-    die $@ if $@;
-    $res;
+    my $code;
+    my $e = do{
+        local $@;
+        $code = eval $source;
+        $@;
+    };
+    die $e if $e;
+
+    $metaclass->add_method($args->{constructor_name} => $code);
+    return;
 }
 
 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;
@@ -39,31 +55,31 @@ sub _generate_processattrs {
         if (defined $attr->init_arg) {
             my $from = $attr->init_arg;
 
-            $code .= "if (exists \$args->{'$from'}) {\n";
+            $code .= "if (exists \$args->{q{$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'});";
-            }
-            else {
-                $code .= "my \$value = \$args->{'$from'};";
-            }
+            my $value = "\$args->{q{$from}}";
+            if(my $type_constraint = $attr->type_constraint){
+                if($attr->should_coerce && $type_constraint->has_coercion){
+                    $code .= "my \$value = \$attrs[$index]->{type_constraint}->coerce(\$args->{q{$from}});\n";
+                    $value = '$value';
+                }
 
-            if ($attr->has_type_constraint) {
-                $code .= "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
-                $code .= "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
+                $code .= "\$compiled_constraints[$index]->($value)\n";
+                $code .= "  or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
             }
 
-            $code .= "\$instance->{'$key'} = \$value;";
+            $code .= "\$instance->{q{$key}} = $value;\n";
 
             if ($attr->is_weak_ref) {
-                $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
+                $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
             }
 
             if ($attr->has_trigger) {
-                $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );";
+                $has_triggers++;
+                $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
             }
 
-            $code .= "} else {";
+            $code .= "\n} else {\n";
         }
 
         if ($attr->has_default || $attr->has_builder) {
@@ -74,94 +90,100 @@ 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 .= "\$attrs[$index]->_coerce_and_verify(";
                 }
 
-                    if ($attr->has_builder) {
-                        $code .= "\$instance->$builder";
-                    }
-                    elsif (ref($default) eq 'CODE') {
-                        $code .= "\$attrs[$index]->{default}->(\$instance)";
-                    }
-                    elsif (!defined($default)) {
-                        $code .= 'undef';
-                    }
-                    elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
-                        $code .= $default;
-                    }
-                    else {
-                        $code .= "'$default'";
-                    }
+                if ($attr->has_builder) {
+                    $code .= "\$instance->$builder()";
+                }
+                elsif (ref($default) eq 'CODE') {
+                    $code .= "\$attrs[$index]->{default}->(\$instance)";
+                }
+                elsif (!defined($default)) {
+                    $code .= 'undef';
+                }
+                elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
+                    $code .= $default;
+                }
+                else {
+                    $code .= "'$default'";
+                }
 
                 if ($attr->should_coerce) {
-                    $code .= ");";
+                    $code .= ");\n";
                 }
                 else {
-                    $code .= ";";
+                    $code .= ";\n";
                 }
 
                 if ($attr->has_type_constraint) {
-                    $code .= "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
-                    $code .= "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->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->{'$key'} = \$value;";
+                $code .= "\$instance->{q{$key}} = \$value;\n";
 
                 if ($attr->is_weak_ref) {
-                    $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
+                    $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
                 }
             }
         }
         elsif ($attr->is_required) {
-            $code .= qq{Carp::confess("Attribute ($key) is required");};
+            $code .= "Carp::confess('Attribute ($key) is required');";
         }
 
-        $code .= "}" if defined $attr->init_arg;
+        $code .= "}\n" if defined $attr->init_arg;
 
         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';
+        no warnings 'once';
+
+        if (*{ $class . '::BUILD' }{CODE}) {
+            unshift  @code, qq{${class}::BUILD(\$instance, \$args);};
         }
     }
     return join "\n", @code;