From: gfx Date: Sun, 7 Mar 2010 08:47:48 +0000 (+0900) Subject: Revert "Refactor pp method generators" X-Git-Tag: 0.50_07~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f031f4d561c30e19ef18f35445ded03d25d40f7d;p=gitmo%2FMouse.git Revert "Refactor pp method generators" This reverts commit 3db78d2a0679fe91cf54ec25013d41db02a440d7. --- diff --git a/lib/Mouse/Meta/Method/Accessor.pm b/lib/Mouse/Meta/Method/Accessor.pm index 3f3bb87..ff6984a 100755 --- a/lib/Mouse/Meta/Method/Accessor.pm +++ b/lib/Mouse/Meta/Method/Accessor.pm @@ -2,14 +2,16 @@ package Mouse::Meta::Method::Accessor; use Mouse::Util qw(:meta); # enables strict and warnings use warnings FATAL => 'recursion'; -use Mouse::Meta::Method::Constructor; # for slot access + +sub _inline_slot{ + my(undef, $self_var, $attr_name) = @_; + return sprintf '%s->{q{%s}}', $self_var, $attr_name; +} sub _generate_accessor_any{ my($method_class, $type, $attribute, $class) = @_; - my $c = 'Mouse::Meta::Method::Constructor'; - - my $key = $attribute->name; + my $name = $attribute->name; my $default = $attribute->default; my $constraint = $attribute->type_constraint; my $builder = $attribute->builder; @@ -20,14 +22,11 @@ sub _generate_accessor_any{ my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef; - my $instance = '$_[0]'; - my $slot = $c->_inline_get_slot($instance, $key);; + my $self = '$_[0]'; + my $slot = $method_class->_inline_slot($self, $name);; - my $accessor = sprintf(<<'END_SUB_START', $class->name, __LINE__, $type, $key, __FILE__); -package %s; -#line %d "%s-accessor for %s (%s) -sub { -END_SUB_START + my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__) + . "sub {\n"; if ($type eq 'rw' || $type eq 'wo') { if($type eq 'rw'){ @@ -36,7 +35,7 @@ END_SUB_START } else{ # writer $accessor .= - 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$key.'") }'. + 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'. '{' . "\n"; } @@ -59,14 +58,14 @@ END_SUB_START # this setter $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref; - $accessor .= $c->_inline_set_slot($instance, $key, $value) . ";\n"; + $accessor .= "$slot = $value;\n"; if ($is_weak) { - $accessor .= $c->_inline_weaken_slot($instance, $key) ." if ref $slot;\n"; + $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n"; } if ($trigger) { - $accessor .= '$trigger->('.$instance.', '.$value.');' . "\n"; + $accessor .= '$trigger->('.$self.', '.$value.');' . "\n"; } $accessor .= "}\n"; @@ -82,31 +81,31 @@ END_SUB_START my $value; if (defined $builder){ - $value = "$instance->\$builder()"; + $value = "$self->\$builder()"; } elsif (ref($default) eq 'CODE'){ - $value = "$instance->\$default()"; + $value = "$self->\$default()"; } else{ $value = '$default'; } - $accessor .= sprintf "if(!%s){\n", $c->_inline_has_slot($instance, $key); + $accessor .= "if(!exists $slot){\n"; if($should_coerce){ - $value = "\$constraint->coerce($value)"; + $accessor .= "$slot = \$constraint->coerce($value)"; } elsif(defined $constraint){ $accessor .= "my \$tmp = $value;\n"; $accessor .= "\$compiled_type_constraint->(\$tmp)"; $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n"; - $value = '$tmp'; + $accessor .= "$slot = \$tmp;\n"; + } + else{ + $accessor .= "$slot = $value;\n"; } - - $accessor .= $c->_inline_set_slot($instance, $key, $value) .";\n"; - if ($is_weak) { - $accessor .= $c->_inline_weaken_slot($instance, $key) . " if ref $slot;\n"; + $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n"; } $accessor .= "}\n"; } diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index e57dd6e..e78318e 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -1,71 +1,40 @@ package Mouse::Meta::Method::Constructor; use Mouse::Util qw(:meta); # enables strict and warnings -sub _inline_create_instance { - my(undef, $class_expr) = @_; - return "bless {}, $class_expr"; -} - -sub _inline_slot { +sub _inline_slot{ my(undef, $self_var, $attr_name) = @_; return sprintf '%s->{q{%s}}', $self_var, $attr_name; } -sub _inline_has_slot { - my($class, $self_var, $attr_name) = @_; - - return sprintf 'exists(%s)', $class->_inline_slot($self_var, $attr_name); -} - -sub _inline_get_slot { - my($class, $self_var, $attr_name) = @_; - - return $class->_inline_slot($self_var, $attr_name); -} - -sub _inline_set_slot { - my($class, $self_var, $attr_name, $rvalue) = @_; - - return $class->_inline_slot($self_var, $attr_name) . " = $rvalue"; -} - -sub _inline_weaken_slot { - my($class, $self_var, $attr_name) = @_; - - return sprintf 'Scalar::Util::weaken(%s)', $class->_inline_slot($self_var, $attr_name); -} - sub _generate_constructor { my ($class, $metaclass, $args) = @_; + my $associated_metaclass_name = $metaclass->name; + my @attrs = $metaclass->get_all_attributes; - my $init_attrs = $class->_generate_processattrs($metaclass, \@attrs); - my $buildargs = $class->_generate_BUILDARGS($metaclass); 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 $class_name = $metaclass->name; - my $source = sprintf(<<'END_CONSTRUCTOR', $class_name, __LINE__, __FILE__, $class_name, $buildargs, $class->_inline_create_instance('$class'), $init_attrs, $buildall); -package %s; -#line %d "constructor of %s (%s)" - sub { - my $class = shift; - return $class->Mouse::Object::new(@_) - if $class ne __PACKAGE__; + 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 - %s; - # create instance - my $instance = %s; + $buildargs; + my \$instance = bless {}, \$class; # process attributes - %s; + $processattrs; # BUILDALL - %s; - return $instance; + $buildall; + return \$instance; } -END_CONSTRUCTOR +... #warn $source; my $code; my $e = do{ @@ -99,8 +68,7 @@ sub _generate_processattrs { my $is_weak_ref = $attr->is_weak_ref; my $need_coercion; - my $instance = '$instance'; - my $instance_slot = $method_class->_inline_get_slot($instance, $key); + my $instance_slot = $method_class->_inline_slot('$instance', $key); my $attr_var = "\$attrs[$index]"; my $constraint_var; @@ -117,7 +85,7 @@ sub _generate_processattrs { $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n"; } if($is_weak_ref){ - $post_process .= $method_class->_inline_weaken_slot($instance, $key) . " if ref $instance_slot;\n"; + $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n"; } if (defined $init_arg) { @@ -129,7 +97,7 @@ sub _generate_processattrs { $value = "$constraint_var->coerce($value)"; } - $code .= $method_class->_inline_set_slot($instance, $key, $value) . ";\n"; + $code .= "$instance_slot = $value;\n"; $code .= $post_process; if ($attr->has_trigger) { @@ -167,9 +135,9 @@ sub _generate_processattrs { $value = "$constraint_var->coerce($value)"; } - $code .= $method_class->_inline_set_slot($instance, $key, $value) . ";\n"; + $code .= "$instance_slot = $value;\n"; if($is_weak_ref){ - $code .= $method_class->_inline_weaken_slot($instance, $key) . ";\n"; + $code .= "Scalar::Util::weaken($instance_slot);\n"; } } }