JE/Object/Function.pm

filter: Statementstmt 96.2% 207 / 215 Branchbran 83.6% 92 / 110 Conditioncond 76.7% 33 / 43 MC/DCmcdc 66.6% 16 / 24 Subroutinesub 94.1% 32 / 34 Podpod 100% 8 / 8 totaltotal 89.4% 388 / 434 CC 76 SCAR 22.3
CC
76
Cov
90%
new
18.1
apply
15.3
construct
14.7
CRAP 9.3
« JE/Object/Number/maxvalue.pl ↑ Summary JE/Destroyer.pm »
1 package JE::Object::Function;
2  
3 101 our $VERSION = '0.066';
4  
5  
6 101 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 101 ▶use warnings; no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 101 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 101 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 use overload
12 101 ▶ fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶ '&{}' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
14 11 my $self = shift;
15 11 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
16 11 my $ret = $self->call($self->global->upgrade(@_));
17 11 ▶ typeof $ret eq 'undefined' ? undef : $ret
Branch: 1 branch
BranchTrueFalseTotal
$ret->typeof eq 'undefined' ? : 1 10 11
18 }
19 101 };
20  
21 101 our @ISA = 'JE::Object';
22  
23 101 require JE::Code ;
24 101 require JE::Number ;
25 101 require JE::Object ;
26 101 require JE::Object::Error::TypeError;
27 101 require JE::Parser ;
28 101 require JE::Scope ;
29  
30 101 import JE::Code 'add_line_number';
31 sub add_line_number;
32  
33 =head1 NAME
34  
35 JE::Object::Function - JavaScript function class
36  
37 =head1 SYNOPSIS
38  
39 use JE::Object::Function;
40  
41 # simple constructors:
42  
43 $f = new JE::Object::Function $scope, @argnames, $function;
44 $f = new JE::Object::Function $scope, $function;
45  
46 # constructor that lets you do anything:
47  
48 $f = new JE::Object::Function {
49 name => $name,
50 scope => $scope,
51 length => $number_of_args,
52 argnames => [ @argnames ],
53 function => $function,
54 function_args => [ $arglist ],
55 constructor => sub { ... },
56 constructor_args => [ $arglist ],
57 downgrade => 0,
58 };
59  
60  
61 $f->(@args);
62 $f->call_with($obj, @args);
63  
64 =head1 DESCRIPTION
65  
66 All JavaScript functions are instances of this class. If you want to call
67 a JavaScript function from Perl, just treat is as a coderef (C<< $f->() >>)
68 or use the C<call_with> method (C<< $f->call_with($obj, @args) >>) if you
69 want to specify the invocant (the 'this' value).
70  
71 =head1 OBJECT CREATION
72  
73 =over 4
74  
75 =item new
76  
77 Creates and returns a new function (see the next few items for its usage).
78 The new function will have a C<prototype> property that is an object with
79 a C<constructor> property that refers to the function itself.
80  
81 The return value of the function will be upgraded if necessary (see
82 L<UPGRADING VALUES|JE::Types/UPGRADING VALUES> in the JE::Types man page),
83 which is why C<new> I<has> to be given a reference to the global object
84 or the scope chain. (But see also L<JE/new_function> and L<JE/new_method>.)
85  
86 A function written in Perl can return an lvalue if it wants to. Use
87 S<< C<new JE::LValue($object, 'property name')> >> to create it. To create
88 an lvalue
89 that
90 refers to a variable visible within the function's scope, use
91 S<< C<<< $scope->var('varname') >>> >> (this assumes that you have
92 shifted the scope object off C<@_> and called it C<$scope>; you also need
93 to call C<new> with hashref syntax and specify the C<function_args> [see
94 below]).
95  
96 =item new JE::Object::Function $scope_or_global, @argnames, $function;
97  
98 =item new JE::Object::Function $scope_or_global, $function;
99  
100 C<$scope_or_global> is one of the following:
101  
102 - a global (JE) object
103 - a scope chain (JE::Scope) object
104  
105 C<@argnames> is a list of argument names, that JavaScript functions use to access the arguments.
106  
107 $function is one of
108  
109 - a string containing the body of the function (JavaScript code)
110 - a JE::Code object
111 - a coderef
112  
113 =item new JE::Object::Function { ... };
114  
115 This is the big fancy way of creating a function that lets you do anything.
116 The elements of the hash ref passed to C<new> are as follows (they are
117 all optional, except for C<scope>):
118  
119 =over 4
120  
121 =item name
122  
123 The name of the function. This is used only by C<toString>.
124  
125 =item scope
126  
127 A global object or scope chain object.
128  
129 =item length
130  
131 The number of arguments expected. If this is omitted, the number of
132 elements of C<argnames> will be used. If that is omitted, 0 will be used.
133 Note that this does not cause the argument list to be checked. It only
134 provides the C<length> property (and possibly, later, an C<arity> property)
135 for inquisitive scripts to look at.
136  
137 =item argnames
138  
139 An array ref containing the variable names that a JS function uses to
140 access the
141 arguments.
142  
143 =item function
144  
145 A coderef, string of JS code or JE::Code object (the body of the function).
146  
147 This will be run when the function is called from JavaScript without the
148 C<new> keyword, or from Perl via the C<call> method.
149  
150 =item function_args
151  
152 This only applies when C<function> is a code ref. C<function_args> is an
153 array ref, the elements being strings that indicated what arguments should
154 be passed to the Perl subroutine. The strings, and what they mean, are
155 as follows:
156  
157 self the function object itself
158 scope the scope chain
159 global the global object
160 this the invocant
161 args the arguments passed to the function (as individual
162 arguments)
163 [args] the arguments passed to the function (as an array ref)
164  
165 If C<function_args> is omitted, 'args' will be assumed.
166  
167 =item constructor
168  
169 A code ref that creates and initialises a new object. This is called when
170 the C<new> keyword is used in JavaScript, or when the C<construct> method
171 is used in Perl.
172  
173 If this is omitted, when C<new> or C<construct> is used, a new empty object
174 will be created and passed to the
175 sub specified under C<function> as its 'this' value. The return value of
176 the sub will be
177 returned I<if> it is an object; the (possibly modified) object originally
178 passed to the function will be returned otherwise.
179  
180 =item constructor_args
181  
182 Like C<function_args>, but the C<'this'> string does not apply. If
183 C<constructor_args> is
184 omitted, the arg list will be set to
185 C<[ qw( scope args ) ]> (B<this might change>).
186  
187 This is completely ignored if C<constructor> is
188 omitted.
189  
190 =item downgrade (not yet implemented)
191  
192 This applies only when C<function> or C<constructor> is a code ref. This
193 is a boolean indicating whether the arguments to the function should have
194 their C<value> methods called automatically.; i.e., as though
195 S<<< C<< map $_->value, @args >> >>> were used instead of C<@args>.
196  
197 =item no_proto
198  
199 If this is set to true, the returned function will have no C<prototype>
200 property.
201  
202 =back
203  
204 =back
205  
206 =head1 METHODS
207  
208 =over 4
209  
210 =item new JE::Object::Function
211  
212 See L<OBJECT CREATION>.
213  
214 =cut
215  
216 3814 ▶sub new {
Subroutine new: called CC=18 SCAR=28.9
Pod documented
217 # E 15.3.2
218 3814 my($class,$scope) = (shift,shift);
219 3814 my %opts;
220  
221 3814 ▶ if(ref $scope eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scope eq 'HASH') { } 3756 58 3814
222 3756 %opts = %$scope;
223 3756 $scope = $opts{scope};
224 }
225 else {
226 %opts = @_ == 1 # bypass param-parsing for the sake of
227 # efficiency
228 ? ( function => shift )
229 58 ▶ : ( argnames => do {
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 23 35 58
230 35 my $src = '(' . join(',', @_[0..$#_-1]) .
231 ')';
232 35 $src =~ s/\p{Cf}//g;
233 # ~~~ What should I do here for the file
234 # name and the starting line number?
235 35 my $params = JE::Parser::_parse(
236 params => $src, $scope
237 );
238 35 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 4 31 35
239 31 $params;
240 },
241 function => pop )
242 ;
243 }
244  
245 3810 ▶ defined blessed $scope
Branch: 2 branches
BranchTrueFalseTotal
defined builtin::blessed($scope) or croak(q[The 'scope' passed to JE::Object::Function->new (] . (defined $scope ? $scope : "undef") . ') is not an object') 3810 0 3810
defined $scope ? : 0 0 0
246 or croak "The 'scope' passed to JE::Object::Function->new (" .
247 (defined $scope ? $scope : 'undef') . ") is not an object";
248  
249 # ~~~ I should be able to remove the need for this to be a JE::Scope. Per-
250 # haps it could be an array ref instead. That way, the caller won’t
251 # have to bless something that we copy & bless further down anyway.
252 # Right now, other parts of the code base rely on it, so it would
253 # require a marathon debugging session.
254 3810 ▶ ref $scope ne 'JE::Scope' and $scope = bless [$scope], 'JE::Scope';
Branch: 1 branch
BranchTrueFalseTotal
ref $scope ne "JE::Scope" and $scope = bless([$scope], "JE::Scope") 3473 337 3810
255 3810 my $global = $$scope[0];
256  
257 3810 my $self = $class->SUPER::new($global, {
258 prototype => $global->prototype_for('Function')
259 });
260 3810 my $guts = $$self;
261  
262 3810 $$guts{scope} = $scope;
263  
264  
265 3810 ▶ $opts{no_proto} or $self->prop({
Branch: 1 branch
BranchTrueFalseTotal
$opts{"no_proto"} or $self->prop({"name", "prototype", "dontdel", 1, "value", "JE::Object"->new($global)})->prop({"name", "constructor", "dontenum", 1, "value", $self}) 2438 1372 3810
266 name => 'prototype',
267 dontdel => 1,
268 value => JE::Object->new($global),
269 })->prop({
270 name => 'constructor',
271 dontenum => 1,
272 value => $self,
273 });
274  
275 101 ▶ { no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 $$guts{function} =
278 ref($opts{function}) =~ /^(?:JE::Code|CODE)\z/ ? $opts{function}
279 : length $opts{function} &&
280 (
281 3810 ▶ parse $global $opts{function} or die
Branch: 1 branch
BranchTrueFalseTotal
ref($opts{'function'}) =~ /^(?:JE::Code|CODE)\z/ ? : 3760 50 3810
Condition100%5/5
length $opts{"function"} and $global->parse($opts{"function"}) || die()
!ll&&!rl&&r
20129
$global->parse($opts{"function"}) or die
l!l
246
MC/DC100%2/2
length $opts{"function"} and $global->parse($opts{"function"}) || die()
length $opts{"function"} $global->parse($opts{"function"})
Truth table100%3/3
A and B
A= length $opts{"function"}B= $global->parse($opts{"function"})
ABresult
0X0
100
111
282 )
283 ;
284  
285 $self->prop({
286 name => 'length',
287 value => JE::Number->new($global, $opts{length} ||
288 (ref $opts{argnames} eq 'ARRAY'
289 3804 ▶ ? scalar @{$opts{argnames}} : 0)),
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'argnames'} eq 'ARRAY' ? : 2026 1606 3632
Condition100%3/3
$opts{'length'} || (ref $opts{'argnames'} eq 'ARRAY' ? scalar @{$opts{'argnames'};} : 0)
l!l&&r!l&&!r
17217671865
MC/DC100%2/2
$opts{'length'} || (ref $opts{'argnames'} eq 'ARRAY' ? scalar @{$opts{'argnames'};} : 0)
$opts{'length'} (ref $opts{'argnames'} eq 'ARRAY' ? scalar @{$opts{'argnames'};} : 0)
Truth table100%3/3
A || B
A= $opts{'length'}B= (ref $opts{'argnames'} eq 'ARRAY' ? scalar @{$opts{'argnames'};} : 0)
ABresult
1X1
011
000
290 dontenum => 1,
291 dontdel => 1,
292 readonly => 1,
293 });
294  
295 } #warnings back on
296  
297 $$guts{func_argnames} = [
298 3804 ▶ ref $opts{argnames} eq 'ARRAY' ? @{$opts{argnames}} : ()
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'argnames'} eq 'ARRAY' ? : 2064 1740 3804
299 ];
300 $$guts{func_args} = [
301 ref $opts{function_args} eq 'ARRAY'
302 3804 ▶ ? @{$opts{function_args}} :
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'function_args'} eq 'ARRAY' ? : 3418 386 3804
303 'args'
304 ];
305  
306 3804 ▶ if(exists $opts{constructor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"constructor"}) 208 3596 3804
307 208 $$guts{constructor} = $opts{constructor};
308 $$guts{constructor_args} = [
309 ref $opts{constructor_args} eq 'ARRAY'
310 208 ▶ ? @{$opts{constructor_args}} : ('scope', 'args')
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'constructor_args'} eq 'ARRAY' ? : 208 0 208
311 # ~~~ what is the most useful default here?
312 ];
313 }
314 3804 ▶ if(exists $opts{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"name"}) 3593 211 3804
315 3593 $$guts{func_name} = $opts{name};
316 }
317  
318 3804 $self->prop({dontdel=>1, name=>'arguments',value=>$global->null});
319
320 3804 $self;
321 }
322  
323  
324 =item call_with ( $obj, @args )
325  
326 Calls a function with the given arguments. The C<$obj> becomes the
327 function's invocant. This method is intended for general use from the Perl
328 side. The arguments (including C<$obj>) are automatically upgraded.
329  
330 =cut
331  
332 2 ▶sub call_with {
Subroutine call_with: called CC=2 SCAR=6.9
Pod documented
333 2 my $func = shift;
334 2 my $ret = $func->apply( $func->global->upgrade(@_) );
335 2 ▶ typeof $ret eq 'undefined' ? undef : $ret
Branch: 1 branch
BranchTrueFalseTotal
$ret->typeof eq 'undefined' ? : 1 1 2
336 }
337  
338 =item call ( @args )
339  
340 This method, intended mainly for internal use, calls a function with the
341 given arguments, without upgrading them. The invocant (the 'this' value)
342 will be the global object. This is just a wrapper around C<apply>.
343  
344 This method is very badly named and will probably be renamed in a future
345 version. Does anyone have any suggestions?
346  
347 =cut
348  
349 182 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
350 182 my $self = shift;
351 182 $self->apply($$$self{global}, @_);
352 }
353  
354  
355  
356  
357 =item construct
358  
359 This method, likewise intended mainly for internal use, calls the
360 constructor, if this function has one (functions written in JS
361 don't have this). Otherwise, an object will be created and passed to the
362 function as its invocant. The return value of the function will be
363 discarded, and the object (possibly modified) will be returned instead.
364  
365 =cut
366  
367 1117 ▶sub construct { # ~~~ we need to upgrade the args passed to construct, but
Subroutine construct: called CC=14 SCAR=26.9
Pod documented
368 # still retain the unupgraded values to pass to the
369 # function *if* the function wants them downgraded
370 1117 my $self = shift;
371 1117 my $guts = $$self;
372 1117 my $global = $$guts{global};
373 1117 ▶ if(exists $$guts{constructor}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $guts->{'constructor'} and ref $guts->{'constructor'} eq 'CODE') { } 1014 103 1117
Condition66%2/3
exists $guts->{"constructor"} and ref $guts->{"constructor"} eq "CODE"
!ll&&!rl&&r
10301014
MC/DC50%1/2
exists $guts->{"constructor"} and ref $guts->{"constructor"} eq "CODE"
exists $guts->{"constructor"} ref $guts->{"constructor"} eq "CODE"
Truth table66%2/3
A and B
A= exists $guts->{"constructor"}B= ref $guts->{"constructor"} eq "CODE"
ABresult
0X0
100
111
374 and ref $$guts{constructor} eq 'CODE') {
375 1014 my $code = $$guts{constructor};
376 1014 my @args;
377 1014 for( @{ $$guts{constructor_args} } ) {
378 push @args,
379 $_ eq 'self'
380 ? $self
381 : $_ eq 'scope'
382 ? _init_scope($self, $$guts{scope},
383 2004 ▶ [], @_)
Branch: 5 branches
BranchTrueFalseTotal
$_ eq 'self' ? : 0 2004 2004
$_ eq 'scope' ? : 951 1053 2004
$_ eq 'global' ? : 39 1014 1053
$_ eq 'args' ? : 1014 0 1014
$_ eq '[args]' ? : 0 0 0
384 : $_ eq 'global'
385 ? $global
386 : $_ eq 'args'
387 ? @_ # ~~~ downgrade if wanted
388 : $_ eq '[args]'
389 ? [@_] # ~~~ downgrade if wanted
390 : undef;
391 }
392 # ~~~ What can we do to avoid the upgrade overhead for
393 # JS internal functions?
394 1014 return $global->upgrade($code->(@args));
395 }
396 else {
397 # If the prototype property does not exist, then, since it
398 # is undeletable, this can only be a function created with
399 # no_proto => 1, i.e., an internal functions that’s meant
400 # to die here.
401 defined(my $proto = $self->prop('prototype'))
402 or die JE::Object::Error::TypeError->new(
403 $global, add_line_number
404 103 ▶ +($$guts{func_name} || 'The function').
Branch: 1 branch
BranchTrueFalseTotal
defined(my $proto = $self->prop("prototype")) or die "JE::Object::Error::TypeError"->new($global, add_line_number(($guts->{"func_name"} || "The function") . ' cannot be called as a constructor')) 20 83 103
Condition100%2/2
$guts->{'func_name'} || 'The function'
l!l
821
MC/DC100%1/1
$guts->{'func_name'} || 'The function'
$guts->{'func_name'}
Truth table100%2/2
A
A= $guts->{'func_name'}
Aresult
11
00
405 " cannot be called as a constructor");
406  
407 20 ▶ my $obj = JE::Object->new($global,
Branch: 1 branch
BranchTrueFalseTotal
!$proto->primitive ? : 14 6 20
408 !$proto->primitive ?
409 { prototype => $proto }
410 : ()
411 );
412 20 my $return = $global->upgrade(
413 $self->apply($obj, @_)
414 );
415 20 ▶ return $return->can('primitive') && !$return->primitive
Branch: 1 branch
BranchTrueFalseTotal
$return->can('primitive') && !$return->primitive ? : 3 17 20
Condition66%2/3
$return->can("primitive") and not $return->primitive
!ll&&!rl&&r
0173
MC/DC50%1/2
$return->can("primitive") and not $return->primitive
$return->can("primitive") not $return->primitive
Truth table66%2/3
A and not(B)
A= $return->can("primitive")B= not $return->primitive
ABresult
0X0
100
111
416 ? $return
417 : $obj;
418 }
419 }
420  
421  
422  
423  
424 =item apply ( $obj, @args )
425  
426 This method, intended mainly for internal use just like the two above,
427 calls the function with $obj as the invocant and @args as the args. No
428 upgrading occurs.
429  
430 This method is very badly named and will probably be renamed in a future
431 version. Does anyone have any suggestions?
432  
433 =cut
434  
435 24000 ▶sub apply { # ~~~ we need to upgrade the args passed to apply, but still
Subroutine apply: called CC=15 SCAR=27.3
Pod documented
436 # retain the unupgraded values to pass to the function *if*
437 # the function wants them downgraded
438 24000 my ($self, $obj) = (shift, shift);
439 24000 my $guts = $$self;
440 24000 my $global = $$guts{global};
441  
442 24000 ▶ if(!blessed $obj or ref $obj eq 'JE::Object::Function::Call'
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($obj) or ref $obj eq "JE::Object::Function::Call" or ref($obj) =~ /^JE::(?:Null|Undefined)\z/) 25 23975 24000
Condition100%6/6
not builtin::blessed($obj) or ref $obj eq "JE::Object::Function::Call" or ref($obj) =~ /^JE::(?:Null|Undefined)\z/
l!l&&r!l&&!r
21423975
not builtin::blessed($obj) or ref $obj eq "JE::Object::Function::Call"
l!l&&r!l&&!r
21923979
MC/DC100%3/3
not builtin::blessed($obj) or ref $obj eq "JE::Object::Function::Call" or ref($obj) =~ /^JE::(?:Null|Undefined)\z/
not builtin::blessed($obj) ref $obj eq "JE::Object::Function::Call" ref($obj) =~ /^JE::(?:Null|Undefined)\z/
Truth table100%4/4
not(A) or B or C
A= not builtin::blessed($obj)B= ref $obj eq "JE::Object::Function::Call"C= ref($obj) =~ /^JE::(?:Null|Undefined)\z/
ABCresult
1XX1
01X1
0011
0000
443 or ref($obj) =~ /^JE::(?:Null|Undefined)\z/) {
444 25 $obj = $global;
445 }
446  
447 24000 ▶ if(ref $$guts{function} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $guts->{'function'} eq 'CODE') { } 22124 1876 24000
elsif ($guts->{'function'}) { } 1866 10 1876
448 22124 my @args;
449 22124 for( @{ $$guts{func_args} } ) {
450 push @args,
451 $_ eq 'self'
452 ? $self
453 : $_ eq 'scope'
454 ? _init_scope($self, $$guts{scope},
455 27038 ▶ $$guts{func_argnames}, @_)
Branch: 6 branches
BranchTrueFalseTotal
$_ eq 'self' ? : 0 27038 27038
$_ eq 'scope' ? : 3530 23508 27038
$_ eq 'global' ? : 29 23479 23508
$_ eq 'this' ? : 4483 18996 23479
$_ eq 'args' ? : 18996 0 18996
$_ eq '[args]' ? : 0 0 0
456 : $_ eq 'global'
457 ? $global
458 : $_ eq 'this'
459 ? $obj
460 : $_ eq 'args'
461 ? @_ # ~~~ downgrade if wanted
462 : $_ eq '[args]'
463 ? [@_] # ~~~ downgrade if wanted
464 : undef;
465 }
466 return $global->upgrade(
467 # This list slice is necessary to work around a bug
468 # in perl5.8.8 (but not in 5.8.6 or 5.10). Try
469 # running this code to see what I mean:
470 #
471 # bless ($foo=[]); sub bar{print "ok\n"}
472 # $foo->bar(sub{warn;return "anything"}->())
473 #
474 22124 (scalar $$guts{function}->(@args))[0]
475 );
476 }
477 elsif ($$guts{function}) {
478 1866 my $at = $@;
479 my $scope = _init_scope(
480 $self, $$guts{scope},
481 $$guts{func_argnames}, @_
482 1866 );
483 1866 my $time_bomb = bless [$self, $self->prop('arguments')],
484 'JE::Object::Function::_arg_wiper';
485 1866 $self->prop('arguments', $$scope[-1]{-arguments});
486 my $ret = $$guts{function}->execute(
487 1866 $obj->to_object, $scope, 2
488 );
489 1866 ▶ defined $ret or die;
Branch: 1 branch
BranchTrueFalseTotal
defined $ret or die 1862 4 1866
490 1862 $@ = $at;
491 1862 return $ret;
492 }
493 else {
494 0 ▶if (!defined $global) { use Carp; Carp::cluck() }
Subroutine BEGIN: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $global) 0 10 10
495 10 return $global->undefined;
496 }
497 }
498  
499 1866 ▶sub JE::Object::Function::_arg_wiper::DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
500 1866 $_[0][0] # function
501 ->prop(
502 'arguments', $_[0][1] # old arguments value
503 )
504 }
505  
506 6347 ▶sub _init_scope { # initialise the new scope for the function call
Subroutine _init_scope: called CC=1 SCAR=0.0
507 6347 my($self, $scope, $argnames, @args) = @_;
508  
509 bless([ @$scope, JE::Object::Function::Call->new({
510 global => $$$self{global},
511 6347 argnames => $argnames,
512 args => [@args],
513 function => $self,
514 })], 'JE::Scope');
515 }
516  
517  
518  
519  
520 =item typeof
521  
522 This returns the string 'function'.
523  
524 =cut
525  
526 325 ▶sub typeof { 'function' }
Subroutine typeof: called CC=1 SCAR=0.0
Pod documented
527  
528  
529  
530  
531 =item class
532  
533 This returns the string 'Function'.
534  
535 =cut
536  
537 121 ▶sub class { 'Function' }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
538  
539  
540  
541  
542 =item value
543  
544 Not yet implemented.
545  
546 =cut
547  
548 0 ▶sub value { die "JE::Object::Function::value is not yet implemented." }
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
549  
550  
551 #----------- PRIVATE SUBROUTINES ---------------#
552  
553 # _init_proto takes the Function prototype (Function.prototype) as its sole
554 # arg and adds all the default properties thereto.
555  
556 107 ▶sub _init_proto {
Subroutine _init_proto: called CC=1 SCAR=0.0
557 107 my $proto = shift;
558 107 my $scope = $$proto->{global};
559  
560 # E 15.3.4
561 107 $proto->prop({
562 dontenum => 1,
563 name => 'constructor',
564 value => $scope->prop('Function'),
565 });
566  
567 $proto->prop({
568 name => 'toString',
569 value => JE::Object::Function->new({
570 scope => $scope,
571 name => 'toString',
572 no_proto => 1,
573 function_args => ['this'],
574 38 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
575 38 my $self = shift;
576 38 ▶ $self->isa(__PACKAGE__) or die new
Branch: 1 branch
BranchTrueFalseTotal
$self->isa(__PACKAGE__) or die "JE::Object::Error::TypeError"->new($scope, add_line_number("Function.prototype.toString can only be called on functions")) 37 1 38
577 JE::Object::Error::TypeError
578 $scope, add_line_number "Function."
579 ."prototype.toString can only be "
580 ."called on functions";
581 37 my $guts = $$self;
582 37 my $str = 'function ';
583 JE::String->_new($scope,
584 'function ' .
585 ( exists $$guts{func_name} ?
586 $$guts{func_name} :
587 'anon'.$self->id) .
588 '(' .
589 37 ▶ join(',', @{$$guts{func_argnames}})
Branch: 1 branch
BranchTrueFalseTotal
ref $guts->{'function'} eq 'JE::Code' ? : 27 10 37
590 . ") {" .
591 ( ref $$guts{function}
592 eq 'JE::Code'
593 37 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
exists $guts->{'func_name'} ? : 24 13 37
594 my $code =
595 27 $$guts{function};
596 my $offsets =
597 $$guts{function}
598 27 {tree}[0];
599 27 $code = substr ${$$code{source}},
600 $$offsets[0],
601 $$offsets[1] -
602 $$offsets[0];
603 # We have to check for a final line
604 # break in case it ends with a sin-
605 # gle-line comment.
606 27 ▶ $code =~ /[\cm\cj\x{2028}\x{2029}]\z/
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /[\cm\cj\x{2028}\x{2029}]\z/ ? : 0 27 27
607 ? $code : $code . "\n"
608 }
609 : "\n // [native code]\n"
610 ) . '}'
611 # ~~~ perhaps this should be changed so it doesn't comment out the
612 # the [native code] thingy. That way an attempt to
613 # eval the strung version will fail. (In this case, I need to add a
614 # teest too make sure it dies.)
615 );
616 },
617 107 }),
618 dontenum => 1,
619 });
620 $proto->prop({
621 name => 'apply',
622 value => JE::Object::Function->new({
623 scope => $scope,
624 name => 'apply',
625 argnames => [qw/thisArg argArray/],
626 no_proto => 1,
627 function_args => ['this','args'],
628 178 ▶ function => sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
629 178 my($self,$obj,$args) = @_;
630  
631 178 my $at = $@;
632  
633 101 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
634 178 ▶ if(defined $args and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args and not ref($args) =~ /^JE::(Null|Undefined| Object::Function::Arguments)\z/x and eval { do { $args->class } } ne "Array") 4 174 178
Condition100%6/6
defined $args and not ref($args) =~ /^JE::(Null|Undefined| Object::Function::Arguments)\z/x and eval { do { $args->class } } ne "Array"
!ll&&!rl&&r
17314
defined $args and not ref($args) =~ /^JE::(Null|Undefined| Object::Function::Arguments)\z/x
!ll&&!rl&&r
17035
MC/DC100%3/3
defined $args and not ref($args) =~ /^JE::(Null|Undefined| Object::Function::Arguments)\z/x and eval { do { $args->class } } ne "Array"
defined $args not ref($args) =~ /^JE::(Null|Undefined| Object::Function::Arguments)\z/x eval { do { $args->class } } ne "Array"
Truth table100%4/4
A and not(B) and C
A= defined $argsB= not ref($args) =~ /^JE::(Null|Undefined| Object::Function::Arguments)\z/xC= eval { do { $args->class } } ne "Array"
ABCresult
0XX0
10X0
1100
1111
635 ref($args) !~ /^JE::(Null|Undefined|
636 Object::Function::Arguments)\z/x
637 5 and eval{$args->class} ne 'Array') {
638 die JE::Object::Error::TypeError
639 ->new($scope, add_line_number
640 "Second argument to "
641 . "'apply' is of type '" .
642 4 (eval{$args->class} ||
643 0 ▶ eval{$args->typeof} ||
Condition33%2/6
eval { do { $args->class } } || eval { do { $args->typeof } } || ref $args
l!l&&r!l&&!r
400
eval { do { $args->class } } or eval { do { $args->typeof } }
l!l&&r!l&&!r
400
MC/DC0%0/3
eval { do { $args->class } } || eval { do { $args->typeof } } || ref $args
eval { do { $args->class } } eval { do { $args->typeof } } ref $args
Truth table25%1/4
A or B || C
A= eval { do { $args->class } }B= eval { do { $args->typeof } }C= ref $args
ABCresult
1XX1
01X1
0011
0000
644 ref $args) .
645 "', not 'Arguments' or " .
646 "'Array'");
647 }
648 174 $@ = $at;
649 174 ▶ $args = $args->value if defined $args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 4 170 174
650 174 ▶ $self->apply($obj, defined $args ?
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 2 172 174
651 @$args : ());
652 },
653 107 }),
654 dontenum => 1,
655 });
656 $proto->prop({
657 name => 'call',
658 value => JE::Object::Function->new({
659 scope => $scope,
660 name => 'call',
661 argnames => ['thisArg'],
662 no_proto => 1,
663 function_args => ['this','args'],
664 104 ▶ function => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
665 104 shift->apply(@_);
666 },
667 107 }),
668 dontenum => 1,
669 });
670 }
671  
672  
673 #----------- THE REST OF THE DOCUMENTATION ---------------#
674  
675 =back
676  
677 =head1 OVERLOADING
678  
679 You can use a JE::Object::Function as a coderef. The sub returned simply
680 invokes the C<call> method, so the following are equivalent:
681  
682 $function->call( $function->global->upgrade(@args) )
683 $function->(@args)
684  
685 The stringification, numification, boolification, and hash dereference ops
686 are also overloaded. See L<JE::Object>, which this class inherits from.
687  
688 =head1 SEE ALSO
689  
690 =over 4
691  
692 =item JE
693  
694 =item JE::Object
695  
696 =item JE::Types
697  
698 =item JE::Scope
699  
700 =item JE::LValue
701  
702 =back
703  
704 =cut
705  
706  
707 package JE::Object::Function::Call;
708  
709 101 our $VERSION = '0.066';
710  
711 6347 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
712 # See sub JE::Object::Function::_init_sub for the usage.
713  
714 6347 my($class,$opts) = @_;
715 6347 my @args = @{$$opts{args}};
716 6347 my(%self,$arg_val);
717 6347 for(@{$$opts{argnames}}){
718 8106 $arg_val = shift @args;
719 8106 $self{-dontdel}{$_} = 1;
720 $self{$_} = defined $arg_val ? $arg_val :
721 8106 ▶ $$opts{global}->undefined;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg_val ? : 8012 94 8106
722 }
723  
724 6347 $self{-dontdel}{arguments} = 1;
725  
726 6347 $self{'-global'} = $$opts{global};
727 # A call object's properties can never be accessed via bracket
728 # syntax, so '-global' cannot conflict with properties, since the
729 # latter have to be valid identifiers. Same 'pplies to dontdel, o'
730 # course.
731
732 # Note on arguments vs -arguments: ‘arguments’ represents the
733 # actual ‘arguments’ property, which may or may not refer to the
734 # Arguments object, depending on whether there is an argument
735 # named ‘arguments’. ‘-arguments’ always refers to the Arguments
736 # object, which we need further up when we assign to the arguments
737 # property of the function itself.
738  
739 $self{-arguments} =
740 JE::Object::Function::Arguments->new(
741 $$opts{global},
742 $$opts{function},
743 \%self,
744 $$opts{argnames},
745 6347 @{$$opts{args}},
746 );
747 6347 ▶ unless (exists $self{arguments}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self{"arguments"}) 6346 1 6347
748 $self{arguments} = $self{-arguments}
749 6346 };
750  
751 6347 return bless \%self, $class;
752 }
753  
754 9212 ▶sub prop {
Subroutine prop: called CC=6 SCAR=18.0
755 9212 my ($self, $name) =(shift,shift);
756  
757 9212 ▶ if(ref $name eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name eq "HASH") 272 8940 9212
758 272 my $opts = $name;
759 272 $name = $$opts{name};
760 272 ▶ @_ = exists($$opts{value}) ? $$opts{value} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'value'} ? : 0 272 272
761 $$self{'-dontdel'}{$name} = !!$$opts{dontdel}
762 272 ▶ if exists $$opts{dontdel};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"dontdel"} 272 0 272
763 }
764  
765 9212 ▶ if (@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 836 8376 9212
766 836 return $$self{$name} = shift;
767 }
768  
769 8376 ▶ if (exists $$self{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$name}) 8062 314 8376
770 8062 return $$self{$name};
771 }
772  
773 return
774 314 }
775  
776 92 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
777 92 my ($self,$varname) = @_;
778 92 ▶ unless($_[2]) { # if $_[2] is true we delete it anyway
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[2]) 39 53 92
779 exists $$self{-dontdel}{$varname}
780 39 ▶ && $$self{-dontdel}{$varname}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"-dontdel"}{$varname} and $self->{"-dontdel"}{$varname} and 4 35 39
Condition66%2/3
exists $self->{"-dontdel"}{$varname} and $self->{"-dontdel"}{$varname}
!ll&&!rl&&r
3504
MC/DC50%1/2
exists $self->{"-dontdel"}{$varname} and $self->{"-dontdel"}{$varname}
exists $self->{"-dontdel"}{$varname} $self->{"-dontdel"}{$varname}
Truth table66%2/3
A and B
A= exists $self->{"-dontdel"}{$varname}B= $self->{"-dontdel"}{$varname}
ABresult
0X0
100
111
781 && return !1;
782 }
783 88 delete $$self{-dontdel}{$varname};
784 88 delete $$self{$varname};
785 88 return 1;
786 }
787  
788 8495 ▶sub exists { exists $_[0]{$_[1]} }
Subroutine exists: called CC=1 SCAR=0.0
789 1735 ▶sub prototype{}
Subroutine prototype: called CC=1 SCAR=0.0
790  
791  
792  
793  
794 package JE::Object::Function::Arguments;
795  
796 101 our $VERSION = '0.066';
797  
798 101 our @ISA = 'JE::Object';
799  
800 6347 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
801 6347 my($class,$global,$function,$call,$argnames,@args) = @_;
802
803 6347 my $self = $class->SUPER::new($global);
804 6347 my $guts = $$self;
805  
806 6347 $$guts{args_call} = $call;
807 6347 $self->prop({
808 name => 'callee',
809 value => $function,
810 dontenum => 1,
811 });
812 6347 $self->prop({
813 name => 'length',
814 value => JE::Number->new($global, scalar @args),
815 dontenum => 1,
816 });
817 6347 $$guts{args_length} = @args; # in case the length prop
818 # gets changed
819  
820 =begin pseudocode
821  
822 Go through the named args one by one in reverse order, starting from $#args
823 if $#args < $#params
824  
825 If an arg with the same name as the current one has been seen
826 Create a regular numbered property for that arg.
827 Else
828 Create a magical property.
829  
830 =end pseudocode
831  
832 =cut
833  
834 6347 my (%seen,$name,$val);
835 6347 for (reverse 0..($#args,$#$argnames)[$#$argnames < $#args]) {
836 8012 ($name,$val) = ($$argnames[$_], $args[$_]);
837 8012 ▶ if($seen{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$name}++) { } 2 8010 8012
838 2 $self->prop({
839 name => $_,
840 value => $val,
841 dontenum => 1,
842 });
843 }
844 else {
845 8010 $$guts{args_magic}{$_} = $name;
846 }
847 }
848  
849 # deal with any extra properties
850 6347 for (@$argnames..$#args) {
851 1399 $self->prop({
852 name => $_,
853 value => $args[$_],
854 dontenum => 1,
855 });
856 }
857  
858 6347 $self;
859 }
860  
861 14184 ▶sub prop {
Subroutine prop: called CC=3 SCAR=11.0
862 # Some properties are magically linked to properties of
863 # the call object.
864  
865 14184 my($self,$name) = @_;
866 14184 my $guts = $$self;
867 14184 ▶ if (exists $$guts{args_magic} and exists $$guts{args_magic}{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $guts->{"args_magic"} and exists $guts->{"args_magic"}{$name}) 13 14171 14184
Condition100%3/3
exists $guts->{"args_magic"} and exists $guts->{"args_magic"}{$name}
!ll&&!rl&&r
141462513
MC/DC100%2/2
exists $guts->{"args_magic"} and exists $guts->{"args_magic"}{$name}
exists $guts->{"args_magic"} exists $guts->{"args_magic"}{$name}
Truth table100%3/3
A and B
A= exists $guts->{"args_magic"}B= exists $guts->{"args_magic"}{$name}
ABresult
0X0
100
111
868 {
869 return $$guts{args_call}->prop(
870 13 $$guts{args_magic}{$name}, @_[2..$#_]
871 );
872 }
873 14171 SUPER::prop $self @_[1..$#_];
874 }
875  
876 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
877 # Magical properties are still deleteable.
878 0 my($self,$name) = @_;
879 0 my $guts = $$self;
880 0 ▶ if (exists $$guts{args_magic} and exists $$guts{args_magic}{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $guts->{"args_magic"} and exists $guts->{"args_magic"}{$name}) 0 0 0
Condition0%0/3
exists $guts->{"args_magic"} and exists $guts->{"args_magic"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $guts->{"args_magic"} and exists $guts->{"args_magic"}{$name}
exists $guts->{"args_magic"} exists $guts->{"args_magic"}{$name}
Truth table0%0/3
A and B
A= exists $guts->{"args_magic"}B= exists $guts->{"args_magic"}{$name}
ABresult
0X0
100
111
881 {
882 0 delete $$guts{args_magic}{$name}
883 }
884 0 SUPER::delete $self @_[1..$#_];
885 }
886  
887 1 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
888 1 my $self = shift;
889 1 [ map $self->prop($_), 0..$$$self{args_length}-1 ];
890 }
891  
892 101 1;
« JE/Object/Number/maxvalue.pl ↑ Summary JE/Destroyer.pm »