-
Notifications
You must be signed in to change notification settings - Fork 38
/
Object.pm
463 lines (365 loc) · 13.7 KB
/
Object.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# See bottom of file for license and copyright information
package Foswiki::Object;
use v5.14;
=begin TML
---+ Class Foswiki::Object
*NOTE:* This document is in draft status and may change as a result of
a discussion, raised concerns or reasonable proposals.
This is the base object for all Foswiki classes. It defines the default
behaviour and general policies for all descendants.
---++ Behavior
=Foswiki::Object= is a subclass of Moo and as such inherits all it's
features.
=cut
require Carp;
require Foswiki::Exception;
use Try::Tiny;
use Scalar::Util qw(blessed refaddr weaken isweak);
use Foswiki::Class;
use Assert;
=begin TML
---++ ClassMethod BUILDARGS()
Converts positional constructor parameters to named ones. Tries to detect if constructor is already being called using named notation.
The =BUILDARGS()= uses array =@_newParameters= declared statically on a class to get information about the order of parameters.
For example, for =Foswiki::Class=:
<verbatim>
package Foswiki::Class;
use Moo;
our @_newParameters = qw( param1 param2 );
use namespace::clean;
has param1 => (is => 'rw');
has param2 => (is => 'ro');
has param3 => (is => 'rw');
1;
</verbatim>
the following notations are valid:
<verbtaim>
my $object1 = Foswiki::Class->new($param1, $param2);
my $object2 = Foswiki::Class->new($param1);
my $object3 = Foswiki::Class->new(param1 => 1, param2 => '2', param3 => 'additional');
my $object3 = Foswiki::Class->new({param1 => 1, param2 => '2', param3 => 'additional'});
</verbatim>
Note that for =$object2= the =BUILD()= method will be called with no param2 key.
Key/value pairs as in =$object3= example are valid as soon as at least one key is mentioned in =@_newParameters=.
This limitation will remain actual until constructor are no more called with positional parameters.
=cut
has __orig_file => ( is => 'rw', clearer => 1, );
has __orig_line => ( is => 'rw', clearer => 1, );
has __orig_stack => ( is => 'rw', clearer => 1, );
has __id => (
is => 'ro',
lazy => 1,
clearer => 1,
default => sub {
my $this = shift;
my $strID = ref($this) . '_' . refaddr($this);
$strID =~ s/:/_/g;
return $strID;
},
);
has __clone_heap =>
( is => 'rw', clearer => 1, lazy => 1, default => sub { {} }, );
around BUILDARGS => sub {
my $orig = shift;
my ( $class, @params ) = @_;
# Skip processing if already have passed with a hash ref.
return $params[0] if @params == 1 && ref( $params[0] ) eq 'HASH';
# Take care of clone-like methods.
if ( ref($class) ) {
$class = ref($class);
}
my $paramHash;
Carp::confess("Undefined \$class") unless defined $class;
no strict 'refs';
if ( defined *{ $class . '::_newParameters' }{ARRAY} ) {
my @newParameters = @{ $class . '::_newParameters' };
my $isHash = 0;
# If there are even number of parameters passed suspect key/value pairs.
# Note: at least one key has to be in @_newParameters for this to work.
if ( ( @params % 2 ) == 0 ) {
my $prop_re = '^(' . join( '|', @newParameters ) . ')$';
# Check for potential keys if any of them is mentioned in @_newParameters.
# Not key/value form if any single suspected-to-be-key is undef.
for ( my $i = 0 ; !$isHash && $i < @params ; $i += 2 ) {
next unless defined $params[$i];
$isHash = ( $params[$i] =~ $prop_re );
}
}
unless ($isHash) {
ASSERT(
scalar(@params) <= scalar(@newParameters),
"object constructor for class $class has received more parameters than defined in \@_newParameters"
) if DEBUG;
while (@params) {
$paramHash->{ shift @newParameters } = shift @params;
}
}
}
# If $paramHash is undef at this point then either @params is a key/value pairs array or no @_newParameters array defined.
# SMELL XXX Number of elements in @params has to be checked and an exception thrown if it's inappropriate.
unless ( defined $paramHash ) {
Foswiki::Exception::Fatal->throw(
text => "Odd number of elements in $class parameters hash" )
if ( @params % 2 ) == 1;
$paramHash = {@params};
}
use strict 'refs';
return $paramHash;
};
sub BUILD {
my $this = shift;
if (DEBUG) {
my ( $pkg, $file, $line );
my $sFrame = 0;
do {
( $pkg, $file, $line ) = caller( ++$sFrame );
} while (
$pkg =~ /^(Foswiki::Object|Moo::|Method::Generate::Constructor)/ );
$this->__orig_file($file);
$this->__orig_line($line);
$this->__orig_stack( Carp::longmess('') );
}
}
sub DEMOLISH {
my $this = shift;
#say STDERR "In ", __PACKAGE__, ": ", ref($this), "::DEMOLISH; ",
# defined($Foswiki::Plugins::SESSION) ? "SESSION" : "NO SESSION"
# if DEBUG;
if ( $this->can('finish') ) {
say STDERR Carp::shortmess(
ref($this) . " supports finish() but it shouldn't." );
# SMELL every Foswiki::Object ancestor has to use DEMOLISH as the standard.
# XXX We have to generate a warning if this condition is met.
$this->finish;
}
if (DEBUG) {
foreach my $key ( keys %{$this} ) {
unless ( $this->can($key) ) {
say STDERR "Key $key on ", ref($this),
" isn't a valid attribute.";
if ( UNIVERSAL::isa( $this->{key}, 'Foswiki::Object' ) ) {
say STDERR " $key is a Foswiki::Object created in ",
$this->{key}->__orig_file, ":", $this->{key}->__orig_line;
}
}
}
}
}
sub _cloneData {
my $this = shift;
my ( $val, $attr ) = @_;
my $heap = $this->__clone_heap;
my $cloned;
if ( my $dataType = ref($val) ) {
my $refAddr = refaddr($val);
# Check if ref has been cloned before and avoid deep recursion.
if ( defined $heap->{cloning_ref}{$refAddr} ) {
Foswiki::Exception::Fatal->throw( text =>
"Circular dependecy detected on a object being cloned for attribute $attr"
);
}
elsif ( defined $heap->{cloned_ref}{$refAddr} ) {
# This reference was already cloned once, try to replicate the
# original data structure by preserving references too.
$cloned = $heap->{cloned_ref}{$refAddr};
}
else {
# Record the reference being cloned.
$heap->{cloning_ref}{$refAddr} = $attr;
if ( my $class = blessed($val) ) {
if ( $val->can('clone') ) {
$cloned = $val->clone;
}
elsif ( ref($val) eq 'Regexp' ) {
$cloned = $val;
}
else {
# Class without clone method. Try to copy it 'manually' by
# cloning as a hash and blessing the resulting hashref into
# $val's class.
# SMELL Pretty much unreliable for complex classes.
$cloned =
$this->_cloneData( {%$val}, "$attr.blessed($class)" );
bless $cloned, ref($val)
if $cloned != $val;
}
}
else {
if ( $dataType eq 'ARRAY' ) {
$cloned = [];
my $idx = 0;
foreach my $item (@$val) {
push @$cloned,
$this->_cloneData( $item, "${attr}.array[$idx]" );
$idx++;
}
}
elsif ( $dataType eq 'HASH' ) {
$cloned = {};
foreach my $key ( keys %$val ) {
$cloned->{$key} =
$this->_cloneData( $val->{$key},
"${attr}.hash{$key}" );
}
}
elsif ( $dataType eq 'SCALAR' ) {
$cloned = \$$val;
}
else {
# One-to-one copy for non-clonable refs.
$cloned = $val;
}
}
# Record the cloned reference.
$heap->{cloned_ref}{$refAddr} = $cloned;
delete $heap->{cloning_ref}{$refAddr};
}
weaken($cloned) if isweak($val);
}
else {
$cloned = $val;
}
return $cloned;
}
# XXX Experimental.
# clone works on low-level bypassing Moo's accessor methods.
sub clone {
my $this = shift;
$this->_clear__clone_heap;
my @profile;
#my $skipRx = '^(' . join( '|', @skip_attrs ) . ')$';
foreach my $attr ( keys %$this ) {
#next if $attr =~ /$skipRx/;
my $destAttr = $attr;
if ( $destAttr =~ /^__/ ) {
next unless $destAttr =~ /^__orig_/;
# Debug attributes would be preserved but those coming from the
# source object would be kinda pushed on a stack by adding extra __
# prefix. This way we could trace object's history.
$destAttr = "__$destAttr";
}
my $clone_method = "_clone_" . $attr;
my $attrVal;
if ( my $method = $this->can($clone_method) ) {
$attrVal = $method->($this);
}
else {
$attrVal = $this->_cloneData( $this->{$attr}, $attr );
}
push @profile, $destAttr, $attrVal;
}
my $newObj = ref($this)->new(@profile);
$this->_clear__clone_heap;
return $newObj;
}
sub _normalizeAttributeName {
my ($attributeName) = @_;
# If attribute defined by its name only try to guess it's classname by
# checking the callstack.
unless ( $attributeName =~ /::/ ) {
my $package;
my $level = 1;
while ( !defined $package ) {
my $pkg = ( caller( $level++ ) )[0];
$package = $pkg unless $pkg =~ /^Foswiki::Object/;
}
$attributeName = "$package::$attributeName";
}
return $attributeName;
}
sub _validateIsaCode {
my ( $attributeName, $code ) = @_;
#say STDERR "Validator code for $attributeName: $code" if DEBUG;
my $codeRef = eval $code;
if ($@) {
Carp::confess
"Compilation of attribute 'isa' validator for $attributeName failed: $@\nValidator code: $code";
}
return $codeRef;
}
=begin TML
---++ StaticMethod isaARRAY( $attributeName, \%opts )
isa validator generator checking for arrayrefs.
=%opts= hash keys:
* =noUndef= – do not allow undef value
* =noEmpty= - do not allow empty array
=cut
sub isaARRAY {
my ( $attributeName, %opts ) = @_;
$attributeName = _normalizeAttributeName($attributeName);
return _validateIsaCode( $attributeName,
'sub { Foswiki::Exception->throw( text => "'
. $attributeName
. ' attribute may only be '
. ( $opts{noUndef} ? '' : 'undef or an ' )
. 'arrayref." ) if '
. ( $opts{noUndef} ? '!defined( $_[0] ) || ' : '' )
. '( defined( $_[0] ) && ( ref( $_[0] ) ne "ARRAY"'
. ( $opts{noEmpty} ? ' || scalar( @{ $_[0] } ) == 0' : '' )
. ' ) ); }' );
}
=begin TML
---++ StaticMethod isaHASH( $attributeName, \%opts )
isa validator generator checking for arrayrefs.
=%opts= hash keys:
* =noUndef= – do not allow undef value
=cut
sub isaHASH {
my ( $attributeName, %opts ) = @_;
$attributeName = _normalizeAttributeName($attributeName);
return _validateIsaCode( $attributeName,
'sub { Foswiki::Exception->throw( text => "'
. $attributeName
. ' attribute may only be '
. ( $opts{noUndef} ? '' : 'undef or an ' )
. 'hashref." ) if '
. ( $opts{noUndef} ? '!defined( $_[0] ) || ' : '' )
. '( defined( $_[0] ) && ( ref( $_[0] ) ne "HASH" ) );'
. ' }' );
}
=begin TML
---++ StaticMethod isaCLASS( $attributeName, $className, \%opts )
isa validator generator checking if attribute is of =$className= class or it's
descendant.
=%opts= hash keys:
* =noUndef= – do not allow undef value
* =strictMatch= - allow only =$className=, no decsendants
* =does= - defines a Role class must do.
=cut
sub isaCLASS {
my ( $attributeName, $className, %opts ) = @_;
$attributeName = _normalizeAttributeName($attributeName);
return _validateIsaCode(
$attributeName,
'sub { Foswiki::Exception->throw( text => "'
. $attributeName
. ' attribute may only be '
. ( $opts{noUndef} ? '' : 'undef or an ' )
. $className
. ' but not " . (defined $_[0] ? ref($_[0]) || $_[0] : "undef") . "." ) if '
. ( $opts{noUndef} ? '!defined( $_[0] ) || ' : '' )
. '( defined( $_[0] ) && ('
. (
$opts{strictMatch}
? 'ref( $_[0] ) ne "' . $className . '"'
: '!$_[0]->isa("' . $className . '")'
)
. ( $opts{does} ? ' || !$_[0]->does("' . $opts{does} . '")' : '' )
. ')' . '); }'
);
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2016 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.