JE/Object.pm

filter: Statementstmt 94.9% 208 / 219 Branchbran 90.0% 99 / 110 Conditioncond 80.3% 41 / 51 MC/DCmcdc 60.7% 17 / 28 Subroutinesub 93.8% 46 / 49 Podpod 30.7% 8 / 26 totaltotal 86.7% 419 / 483 CC 76 SCAR 23.1
CC
76
Cov
92%
prop
29.7
new
10.1
STORE
9.2
CRAP 10.1
« JE/Object/Proxy.pm ↑ Summary JE/Object/Number/maxvalue.pl »
1 package JE::Object;
2  
3 # This has to come before any pragmas and sub declarations.
4 190 ▶sub evall { my $global = shift; my $r = eval 'local *_;' . shift;
Subroutine evall: called CC=2 SCAR=7.0
Pod undocumented
5 190 ▶ $@ and die; $r }
Branch: 1 branch
BranchTrueFalseTotal
$@ and die 0 190 190
6  
7 101 our $VERSION = '0.066';
8  
9 101 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 101 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 101 ▶use overload fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
13 '%{}'=> \&_get_tie,
14 '""' => 'to_string',
15 '0+' => 'to_number',
16 # cmp => sub { "$_[0]" cmp $_[1] },
17 101 ▶ bool => sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 101 ▶use Scalar::Util qw'refaddr blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 101 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 101 ▶use B 'svref_2object';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 #use Data::Dumper;
23  
24  
25 101 require JE::Code;
26 101 require JE::Object::Error::TypeError;
27 101 require JE::Object::Function;
28 101 require JE::Boolean;
29 101 require JE::String;
30  
31 101 import JE::Code 'add_line_number';
32 sub add_line_number;
33  
34 231 ▶sub in_list {
Subroutine in_list: called CC=3 SCAR=11.0
Pod undocumented
35 231 my $str = shift;
36 231 ▶ shift eq $str and return 1 while @_;
Branch: 2 branches
BranchTrueFalseTotal
@_ and do { shift() eq $str and return 1 } 458 219 677
shift() eq $str and return 1 12 446 458
37 219 !1;
38 }
39  
40  
41 =head1 NAME
42  
43 JE::Object - Base class for all JavaScript objects
44  
45 =head1 SYNOPSIS
46  
47 use JE;
48 use JE::Object;
49  
50 $j = new JE;
51  
52 $obj = new JE::Object $j;
53  
54 $obj->prop('property1', $new_value); # sets the property
55 $obj->prop('property1'); # returns $new_value;
56 $obj->{property1} = $new_value; # or use it as a hash
57 $obj->{property1}; # ref like this
58  
59 $obj->keys; # returns a list of the names of enumerable property
60 keys %$obj;
61  
62 $obj->delete('property_name');
63 delete $obj->{property_name};
64  
65 $obj->method('method_name', 'arg1', 'arg2');
66 # calls a method with the given arguments
67  
68 $obj->value ; # returns a value useful in Perl (a hashref)
69  
70 "$obj"; # "[object Object]" -- same as $obj->to_string->value
71 0+$obj"; # nan -- same as $obj->to_number->value
72 # etc.
73  
74 =head1 DESCRIPTION
75  
76 This module implements JavaScript objects for JE. It serves as a base
77 class
78 for all other JavaScript objects.
79  
80 A JavaScript object is an associative array, the elements of which are
81 its properties. A method is a property that happens to be an instance
82 of the
83 C<Function> class (C<JE::Object::Function>).
84  
85 JE::Object objects can be used in Perl as a number, string or boolean. The
86 result will be the same as in JavaScript. The C<%{}> (hashref) operator is
87 also overloaded and returns a hash that can be used to modify the object.
88 See L<"USING AN OBJECT AS A HASH">.
89  
90 See also L<JE::Types> for descriptions of most of the methods. Only what
91 is specific to JE::Object is explained here.
92  
93 =head1 METHODS
94  
95 =over 4
96  
97 =item $obj = JE::Object->new( $global_obj )
98  
99 =item $obj = JE::Object->new( $global_obj, $value )
100  
101 =item $obj = JE::Object->new( $global_obj, \%options )
102  
103 This class method constructs and returns a new JavaScript object, unless
104 C<$value> is
105 already a JS object, in which case it just returns it. The behaviour is
106 the
107 same as the C<Object> constructor in JavaScript.
108  
109 The C<%options> are as follows:
110  
111 prototype the object to be used as the prototype for this
112 object (Object.prototype is the default)
113 value the value to be turned into an object
114  
115 C<prototype> only applies when C<value> is omitted, undef, undefined
116 or null.
117  
118 To convert a hash into an object, you can use the hash ref syntax like
119 this:
120  
121 new JE::Object $j, { value => \%hash }
122  
123 Though it may be easier to write:
124  
125 $j->upgrade(\%hash)
126  
127 The former is what C<upgrade> itself uses.
128  
129 =cut
130  
131 # ~~~ Perhaps I should eliminate the hash ref syntax and have new()
132 # check to see if $j->exists($class->class), and use that as the
133 # prototype. That would make the other constructors simpler, but would
134 # it make it harder to control JE and customise host objects?
135  
136 19499 ▶sub new {
Subroutine new: called CC=10 SCAR=23.2
Pod documented
137 19499 my($class, $global, $value) = @_;
138  
139 19499 ▶ if (defined blessed $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($value) and $value->can("to_object")) 9 19490 19499
Condition100%3/3
defined builtin::blessed($value) and $value->can("to_object")
!ll&&!rl&&r
1948649
MC/DC100%2/2
defined builtin::blessed($value) and $value->can("to_object")
defined builtin::blessed($value) $value->can("to_object")
Truth table100%3/3
A and B
A= defined builtin::blessed($value)B= $value->can("to_object")
ABresult
0X0
100
111
140 and can $value 'to_object') {
141 9 return to_object $value;
142 }
143
144 19490 my $p;
145 my %hash;
146 19490 my %opts;
147  
148 19490 ▶ ref $value eq 'HASH' and (%opts = %$value), $value = $opts{value};
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "HASH" and %opts = %$value, $value = $opts{"value"} 10981 8509 19490
149
150 19490 local $@;
151 19490 ▶ if (!defined $value || !defined eval{$value->value} && $@ eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value or not defined eval { do { $value->value } } and $@ eq '') { } 19487 3 19490
elsif (ref $value eq 'HASH') { } 3 0 3
Condition83%5/6
not defined $value or not defined eval { do { $value->value } } and $@ eq ""
l!l&&r!l&&!r
1948343
not defined eval { do { $value->value } } and $@ eq ""
!ll&&!rl&&r
034
MC/DC66%2/3
not defined $value or not defined eval { do { $value->value } } and $@ eq ""
not defined $value not defined eval { do { $value->value } } $@ eq ""
Truth table75%3/4
not(A) or not(B) and C
A= not defined $valueB= not defined eval { do { $value->value } }C= $@ eq ""
ABCresult
1XX1
0111
00X0
0100
152 $p = exists $opts{prototype} ? $opts{prototype}
153 19487 ▶ : $global->prototype_for("Object");
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'prototype'} ? : 10978 8509 19487
154 }
155 elsif(ref $value eq 'HASH') {
156 3 %hash = %$value;
157 3 $p = $global->prototype_for("Object");
158 }
159 else {
160 0 return $global->upgrade($value);
161 }
162  
163 19490 my $self =
164 bless \{ prototype => $p,
165 global => $global,
166 props => \%hash,
167 keys => [keys %hash] }, $class;
168  
169 19490 ▶ $JE::Destroyer && JE::Destroyer'register($self);
Branch: 1 branch
BranchTrueFalseTotal
$JE::Destroyer and JE::Destroyer::register($self) 0 19490 19490
170  
171 19490 $self;
172 }
173  
174 0 ▶sub destroy { # not DESTROY; called by JE::Destroyer
Subroutine destroy: not called CC=1 SCAR=6.9
Pod undocumented
175 0 undef ${$_[0]};
176 }
177  
178  
179 =item $obj->new_function($name, sub { ... })
180  
181 =item $obj->new_function(sub { ... })
182  
183 This creates and returns a new function object. If $name is given,
184 it will become a property of the object. The function is enumerable, like
185 C<alert> I<et al.> in web browsers.
186  
187 For more ways to create functions, see L<JE::Object::Function>.
188  
189 =cut
190  
191 709 ▶sub new_function {
Subroutine new_function: called CC=3 SCAR=11.4
Pod documented
192 709 my $self = shift;
193 709 ▶ my $f = JE::Object::Function->new({
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 709 0 709
194 scope => $self->global,
195 function => pop,
196 function_args => ['args'],
197 @_ ? (name => $_[0]) : ()
198 });
199 709 ▶ @_ and $self->prop({
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->prop({"name", shift(), "value", $f}) 709 0 709
200 name => shift,
201 value=>$f,
202 });
203 709 $f;
204 }
205  
206  
207  
208  
209 =item $obj->new_method($name, sub { ... })
210  
211 =item $obj->new_method(sub { ... })
212  
213 This is the same as C<new_function>, except that the subroutine's first
214 argument will be the object with which the function is called, and that the
215 property created will not be enumerable. This allows one to add methods to
216 C<Object.prototype>, for instance, without making every for-in loop list
217 that method.
218  
219 For more ways to create functions, see L<JE::Object::Function>.
220  
221 =cut
222  
223 28 ▶sub new_method {
Subroutine new_method: called CC=3 SCAR=11.4
Pod documented
224 28 my $self = shift;
225 28 ▶ my $f = JE::Object::Function->new({
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 28 0 28
226 scope => $self->global,
227 function => pop,
228 function_args => ['this','args'],
229 @_ ? (name => $_[0]) : ()
230 });
231 28 ▶ @_ and $self->prop({
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->prop({"name", shift(), "value", $f, "dontenum", 1}) 28 0 28
232 name => shift,
233 value=>$f,
234 dontenum=>1
235 });
236 28 $f;
237 }
238  
239 =item $obj->prop( $name )
240  
241 =item $obj->prop( $name => $value )
242  
243 =item $obj->prop({ ... })
244  
245 See C<JE::Types> for the first two uses.
246  
247 When the C<prop> method is called with a hash ref as its argument, the
248 prototype chain is I<not> searched.
249 The elements of the hash are as follows:
250  
251 name property name
252 value new value
253 dontenum whether this property is unenumerable
254 dontdel whether this property is undeletable
255 readonly whether this property is read-only
256 fetch subroutine called when the property is fetched
257 store subroutine called when the property is set
258 autoload see below
259  
260 If C<dontenum>, C<dontdel> or C<readonly> is given, the attribute in
261 question will be set.
262 If C<value> is given, the value of the property will be set, regardless of
263 the attributes.
264  
265 C<fetch> and C<store>, if specified, must be subroutines for
266 fetching/setting the value of the property. The 'fetch' subroutine will be
267 called with ($object, $storage_space) as the arguments, where
268 C<$storage_space> is a hash key inside the object that the two subroutines
269 can use for storing the value (they can ignore it if they like). The
270 'store' subroutine will be call with
271 ($object, $new_value, $storage_space) as
272 the arguments. Values assigned to the storage space from within these
273 routines are I<not>
274 upgraded, neither is the return value of C<fetch>. C<fetch> and C<store> do
275 not necessarily have to go
276 together. If you only specify C<fetch>, then the value will be set as
277 usual, but C<fetch> will be able to mangle the value when it is retrieved.
278 Likewise, if you only specify C<store>, the value will be retrieved the
279 usual way, so you can use this for validating or normalising the assigned
280 value, for
281 instance. B<Note:> Currently, a simple scalar or unblessed coderef in the
282 storage space will cause autoloading, but that is subject to change.
283  
284 C<autoload> can be a string or a coderef. It will be called/evalled the
285 first time the property is accessed (accessing it with a hash ref as
286 described here does not count). If it is a string, it will be
287 evaluated in the calling package (see warning below), in a scope that has a
288 variable named
289 C<$global> that refers to the global object. The result will become the
290 property's value. The value returned is not currently upgraded. The behaviour when a simple scalar or unblessed reference is returned is
291 undefined. C<autoload> will be
292 ignored completely if C<value> or C<fetch> is also given. B<Warning:> The
293 'calling package' may not be what you think it is if a subclass overrides
294 C<prop>. It may be the subclass in such cases. To be on the safe side,
295 always begin the string of code with an explicit C<package> statement. (If
296 anyone knows of a clean solution to this, please let the author know.)
297  
298 This hash ref calling convention does not work on Array
299 objects when the property name is C<length> or an array index (a
300 non-negative integer
301 below
302 4294967295). It does not work on String objects if the
303 property name is C<length>.
304  
305 =cut
306  
307 164167 ▶sub prop {
Subroutine prop: called CC=29 SCAR=33.9
Pod documented
308 164167 my ($self, $opts) = (shift, shift);
309 164167 my $guts = $$self;
310  
311 164167 ▶ if(ref $opts eq 'HASH') { # special use
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts eq 'HASH') { } 34219 129948 164167
312 34219 my $name = $$opts{name};
313 34219 for (qw< dontdel readonly >) {
314 exists $$opts{$_}
315 68438 ▶ and $$guts{"prop_$_"}{$name} = $$opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{$_} and $$guts{"prop_$_"}{$name} = $opts->{$_} 17745 50693 68438
316 }
317  
318 34219 my $props = $$guts{props};
319  
320 34219 my $dontenum;
321 34219 ▶ if(exists $$opts{dontenum}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts->{'dontenum'}) { } 27285 6934 34219
elsif (not exists $props->{$name}) { } 6565 369 6934
322 27285 ▶ if($$opts{dontenum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'dontenum'}) { } 27285 0 27285
323 27285 @{$$guts{keys}} =
324 27285 grep $_ ne $name, @{$$guts{keys}};
325 }
326 else {
327 0 push @{ $$guts{keys} }, $name
328 0 ▶ unless first {$_ eq $name} @{$$guts{keys}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $name; } , @{$$guts{"keys"};}) 0 0 0
329 }
330 }
331 elsif(!exists $$props{$name}) { # new property
332 6565 push @{ $$guts{keys} }, $name
333 }
334  
335 34219 ▶ if(exists $$opts{fetch}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"fetch"}) 168 34051 34219
336 168 $$guts{fetch_handler}{$name} = $$opts{fetch};
337 168 ▶ $$props{$name} = undef if !exists $$props{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $props->{$name} 168 0 168
338 }
339 34219 ▶ if(exists $$opts{store}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"store"}) 137 34082 34219
340 137 $$guts{store_handler}{$name} = $$opts{store};
341 137 ▶ $$props{$name} = undef if !exists $$props{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $props->{$name} 4 133 137
342 }
343 34219 ▶ if(exists $$opts{value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts->{'value'}) { } 31433 2786 34219
elsif (not exists $opts->{'fetch'} and exists $opts->{'autoload'}) { } 2070 883 2953
344 31433 return $$props{$name} = $$opts{value};
345 }
346 ▶ elsif(!exists $$opts{fetch} && exists $$opts{autoload}) {
Condition100%3/3
not exists $opts->{"fetch"} and exists $opts->{"autoload"}
!ll&&!rl&&r
1675492070
MC/DC100%2/2
not exists $opts->{"fetch"} and exists $opts->{"autoload"}
not exists $opts->{"fetch"} exists $opts->{"autoload"}
Truth table100%3/3
not(A) and B
A= not exists $opts->{"fetch"}B= exists $opts->{"autoload"}
ABresult
0X0
100
111
347 2070 my $auto = $$opts{autoload};
348 2070 ▶ $$props{$name} = ref $auto eq 'CODE' ? $auto :
Branch: 1 branch
BranchTrueFalseTotal
ref $auto eq 'CODE' ? : 1 2069 2070
349 "package " . caller() . "; $auto";
350 return # ~~~ Figure out what this should
351 # return, if anything
352 2070 }
353  
354 # ~~~ what should we return if fetch is given,
355 # but not value?
356  
357 return exists $$opts{fetch} ? () :
358 716 ▶ exists $$props{$name} ? $$props{$name} : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $opts->{'fetch'} ? : 167 549 716
exists $props->{$name} ? : 546 3 549
359 }
360  
361 else { # normal use
362 129948 my $name = $opts;
363 129948 my $props = $$guts{props};
364 129948 ▶ if (@_) { # we is doing a assignment
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 23334 106614 129948
elsif (exists $props->{$name}) { } 98998 7616 106614
365 23334 my($new_val) = shift;
366  
367 23334 ▶ return $new_val if $self->is_readonly($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_readonly($name) 122 23212 23334
368  
369 # Make sure we don't change attributes if the
370 # property already exists
371 my $exists = exists $$props{$name} &&
372 23212 ▶ defined $$props{$name};
Condition100%3/3
exists $props->{$name} && defined $props->{$name}
!ll&&!rl&&r
20722621114
MC/DC100%2/2
exists $props->{$name} && defined $props->{$name}
exists $props->{$name} defined $props->{$name}
Truth table100%3/3
A && B
A= exists $props->{$name}B= defined $props->{$name}
ABresult
0X0
100
111
373  
374 exists $$guts{store_handler}{$name}
375 ? $$guts{store_handler}{$name}->(
376 $self, $new_val, $$props{$name})
377 23212 ▶ : ($$props{$name} = $new_val);
Branch: 1 branch
BranchTrueFalseTotal
exists $guts->{'store_handler'}{$name} ? : 29 23183 23212
378  
379 23212 ▶ push @{ $$guts{keys} }, $name
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 2098 21114 23212
380 unless $exists;
381  
382 23212 return $new_val;
383 }
384 elsif (exists $$props{$name}) {
385 98998 ▶ if(exists $$guts{fetch_handler}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $guts->{"fetch_handler"}{$name}) 88 98910 98998
386 return $$guts{fetch_handler}{$name}-> (
387 88 $self, $$props{$name}
388 );
389 }
390  
391 98910 my $val = $$props{$name};
392 ref $val eq 'CODE' ?
393 $val = $$props{$name} = &$val() :
394 defined $val && ref $val eq '' &&
395 ($val = $$props{$name} =
396 98910 ▶ evall $$guts{global}, $val
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'CODE' ? : 2 98908 98910
Condition66%4/6
defined $val and ref $val eq "" and $val = ($props->{$name} = evall($guts->{"global"}, $val))
!ll
987181900
defined $val and ref $val eq ""
!ll&&!rl&&r
098718190
MC/DC0%0/3
defined $val and ref $val eq "" and $val = ($props->{$name} = evall($guts->{"global"}, $val))
defined $val ref $val eq "" $val = ($props->{$name} = evall($guts->{"global"}, $val))
Truth table0%0/4
A and B and C
A= defined $valB= ref $val eq ""C= $val = ($props->{$name} = evall($guts->{"global"}, $val))
ABCresult
0XX0
10X0
1100
1111
397 );
398 98910 return $val;
399 }
400 else {
401 7616 my $proto = $self->prototype;
402 7616 ▶ return $proto ?
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 6233 1383 7616
403 $proto->prop($name) :
404 undef;
405 }
406 }
407  
408 }
409  
410  
411 99820 ▶sub exists { # = hasOwnProperty
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
412 99820 my($self,$name) = @_;
413 99820 return exists $$$self{props}{$name}
414 }
415  
416  
417 26447 ▶sub is_readonly { # See JE::Types for a description of this.
Subroutine is_readonly: called CC=4 SCAR=13.9
Pod undocumented
418 26447 my ($self,$name) = (shift,@_); # leave $name in @_
419  
420 26447 my $guts = $$self;
421  
422 26447 my $props = $$guts{props};
423 26447 ▶ if( exists $$props{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $props->{$name}) 21290 5157 26447
424 21290 my $read_only_list = $$guts{prop_readonly};
425 return exists $$read_only_list{$name} ?
426 21290 ▶ $$read_only_list{$name} : !1;
Branch: 1 branch
BranchTrueFalseTotal
exists $read_only_list->{$name} ? : 127 21163 21290
427 }
428  
429 5157 ▶ if(my $proto = $self->prototype) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proto = $self->prototype) 3095 2062 5157
430 3095 return $proto->is_readonly(@_);
431 }
432  
433 2062 return !1;
434 }
435  
436  
437  
438  
439 231 ▶sub is_enum {
Subroutine is_enum: called CC=1 SCAR=0.0
Pod undocumented
440 231 my ($self, $name) = @_;
441 231 $self = $$self;
442 231 in_list $name, @{ $$self{keys} };
443 }
444  
445  
446  
447  
448 255 ▶sub keys {
Subroutine keys: called CC=2 SCAR=6.9
Pod undocumented
449 255 my $self = shift;
450 255 my $proto = $self->prototype;
451 255 ▶ @{ $$self->{keys} }, defined $proto ? $proto->keys : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $proto ? : 154 101 255
452 }
453  
454  
455  
456  
457 =item $obj->delete($property_name, $even_if_it's_undeletable)
458  
459 Deletes the property named $name, if it is deletable. If the property did
460 not exist or it was deletable, then
461 true is returned. If the property exists and could not be deleted, false
462 is returned.
463  
464 If the second argument is given and is true, the property will be deleted
465 even if it is marked is undeletable. A subclass may override this,
466 however.
467 For instance, Array and String objects always have a 'length' property
468 which cannot be deleted.
469  
470 =cut
471  
472 291 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
Pod documented
473 291 my ($self, $name) = @_;
474 291 my $guts = $$self;
475  
476 291 ▶ unless($_[2]) { # second arg means always delete
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[2]) 176 115 291
477 176 my $dontdel_list = $$guts{prop_dontdel};
478 176 ▶ exists $$dontdel_list{$name} and $$dontdel_list{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $dontdel_list->{$name} and $dontdel_list->{$name} and 146 30 176
Condition66%2/3
exists $dontdel_list->{$name} and $dontdel_list->{$name}
!ll&&!rl&&r
300146
MC/DC50%1/2
exists $dontdel_list->{$name} and $dontdel_list->{$name}
exists $dontdel_list->{$name} $dontdel_list->{$name}
Truth table66%2/3
A and B
A= exists $dontdel_list->{$name}B= $dontdel_list->{$name}
ABresult
0X0
100
111
479 and return !1;
480 }
481
482 145 delete $$guts{prop_dontdel }{$name};
483 145 delete $$guts{prop_dontenum}{$name};
484 145 delete $$guts{prop_readonly}{$name};
485 145 delete $$guts{props}{$name};
486 145 $$guts{keys} = [ grep $_ ne $name, @{$$guts{keys}} ];
487 145 return 1;
488 }
489  
490  
491  
492  
493 35 ▶sub method {
Subroutine method: called CC=1 SCAR=0.0
Pod undocumented
494 35 my($self,$method) = (shift,shift);
495  
496 35 $self->prop($method)->apply($self, $self->global->upgrade(@_));
497 }
498  
499 =item $obj->typeof
500  
501 This returns the string 'object'.
502  
503 =cut
504  
505 112 ▶sub typeof { 'object' }
Subroutine typeof: called CC=1 SCAR=0.0
Pod documented
506  
507  
508  
509  
510 =item $obj->class
511  
512 Returns the string 'Object'.
513  
514 =cut
515  
516 440 ▶sub class { 'Object' }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
517  
518  
519  
520  
521 =item $obj->value
522  
523 This returns a hash ref of the object's enumerable properties. This is a
524 copy of the object's properties. Modifying it does not modify the object
525 itself.
526  
527 =cut
528  
529 1 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
530 1 my $self = shift;
531 1 +{ map +($_ => $self->prop($_)), $self->keys };
532 }
533  
534 101 *TO_JSON=*value;
535  
536  
537  
538  
539 145263 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
540 145263 refaddr shift;
541 }
542  
543 1361 ▶sub primitive { !1 };
Subroutine primitive: called CC=1 SCAR=0.0
Pod undocumented
544  
545 15836 ▶sub prototype {
Subroutine prototype: called CC=2 SCAR=6.9
Pod undocumented
546 15836 ▶ @_ > 1 ? (${+shift}->{prototype} = $_[1]) : ${+shift}->{prototype};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 476 15360 15836
547 }
548  
549  
550  
551  
552 992 ▶sub to_primitive {
Subroutine to_primitive: called CC=7 SCAR=19.5
Pod undocumented
553 992 my($self, $hint) = @_;
554  
555 992 my @methods = ('valueOf','toString');
556 992 ▶ defined $hint && $hint eq 'string' and @methods = reverse @methods;
Branch: 1 branch
BranchTrueFalseTotal
defined $hint and $hint eq "string" and @methods = reverse @methods 328 664 992
Condition100%3/3
defined $hint and $hint eq "string"
!ll&&!rl&&r
259405328
MC/DC100%2/2
defined $hint and $hint eq "string"
defined $hint $hint eq "string"
Truth table100%3/3
A and B
A= defined $hintB= $hint eq "string"
ABresult
0X0
100
111
557  
558 992 my $method; my $prim;
559 992 for (@methods) {
560 1334 ▶ defined($method = $self->prop($_)) || next;
Branch: 1 branch
BranchTrueFalseTotal
defined($method = $self->prop($_)) or next 1322 12 1334
561 1322 ▶ ($prim = $method->apply($self))->primitive || next;
Branch: 1 branch
BranchTrueFalseTotal
($prim = $method->apply($self))->primitive or next 978 338 1316
562 978 return $prim;
563 }
564  
565 die new JE::Object::Error::TypeError $self->global,
566 add_line_number "An object of type " .
567 8 ▶ (eval {$self->class} || ref $self) .
Condition33%1/3
eval { do { $self->class } } || ref $self
l!l&&r!l&&!r
800
MC/DC0%0/2
eval { do { $self->class } } || ref $self
eval { do { $self->class } } ref $self
Truth table33%1/3
A || B
A= eval { do { $self->class } }B= ref $self
ABresult
1X1
011
000
568 " cannot be converted to a primitive";
569 }
570  
571  
572  
573  
574 42 ▶sub to_boolean {
Subroutine to_boolean: called CC=1 SCAR=0.0
Pod undocumented
575 42 JE::Boolean->new( $${+shift}{global}, 1 );
576 }
577  
578 330 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
579 330 shift->to_primitive('string')->to_string;
580 }
581  
582  
583 408 ▶sub to_number {
Subroutine to_number: called CC=1 SCAR=0.0
Pod undocumented
584 408 shift->to_primitive('number')->to_number;
585 }
586  
587 1872 ▶sub to_object { $_[0] }
Subroutine to_object: called CC=1 SCAR=0.0
Pod undocumented
588  
589 1657 ▶sub global { ${+shift}->{global} }
Subroutine global: called CC=1 SCAR=0.0
Pod undocumented
590  
591 =back
592  
593 =cut
594  
595  
596  
597  
598 #----------- PRIIVATE ROUTIES ---------------#
599  
600 # _init_proto takes the Object prototype (Object.prototype) as its sole
601 # arg and adds all the default properties thereto.
602  
603 107 ▶sub _init_proto {
Subroutine _init_proto: called CC=1 SCAR=0.0
604 107 my $proto = shift;
605 107 my $global = $$proto->{global};
606  
607 # E 15.2.4
608  
609 107 $proto->prop({
610 dontenum => 1,
611 name => 'constructor',
612 value => $global->prop('Object'),
613 });
614  
615 569 ▶ my $toString_sub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
616 569 my $self = shift;
617 569 JE::String->new($global,
618 '[object ' . $self->class . ']');
619 107 };
620  
621 107 $proto->prop({
622 name => 'toString',
623 value => JE::Object::Function->new({
624 scope => $global,
625 name => 'toString',
626 length => 0,
627 function_args => ['this'],
628 function => $toString_sub,
629 no_proto => 1,
630 }),
631 dontenum => 1,
632 });
633  
634 $proto->prop({
635 name => 'toLocaleString',
636 value => JE::Object::Function->new({
637 scope => $global,
638 name => 'toLocaleString',
639 length => 0,
640 function_args => ['this'],
641 7 ▶ function => sub { shift->method('toString') },
Subroutine __ANON__: called CC=1 SCAR=0.0
642 107 no_proto => 1,
643 }),
644 dontenum => 1,
645 });
646  
647 $proto->prop({
648 name => 'valueOf',
649 value => JE::Object::Function->new({
650 scope => $global,
651 name => 'valueOf',
652 length => 0,
653 function_args => ['this'],
654 327 ▶ function => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
655 107 no_proto => 1,
656 }),
657 dontenum => 1,
658 });
659  
660 $proto->prop({
661 name => 'hasOwnProperty',
662 value => JE::Object::Function->new({
663 scope => $global,
664 name => 'hasOwnProperty',
665 argnames => ['V'],
666 function_args => ['this', 'args'],
667 24 ▶ function => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
668 24 ▶ JE::Boolean->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 22 2 24
669 shift->exists(
670 defined $_[0] ? $_[0] : 'undefined'
671 )
672 );
673 },
674 107 no_proto => 1,
675 }),
676 dontenum => 1,
677 });
678  
679 $proto->prop({
680 name => 'isPrototypeOf',
681 value => JE::Object::Function->new({
682 scope => $global,
683 name => 'isPrototypeOf',
684 argnames => ['V'],
685 function_args => ['this', 'args'],
686 15 ▶ function => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
687 15 my ($self, $obj) = @_;
688  
689 15 ▶ !defined $obj || $obj->primitive and return
Branch: 1 branch
BranchTrueFalseTotal
!defined($obj) || $obj->primitive and return "JE::Boolean"->new($global, 0) 2 13 15
Condition100%3/3
not defined $obj or $obj->primitive
l!l&&r!l&&!r
1113
MC/DC100%2/2
not defined $obj or $obj->primitive
not defined $obj $obj->primitive
Truth table100%3/3
not(A) or B
A= not defined $objB= $obj->primitive
ABresult
1X1
011
000
690 JE::Boolean->new($global, 0);
691  
692 13 my $id = $self->id;
693 13 my $proto = $obj;
694  
695 13 ▶ while (defined($proto = $proto->prototype))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($proto = $proto->prototype)) 13 1 14
696 {
697 13 ▶ $proto->id eq $id and return
Branch: 1 branch
BranchTrueFalseTotal
$proto->id eq $id and return "JE::Boolean"->new($global, 1) 12 1 13
698 JE::Boolean->new($global, 1);
699 }
700  
701 1 return JE::Boolean->new($global, 0);
702 },
703 107 no_proto => 1,
704 }),
705 dontenum => 1,
706 });
707  
708 $proto->prop({
709 name => 'propertyIsEnumerable',
710 value => JE::Object::Function->new({
711 scope => $global,
712 name => 'propertyIsEnumerable',
713 argnames => ['V'],
714 function_args => ['this', 'args'],
715 222 ▶ function => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
716 222 ▶ return JE::Boolean->new($global,
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 220 2 222
717 shift->is_enum(
718 defined $_[0] ? $_[0] : 'undefined'
719 )
720 );
721 },
722 107 no_proto => 1,
723 }),
724 dontenum => 1,
725 });
726 }
727  
728  
729  
730 #----------- TYING MAGIC ---------------#
731  
732 # I'm putting the object itself behind the tied hash, so that no new object
733 # has to be created.
734 # That means that tied %$obj returns $obj.
735  
736  
737 911 ▶sub _get_tie {
Subroutine _get_tie: called CC=2 SCAR=6.9
738 911 my $self = shift;
739 911 my $guts = $$self;
740 911 ▶ $$guts{tie} or tie %{ $$guts{tie} }, __PACKAGE__, $self;
Branch: 1 branch
BranchTrueFalseTotal
$guts->{"tie"} or tie %{$$guts{"tie"};}, __PACKAGE__, $self 148 763 911
741 911 $$guts{tie};
742 }
743  
744 766 ▶sub TIEHASH { $_[1] }
Subroutine TIEHASH: called CC=1 SCAR=0.0
745 903 ▶sub FETCH { $_[0]->prop($_[1]) }
Subroutine FETCH: called CC=1 SCAR=0.0
746 735 ▶sub STORE {
Subroutine STORE: called CC=9 SCAR=22.2
747 735 my($self, $key, $val) = @_;
748 735 my $global = $self->global;
749 735 ▶ if(ref $val eq 'HASH' && !blessed $val
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'HASH' and not builtin::blessed($val) and not %$val and svref_2object($val)->REFCNT == 2) { } 3 732 735
elsif (ref $val eq 'ARRAY' and not builtin::blessed($val) and not @$val and svref_2object($val)->REFCNT == 2) { } 3 1457 1460
Condition77%7/9
ref $val eq "HASH" and not builtin::blessed($val) and not %$val and svref_2object($val)->REFCNT == 2
!ll&&!rl&&r
73023
ref $val eq "HASH" and not builtin::blessed($val) and not %$val
!ll&&!rl&&r
73005
ref $val eq "HASH" and not builtin::blessed($val)
!ll&&!rl&&r
73005
MC/DC50%2/4
ref $val eq "HASH" and not builtin::blessed($val) and not %$val and svref_2object($val)->REFCNT == 2
ref $val eq "HASH" not builtin::blessed($val) not %$val svref_2object($val)->REFCNT == 2
Truth table60%3/5
A and not(B) and not(C) and D
A= ref $val eq "HASH"B= not builtin::blessed($val)C= not %$valD= svref_2object($val)->REFCNT == 2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
750 && !%$val && svref_2object($val)->REFCNT == 2) {
751 3 $val = tie %$val, __PACKAGE__, __PACKAGE__->new(
752 $global);
753 ▶ } elsif (ref $val eq 'ARRAY' && !blessed $val && !@$val &&
Condition77%7/9
ref $val eq "ARRAY" and not builtin::blessed($val) and not @$val and svref_2object($val)->REFCNT == 2
!ll&&!rl&&r
72813
ref $val eq "ARRAY" and not builtin::blessed($val) and not @$val
!ll&&!rl&&r
72804
ref $val eq "ARRAY" and not builtin::blessed($val)
!ll&&!rl&&r
72804
MC/DC50%2/4
ref $val eq "ARRAY" and not builtin::blessed($val) and not @$val and svref_2object($val)->REFCNT == 2
ref $val eq "ARRAY" not builtin::blessed($val) not @$val svref_2object($val)->REFCNT == 2
Truth table60%3/5
A and not(B) and not(C) and D
A= ref $val eq "ARRAY"B= not builtin::blessed($val)C= not @$valD= svref_2object($val)->REFCNT == 2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
754 svref_2object($val)->REFCNT == 2) {
755 3 require JE::Object::Array;
756 3 $val = tie @$val, 'JE::Object::Array',
757 JE::Object::Array->new($global);
758 }
759 735 $self->prop($key => $global->upgrade($val))
760 }
761 #sub CLEAR { }
762 # ~~~ have yet to implement this
763 18 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
764 18 my $val = $_[0]->prop($_[1]);
765 18 $_[0]->delete($_[1]);
766 18 $val;
767 }
768 12 ▶sub EXISTS { $_[0]->exists($_[1]) }
Subroutine EXISTS: called CC=1 SCAR=0.0
769 11 ▶sub FIRSTKEY { ($_[0]->keys)[0] }
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
770 29 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=3 SCAR=11.0
771 29 my @keys = $_[0]->keys;
772 29 my $last = $_[1];
773 29 for (0..$#keys) {
774 58 ▶ if ($last eq $keys[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq $keys[$_]) 29 29 58
775 29 return $keys[$_+1]
776 }
777 }
778  
779 # ~~~ What *should* we do if the property has been
780 # deleted?
781 # I think this means the iterator should have been reset (from the
782 # user's point of view), so we'll start from the beginning.
783  
784 0 return $keys[0];
785 }
786  
787 0 ▶sub DDS_freeze { my $self = shift; delete $$$self{tie}; $self }
Subroutine DDS_freeze: not called CC=1 SCAR=6.9
Pod undocumented
788  
789  
790 #----------- THE REST OF THE DOCUMENTATION ---------------#
791  
792 =head1 USING AN OBJECT AS A HASH
793  
794 Note first of all that C<\%$obj> is I<not> the same as C<< $obj->value >>.
795 The C<value> method creates a new hash containing just the enumerable
796 properties of the object and its prototypes. It's just a plain hash--no
797 ties, no magic. C<%$obj>, on the other hand, is another creature...
798  
799 C<%$obj> returns a magic hash which only lists enumerable properties
800 when you write C<keys %$obj>, but still provides access to the rest.
801  
802 Using C<exists> on this hash will check to see whether it is the object's
803 I<own> property, and not a prototype's.
804  
805 Assignment to the hash itself currently
806 throws an error:
807  
808 %$obj = (); # no good!
809  
810 This is simply because I have not yet figured out what it should do. If
811 anyone has any ideas, please let me know.
812  
813 Autovivification works, so you can write
814  
815 $obj->{a}{b} = 3;
816  
817 and the 'a' element will be created if did not already exist. Note that,
818 if the property C<did> exist but was undefined (from JS's point of view),
819 this throws an error.
820  
821 =begin paranoia
822  
823 One potential problem with this is that, when perl autovivifies in the
824 example
825 above, it first calls C<FETCH> and, when it sees that the result is not
826 defined, then calls C<STORE> with C<{}> as the value. It then uses that
827 same hash that it passed to C<STORE>, and does I<not> make a second call to
828 C<FETCH>. This means that, for autovivification to work, the empty hash
829 that perl automatically assigns has to be tied to the new JE::Object that
830 is created. Now, the same sequence of calls to tie
831 handlers can be triggered by the following lines:
832  
833 my %h;
834 $obj->{a};
835 $h{b} = 3;
836  
837 And, of course, you don't want your %h hash transmogrified and tied to a
838 JE::Object, do you? (Normally
839 hashes and arrays are copied by STORE.) So the only feasible way (I can
840 think of) to
841 make the distinction is to use reference counts (which is what I'm using),
842 but I don't know whether they will change
843 between versions of Perl.
844  
845 =end paranoia
846  
847 =head1 INNARDS
848  
849 Each C<JE::Object> instance is a blessed reference to a hash ref. The
850 contents of the hash
851 are as follows:
852  
853 $$self->{global} a reference to the global object
854 $$self->{props} a hash ref of properties, the values being
855 JavaScript objects
856 $$self->{prop_readonly} a hash ref with property names for the keys
857 and booleans (that indicate whether prop-
858 erties are read-only) for the values
859 $$self->{prop_dontdel} a hash ref in the same format as
860 prop_readonly that indicates whether proper-
861 ties are undeletable
862 $$self->{keys} an array of the names of enumerable
863 properties
864 $$self->{prototype} a reference to this object's prototype
865  
866 In derived classes, if you need to store extra information, begin the hash
867 keys with an underscore or use at least one capital letter in each key.
868 Such keys
869 will never be used by the
870 classes that come with the JE distribution.
871  
872 =head1 SEE ALSO
873  
874 L<JE>
875  
876 L<JE::Types>
877  
878 =cut
879  
880  
881 101 1;
882  
« JE/Object/Proxy.pm ↑ Summary JE/Object/Number/maxvalue.pl »