From: Dave Rolsky Date: Sun, 26 Sep 2010 05:16:27 +0000 (-0500) Subject: When inlining constructor, don't access meta instance directly, use metaclass to... X-Git-Tag: 1.09~34 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7d1a576bad6260090ba0d40950f861227ead48a8;p=gitmo%2FClass-MOP.git When inlining constructor, don't access meta instance directly, use metaclass to do inlining --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index af9b9fe..b3b6b97 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -613,6 +613,13 @@ sub _create_meta_instance { return $instance; } +sub inline_create_instance { + my $self = shift; + my ($class) = @_; + + return $self->get_meta_instance->inline_create_instance($class); +} + sub clone_object { my $class = shift; my $instance = shift; @@ -1517,6 +1524,11 @@ metaclass. Returns an instance of the C to be used in the construction of a new instance of the class. +=item B<< $metaclass->inline_create_instance($class_var) >> + +This method takes a variable name, and uses it create an inline snippet of +code that will create a new instance of the class. + =back =head2 Informational predicates diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index 93e549c..993a6bc 100644 --- a/lib/Class/MOP/Method/Constructor.pm +++ b/lib/Class/MOP/Method/Constructor.pm @@ -72,11 +72,6 @@ sub associated_metaclass { (shift)->{'associated_metaclass'} } ## cached values ... -sub _meta_instance { - my $self = shift; - $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance; -} - sub _attributes { my $self = shift; $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ] @@ -114,7 +109,7 @@ sub _generate_constructor_method_inline { $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};'; - $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class'); + $source .= "\n" . 'my $instance = ' . $self->associated_metaclass->inline_create_instance('$class'); my $idx = 0; $source .= ";\n" . (join ";\n" => map { $self->_generate_slot_initializer($_, $idx++) @@ -145,23 +140,18 @@ sub _generate_slot_initializer { } if ( defined( my $init_arg = $attr->init_arg ) ) { - my $mi = $self->_meta_instance; - my $attr_name = $attr->name; - return ( 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" - . $mi->inline_set_slot_value( + . $attr->inline_set( '$instance', - $attr_name, '$params->{\'' . $init_arg . '\'}' ) . "\n" . '} ' . ( !defined $default ? '' : 'else {' . "\n" - . $mi->inline_set_slot_value( + . $attr->inline_set( '$instance', - $attr_name, $default ) . "\n" . '}' @@ -170,9 +160,8 @@ sub _generate_slot_initializer { } elsif ( defined $default ) { return ( - $self->_meta_instance->inline_set_slot_value( + $attr->inline_set( '$instance', - $attr->name, $default ) . "\n" diff --git a/t/010_self_introspection.t b/t/010_self_introspection.t index daf602d..fade182 100644 --- a/t/010_self_introspection.t +++ b/t/010_self_introspection.t @@ -61,6 +61,7 @@ my @class_mop_class_methods = qw( create_anon_class is_anon_class instance_metaclass get_meta_instance + inline_create_instance create_meta_instance _create_meta_instance new_object clone_object construct_instance _construct_instance