defer string evals from startup to JIT/lazy. on my machine cuts startup by 9% and...
[gitmo/Moose.git] / lib / Class / MOP / Method / Constructor.pm
CommitLineData
38bf2a25 1
2package Class::MOP::Method::Constructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken';
9use Try::Tiny;
10
38bf2a25 11use base 'Class::MOP::Method::Inlined';
12
13sub new {
14 my $class = shift;
15 my %options = @_;
16
17 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
18 || confess "You must pass a metaclass instance if you want to inline"
19 if $options{is_inline};
20
21 ($options{package_name} && $options{name})
22 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
23
24 my $self = $class->_new(\%options);
25
26 # we don't want this creating
27 # a cycle in the code, if not
28 # needed
29 weaken($self->{'associated_metaclass'});
30
31 $self->_initialize_body;
32
33 return $self;
34}
35
36sub _new {
37 my $class = shift;
38
39 return Class::MOP::Class->initialize($class)->new_object(@_)
40 if $class ne __PACKAGE__;
41
42 my $params = @_ == 1 ? $_[0] : {@_};
43
44 return bless {
45 # inherited from Class::MOP::Method
46 body => $params->{body},
47 # associated_metaclass => $params->{associated_metaclass}, # overriden
48 package_name => $params->{package_name},
49 name => $params->{name},
50 original_method => $params->{original_method},
51
52 # inherited from Class::MOP::Generated
53 is_inline => $params->{is_inline} || 0,
54 definition_context => $params->{definition_context},
55
56 # inherited from Class::MOP::Inlined
57 _expected_method_class => $params->{_expected_method_class},
58
59 # defined in this subclass
60 options => $params->{options} || {},
61 associated_metaclass => $params->{metaclass},
62 }, $class;
63}
64
65## accessors
66
67sub options { (shift)->{'options'} }
68sub associated_metaclass { (shift)->{'associated_metaclass'} }
69
38bf2a25 70## method
71
72sub _initialize_body {
73 my $self = shift;
74 my $method_name = '_generate_constructor_method';
75
76 $method_name .= '_inline' if $self->is_inline;
77
78 $self->{'body'} = $self->$method_name;
79}
80
81sub _eval_environment {
82 my $self = shift;
96fec633 83 return $self->associated_metaclass->_eval_environment;
38bf2a25 84}
85
86sub _generate_constructor_method {
87 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
88}
89
90sub _generate_constructor_method_inline {
91 my $self = shift;
92
93 my $meta = $self->associated_metaclass;
94
95 my @source = (
96 'sub {',
97 $meta->_inline_new_object,
98 '}',
99 );
100
101 warn join("\n", @source) if $self->options->{debug};
102
d0efb39c 103 my $RuNNeR; my $code = bless sub { if (!defined($RuNNeR)) { $RuNNeR = try {
38bf2a25 104 $self->_compile_code(\@source);
105 }
106 catch {
107 my $source = join("\n", @source);
108 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_";
109 };
d0efb39c 110 return $RuNNeR if !defined($_[0]) && ref($_[1]) && ref($_[1]) eq 'RuNNeR'}; goto $RuNNeR},'RuNNeR';
38bf2a25 111
112 return $code;
113}
114
1151;
116
117# ABSTRACT: Method Meta Object for constructors
118
119__END__
120
121=pod
122
123=head1 SYNOPSIS
124
125 use Class::MOP::Method::Constructor;
126
127 my $constructor = Class::MOP::Method::Constructor->new(
128 metaclass => $metaclass,
129 options => {
130 debug => 1, # this is all for now
131 },
132 );
133
134 # calling the constructor ...
135 $constructor->body->execute($metaclass->name, %params);
136
137=head1 DESCRIPTION
138
139This is a subclass of C<Class::MOP::Method> which generates
140constructor methods.
141
142=head1 METHODS
143
144=over 4
145
146=item B<< Class::MOP::Method::Constructor->new(%options) >>
147
148This creates a new constructor object. It accepts a hash reference of
149options.
150
151=over 8
152
153=item * metaclass
154
155This should be a L<Class::MOP::Class> object. It is required.
156
157=item * name
158
159The method name (without a package name). This is required.
160
161=item * package_name
162
163The package name for the method. This is required.
164
165=item * is_inline
166
167This indicates whether or not the constructor should be inlined. This
168defaults to false.
169
170=back
171
172=item B<< $metamethod->is_inline >>
173
174Returns a boolean indicating whether or not the constructor is
175inlined.
176
177=item B<< $metamethod->associated_metaclass >>
178
179This returns the L<Class::MOP::Class> object for the method.
180
181=back
182
183=cut
184