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