use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
-our $VERSION = '0.62_01';
+our $VERSION = '0.71_01';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Moose::Meta::Method',
my $self = shift;
my $metaclass = $self->associated_metaclass;
- if ( my $constructor = $metaclass->find_method_by_name( $self->name ) ) {
+ my $expected_class = $self->_expected_constructor_class;
+
+ # If any of our parents have been made immutable, we are okay to
+ # inline our own new method. The assumption is that an inlined new
+ # method provided by a parent does not actually get used by
+ # children anyway.
+ for my $meta (
+ grep { $_->is_immutable }
+ map { ( ref $metaclass )->initialize($_) }
+ grep { $_ ne $expected_class }
+ $metaclass->linearized_isa
+ ) {
+ my $transformer = $meta->get_immutable_transformer;
+
+ # This is actually a false positive if we're in a subclass of
+ # this class, _and_ the expected class is not overridden (but
+ # should be), and the real expected class is actually
+ # immutable itself (see Fey::Object::Table for an example of
+ # how this can happen). I'm not sure how to actually handle
+ # that case, since it's effectively a bug in the subclass (for
+ # not overriding _expected_constructor_class).
+ return 1 if $transformer->inlined_constructor;
+ }
- my $expected_class = $self->_expected_constructor_class;
+ if ( my $constructor = $metaclass->find_method_by_name( $self->name ) ) {
+ my $class = $self->associated_metaclass->name;
if ( $constructor->body != $expected_class->can('new') ) {
- my $class = $metaclass->name;
- warn "Not inlining a constructor for $class since it is not"
+ my $warning
+ = "Not inlining a constructor for $class since it is not"
. " inheriting the default $expected_class constructor\n";
+ $warning .= " (constructor has method modifiers which would be lost if it were inlined)\n"
+ if $constructor->isa('Class::MOP::Method::Wrapped');
+
+ warn $warning;
+
return 0;
}
else {
# this was changed in 0.41, but broke MooseX::Singleton, so try to catch
# any other code using the original broken spelling
-sub intialize_body { Moose->throw_error("Please correct the spelling of 'intialize_body' to 'initialize_body'") }
+sub intialize_body { $_[0]->throw_error("Please correct the spelling of 'intialize_body' to 'initialize_body'") }
sub initialize_body {
my $self = shift;
$self->_generate_slot_initializer($_)
} 0 .. (@{$self->attributes} - 1));
- $source .= ";\n" . $self->_generate_triggers();
+ $source .= ";\n" . $self->_generate_triggers();
$source .= ";\n" . $self->_generate_BUILDALL();
$source .= ";\n" . 'return $instance';
$source .= ";\n" . '}';
warn $source if $self->options->{debug};
- my $code;
- {
- my $meta = $self; # FIXME for _inline_throw_error...
-
- # NOTE:
- # create the nessecary lexicals
- # to be picked up in the eval
- my $attrs = $self->attributes;
-
- # We need to check if the attribute ->can('type_constraint')
- # since we may be trying to immutabilize a Moose meta class,
- # which in turn has attributes which are Class::MOP::Attribute
- # objects, rather than Moose::Meta::Attribute. And
- # Class::MOP::Attribute attributes have no type constraints.
- # However we need to make sure we leave an undef value there
- # because the inlined code is using the index of the attributes
- # to determine where to find the type constraint
-
- my @type_constraints = map {
- $_->can('type_constraint') ? $_->type_constraint : undef
- } @$attrs;
-
- my @type_constraint_bodies = map {
- defined $_ ? $_->_compiled_type_constraint : undef;
- } @type_constraints;
-
- $code = eval $source;
- $self->throw_error("Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source ) if $@;
- }
+ # We need to check if the attribute ->can('type_constraint')
+ # since we may be trying to immutabilize a Moose meta class,
+ # which in turn has attributes which are Class::MOP::Attribute
+ # objects, rather than Moose::Meta::Attribute. And
+ # Class::MOP::Attribute attributes have no type constraints.
+ # However we need to make sure we leave an undef value there
+ # because the inlined code is using the index of the attributes
+ # to determine where to find the type constraint
+
+ my $attrs = $self->attributes;
+
+ my @type_constraints = map {
+ $_->can('type_constraint') ? $_->type_constraint : undef
+ } @$attrs;
+
+ my @type_constraint_bodies = map {
+ defined $_ ? $_->_compiled_type_constraint : undef;
+ } @type_constraints;
+
+ my $code = $self->_compile_code(
+ code => $source,
+ environment => {
+ '$meta' => \$self,
+ '$attrs' => \$attrs,
+ '@type_constraints' => \@type_constraints,
+ '@type_constraint_bodies' => \@type_constraint_bodies,
+ },
+ ) or $self->throw_error("Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source );
+
$self->{'body'} = $code;
}
return join("\n",
'do {',
$self->_inline_throw_error('"Single parameters to new() must be a HASH ref"', 'data => $_[0]'),
- ' if scalar @_ == 1 && defined $_[0] && ref($_[0]) ne q{HASH};',
+ ' if scalar @_ == 1 && !( defined $_[0] && ref $_[0] eq q{HASH} );',
'(scalar @_ == 1) ? {%{$_[0]}} : {@_};',
'}',
);
. '$instance, '
. $self->meta_instance->inline_get_slot_value(
'$instance',
- ("'" . $attr->name . "'")
+ $attr->name,
)
. ', '
. '$attrs->[' . $i . ']'
$source = (
$self->meta_instance->inline_set_slot_value(
'$instance',
- ("'" . $attr->name . "'"),
+ $attr->name,
$value
) . ';'
);
"\n" .
$self->meta_instance->inline_weaken_slot_value(
'$instance',
- ("'" . $attr->name . "'")
+ $attr->name
) .
' if ref ' . $value . ';'
);
return '$attrs->[' . $index . ']->default($instance)';
}
else {
- my $default = $attr->default;
- # make sure to quote strings ...
- return "'$default'";
-
+ return q{"} . quotemeta( $attr->default ) . q{"};
}
}
=head1 DESCRIPTION
This is a subclass of L<Class::MOP::Method> which handles
-constructing an approprate Constructor methods. This is primarily
+constructing an appropriate Constructor methods. This is primarily
used in the making of immutable metaclasses, otherwise it is
not particularly useful.
=item B<new>
+=item B<can_be_inlined>
+
=item B<attributes>
=item B<meta_instance>
=head1 COPYRIGHT AND LICENSE
-Copyright 2006-2008 by Infinity Interactive, Inc.
+Copyright 2006-2009 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>