implement inlined access to the mop slot, to fix immutable anon classes
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Constructor.pm
CommitLineData
d90b42a6 1
2package Class::MOP::Method::Constructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8343d501 8use Scalar::Util 'blessed', 'weaken';
d90b42a6 9
f014c28b 10our $VERSION = '1.10';
d519662a 11$VERSION = eval $VERSION;
d90b42a6 12our $AUTHORITY = 'cpan:STEVAN';
13
29d4e92a 14use base 'Class::MOP::Method::Inlined';
d90b42a6 15
16sub new {
17 my $class = shift;
18 my %options = @_;
8d2d4c67 19
ad315b75 20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
8d2d4c67 22 if $options{is_inline};
23
b38f3848 24 ($options{package_name} && $options{name})
32202ce2 25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 26
0bfc85b8 27 my $self = $class->_new(\%options);
d90b42a6 28
8d2d4c67 29 # we don't want this creating
30 # a cycle in the code, if not
d90b42a6 31 # needed
8683db0e 32 weaken($self->{'associated_metaclass'});
d90b42a6 33
8f7852d8 34 $self->_initialize_body;
d90b42a6 35
8d2d4c67 36 return $self;
d90b42a6 37}
38
28b97bef 39sub _new {
0bfc85b8 40 my $class = shift;
812d58f9 41
ec9e38e5 42 return Class::MOP::Class->initialize($class)->new_object(@_)
812d58f9 43 if $class ne __PACKAGE__;
ec9e38e5 44
45 my $params = @_ == 1 ? $_[0] : {@_};
46
47 return bless {
48 # inherited from Class::MOP::Method
49 body => $params->{body},
50 # associated_metaclass => $params->{associated_metaclass}, # overriden
51 package_name => $params->{package_name},
52 name => $params->{name},
53 original_method => $params->{original_method},
54
55 # inherited from Class::MOP::Generated
56 is_inline => $params->{is_inline} || 0,
57 definition_context => $params->{definition_context},
58
59 # inherited from Class::MOP::Inlined
60 _expected_method_class => $params->{_expected_method_class},
61
62 # defined in this subclass
63 options => $params->{options} || {},
64 associated_metaclass => $params->{metaclass},
28b97bef 65 }, $class;
66}
67
8d2d4c67 68## accessors
c23184fc 69
8683db0e 70sub options { (shift)->{'options'} }
71sub associated_metaclass { (shift)->{'associated_metaclass'} }
c23184fc 72
565f0cbb 73## cached values ...
d90b42a6 74
780f14c2 75sub _attributes {
565f0cbb 76 my $self = shift;
2620be77 77 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
565f0cbb 78}
d90b42a6 79
80## method
81
8f7852d8 82sub _initialize_body {
565f0cbb 83 my $self = shift;
ecb874a0 84 my $method_name = '_generate_constructor_method';
8d2d4c67 85
565f0cbb 86 $method_name .= '_inline' if $self->is_inline;
8d2d4c67 87
8683db0e 88 $self->{'body'} = $self->$method_name;
565f0cbb 89}
90
ecb874a0 91sub _generate_constructor_method {
2a2b8458 92 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
565f0cbb 93}
94
ecb874a0 95sub _generate_constructor_method_inline {
d90b42a6 96 my $self = shift;
565f0cbb 97
8343d501 98 my $defaults = [map { $_->default } @{ $self->_attributes }];
99
100 my $close_over = {
101 '$defaults' => \$defaults,
102 };
0c6f3280 103
d90b42a6 104 my $source = 'sub {';
26ffbb36 105 $source .= "\n" . 'my $class = shift;';
8d2d4c67 106
26ffbb36 107 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
8d2d4c67 108 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
109
26ffbb36 110 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
111
7d1a576b 112 $source .= "\n" . 'my $instance = ' . $self->associated_metaclass->inline_create_instance('$class');
8343d501 113 my $idx = 0;
8d2d4c67 114 $source .= ";\n" . (join ";\n" => map {
8343d501 115 $self->_generate_slot_initializer($_, $idx++)
780f14c2 116 } @{ $self->_attributes });
7931f7db 117 if (Class::MOP::metaclass_is_weak($self->associated_metaclass->name)) {
118 $source .= ";\n" . $self->associated_metaclass->_inline_set_mop_slot('$instance', 'Class::MOP::class_of($class)');
119 }
d90b42a6 120 $source .= ";\n" . 'return $instance';
8d2d4c67 121 $source .= ";\n" . '}';
122 warn $source if $self->options->{debug};
123
e24b19fb 124 my ( $code, $e ) = $self->_eval_closure(
ffe92c8b 125 $close_over,
126 $source
127 );
e24b19fb 128 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
a6eef5a3 129
130 return $code;
d90b42a6 131}
132
133sub _generate_slot_initializer {
134 my $self = shift;
64adcd8d 135 my $attr = shift;
8343d501 136 my $idx = shift;
8d2d4c67 137
d90b42a6 138 my $default;
139 if ($attr->has_default) {
8343d501 140 $default = $self->_generate_default_value($attr, $idx);
8d2d4c67 141 } elsif( $attr->has_builder ) {
142 $default = '$instance->'.$attr->builder;
d90b42a6 143 }
8d2d4c67 144
e42e1c47 145 if ( defined( my $init_arg = $attr->init_arg ) ) {
146 return (
147 'if(exists $params->{\''
148 . $init_arg . '\'}){' . "\n"
7d1a576b 149 . $attr->inline_set(
e42e1c47 150 '$instance',
e42e1c47 151 '$params->{\'' . $init_arg . '\'}'
152 )
153 . "\n" . '} '
154 . (
155 !defined $default ? '' : 'else {' . "\n"
7d1a576b 156 . $attr->inline_set(
c16c9c1b 157 '$instance',
e42e1c47 158 $default
159 )
160 . "\n" . '}'
161 )
c16c9c1b 162 );
e42e1c47 163 }
164 elsif ( defined $default ) {
c16c9c1b 165 return (
7d1a576b 166 $attr->inline_set(
c16c9c1b 167 '$instance',
e42e1c47 168 $default
169 )
170 . "\n"
c16c9c1b 171 );
e42e1c47 172 }
173 else {
174 return '';
175 }
d90b42a6 176}
177
8343d501 178sub _generate_default_value {
179 my ($self, $attr, $index) = @_;
180 # NOTE:
181 # default values can either be CODE refs
182 # in which case we need to call them. Or
183 # they can be scalars (strings/numbers)
184 # in which case we can just deal with them
185 # in the code we eval.
186 if ($attr->is_default_a_coderef) {
187 return '$defaults->[' . $index . ']->($instance)';
188 }
189 else {
190 return '$defaults->[' . $index . ']';
191 }
192}
193
d90b42a6 1941;
195
d90b42a6 196__END__
197
198=pod
199
8d2d4c67 200=head1 NAME
d90b42a6 201
202Class::MOP::Method::Constructor - Method Meta Object for constructors
203
204=head1 SYNOPSIS
205
96e38ba6 206 use Class::MOP::Method::Constructor;
8d2d4c67 207
96e38ba6 208 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 209 metaclass => $metaclass,
96e38ba6 210 options => {
211 debug => 1, # this is all for now
8d2d4c67 212 },
96e38ba6 213 );
8d2d4c67 214
96e38ba6 215 # calling the constructor ...
b7045e66 216 $constructor->body->execute($metaclass->name, %params);
8d2d4c67 217
d90b42a6 218=head1 DESCRIPTION
219
3fd960d9 220This is a subclass of C<Class::MOP::Method> which generates
221constructor methods.
96e38ba6 222
d90b42a6 223=head1 METHODS
224
225=over 4
226
3fd960d9 227=item B<< Class::MOP::Method::Constructor->new(%options) >>
d90b42a6 228
3fd960d9 229This creates a new constructor object. It accepts a hash reference of
230options.
96e38ba6 231
3fd960d9 232=over 8
96e38ba6 233
3fd960d9 234=item * metaclass
96e38ba6 235
3fd960d9 236This should be a L<Class::MOP::Class> object. It is required.
c23184fc 237
3fd960d9 238=item * name
d90b42a6 239
3fd960d9 240The method name (without a package name). This is required.
96e38ba6 241
3fd960d9 242=item * package_name
d90b42a6 243
3fd960d9 244The package name for the method. This is required.
c23184fc 245
3fd960d9 246=item * is_inline
96e38ba6 247
3fd960d9 248This indicates whether or not the constructor should be inlined. This
249defaults to false.
d90b42a6 250
3fd960d9 251=back
f0de47d9 252
3fd960d9 253=item B<< $metamethod->is_inline >>
f0de47d9 254
3fd960d9 255Returns a boolean indicating whether or not the constructor is
256inlined.
d90b42a6 257
3fd960d9 258=item B<< $metamethod->associated_metaclass >>
96e38ba6 259
3fd960d9 260This returns the L<Class::MOP::Class> object for the method.
d90b42a6 261
565f0cbb 262=back
263
d90b42a6 264=head1 AUTHORS
265
266Stevan Little E<lt>stevan@iinteractive.comE<gt>
267
268=head1 COPYRIGHT AND LICENSE
269
3e2c8600 270Copyright 2006-2010 by Infinity Interactive, Inc.
d90b42a6 271
272L<http://www.iinteractive.com>
273
274This library is free software; you can redistribute it and/or modify
8d2d4c67 275it under the same terms as Perl itself.
d90b42a6 276
277=cut
278