JE/Number.pm

filter: Statementstmt 96.1% 74 / 77 Branchbran 91.6% 33 / 36 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 29 / 29 Podpod 0.0% 0 / 17 totaltotal 85.4% 147 / 172 CC 22 SCAR 9.9
CC
22
Cov
94%
new
5.0
_numify
5.0
__ANON__
4.0
CRAP 2.7
« JE/String.pm ↑ Summary JE/Object/Boolean.pm »
1 package JE::Number;
2  
3 101 our $VERSION = '0.066';
4  
5 101 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 101 ▶use warnings; no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # I need constants for inf and nan, because perl 5.8.6 interprets the
10 # strings "inf" and "nan" as 0 in numeric context.
11  
12 # This is what I get running Deparse on 5.8.6:
13 # $ perl -mO=Deparse -e 'print 0+"nan"'
14 # print 0;
15 # $ perl -mO=Deparse -e 'print 0+"inf"'
16 # print 0;
17 # And here is the output from 5.8.8 (PPC [big-endian]):
18 # $ perl -mO=Deparse -e 'print 0+"nan"'
19 # print unpack("F", pack("h*", "f78f000000000000"));
20 # $ perl -mO=Deparse -e 'print 0+"inf"'
21 # print 9**9**9;
22 # I don't know about 5.8.7.
23  
24 # However, that 'unpack' does not work on little-endian Xeons running
25 # Linux. What I'm testing it on is running 5.8.5, so the above one-liners
26 # don't work. But I can use this:
27 # $ perl -mO=Deparse -mPOSIX=fmod -e 'use constant nan=>fmod 0,0;print nan'
28 # use POSIX (split(/,/, 'fmod', 0));
29 # use constant ('nan', fmod(0, 0));
30 # print sin(9**9**9);
31  
32 # sin 9**9**9 also works on the PPC.
33  
34  
35  
36 101 ▶use constant nan => sin 9**9**9;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 101 ▶use constant inf => 9**9**9;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 101 ▶use overload fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
40 37665 ▶ '""' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
41 37665 my $value = $_[0][0];
42 37665 ▶ $value == inf ? 'Infinity' :
Branch: 3 branches
BranchTrueFalseTotal
$value == 9**9**9 ? : 3 37662 37665
$value == -9**9**9 ? : 0 37662 37662
$value == $value ? : 37573 89 37662
43 $value == -+inf ? '-Infinity' :
44 $value == $value ? $value :
45 'NaN'
46 },
47 '0+' => 'value',
48 93 ▶ bool => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
49 93 my $value = $_[0][0];
50 93 ▶ $value && $value == $value;
Branch: 1 branch
BranchTrueFalseTotal
$value and $value == $value 92 1 93
51 },
52 2852 ▶ '+' => sub { $_[0]->value + $_[1] }, # ~~~ I shouldn’t need this,
Subroutine __ANON__: called CC=1 SCAR=0.0
53 # but perl’s magic
54 # auto-generation
55 # isn’t so magic.
56 # cmp => sub { "$_[0]" cmp $_[1] };
57 101 ;
58  
59 101 ▶use Scalar::Util qw 'blessed tainted';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 101 require JE::String;
62 101 require JE::Boolean;
63 101 require JE::Object::Number;
64  
65  
66  
67 # Each JE::Number object is an array ref like this: [value, global object]
68  
69 56375 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
70 56375 my ($class,$global,$val) = @_;
71
72 56375 ▶ if(defined blessed $val and can $val 'to_number') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($val) and $val->can("to_number")) 49 56326 56375
Condition66%2/3
defined builtin::blessed($val) and $val->can("to_number")
!ll&&!rl&&r
56326049
MC/DC50%1/2
defined builtin::blessed($val) and $val->can("to_number")
defined builtin::blessed($val) $val->can("to_number")
Truth table66%2/3
A and B
A= defined builtin::blessed($val)B= $val->can("to_number")
ABresult
0X0
100
111
73 49 my $new_val = $val->to_number;
74 49 ▶ ref $new_val eq $class and return $new_val;
Branch: 1 branch
BranchTrueFalseTotal
ref $new_val eq $class and return $new_val 48 1 49
75 1 ▶ eval { $new_val->isa(__PACKAGE__) } and
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $new_val->isa(__PACKAGE__) } } and $val = $new_val->[0], goto RETURN 1 0 1
76 $val = $new_val->[0],
77 goto RETURN;
78 }
79  
80 56326 $val = _numify($val);
81  
82 56327 RETURN:
83 bless [$val, $global], $class;
84 }
85  
86 56337 ▶sub _numify {
Subroutine _numify: called CC=5 SCAR=16.1
87 56337 ▶ my $val = shift||0;
Condition100%2/2
shift() || 0
l!l
473009037
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
88 # For perls that don't interpret 0+"inf" as inf:
89 56337 ▶ if ($val =~ /^\s*([+-]?)(inf|nan)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^\s*([+-]?)(inf|nan)/i) { } 2848 53489 56337
90 2848 ▶ $val = lc $2 eq 'nan' ? nan :
Branch: 2 branches
BranchTrueFalseTotal
lc $2 eq 'nan' ? : 2229 619 2848
$1 eq '-' ? : 455 164 619
91 $1 eq '-' ? -(inf) : inf;
92 }
93 53489 else { $val+=0 }
94 56337 $val;
95 }
96  
97 49 ▶sub prop {
Subroutine prop: called CC=2 SCAR=6.9
Pod undocumented
98 49 ▶ if(@_ > 2) { return $_[2] } # If there is a value, just return it
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 1 48 49
99  
100 48 my ($self, $name) = @_;
101
102 48 $$self[1]->prototype_for('Number')->prop($name);
103 }
104  
105 2 ▶sub keys {
Subroutine keys: called CC=1 SCAR=0.0
Pod undocumented
106 2 my $self = shift;
107 2 $$self[1]->prototype_for('Number')->keys;
108 }
109  
110 1 ▶sub delete {1}
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
111  
112 2 ▶sub method {
Subroutine method: called CC=1 SCAR=0.0
Pod undocumented
113 2 my $self = shift;
114 2 $$self[1]->prototype_for('Number')->prop(shift)->apply(
115 $self,$$self[1]->upgrade(@_)
116 );
117 }
118  
119 42413 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
120 42413 shift->[0]
121 }
122 101 *TO_JSON=*value;
123  
124 1 ▶sub exists { !1 }
Subroutine exists: called CC=1 SCAR=0.0
Pod undocumented
125  
126 13735 ▶sub typeof { 'number' }
Subroutine typeof: called CC=1 SCAR=0.0
Pod undocumented
127 67 ▶sub class { 'Number' }
Subroutine class: called CC=1 SCAR=0.0
Pod undocumented
128 21030 ▶sub id {
Subroutine id: called CC=4 SCAR=13.9
Pod undocumented
129 21030 my $value = shift->value;
130 # This should (I hope) take care of systems that stringify nan and
131 # inf oddly:
132 21030 ▶ 'num:' . ($value != $value ? 'nan' :
Branch: 3 branches
BranchTrueFalseTotal
$value != $value ? : 2912 18118 21030
$value == 9**9**9 ? : 69 18049 18118
$value == -9**9**9 ? : 47 18002 18049
133 $value == inf ? 'inf' :
134 $value == -+inf ? '-inf' :
135 $value)
136 }
137 361 ▶sub primitive { 1 }
Subroutine primitive: called CC=1 SCAR=0.0
Pod undocumented
138  
139 52053 ▶sub to_primitive { $_[0] }
Subroutine to_primitive: called CC=1 SCAR=0.0
Pod undocumented
140 280 ▶sub to_boolean {
Subroutine to_boolean: called CC=2 SCAR=6.9
Pod undocumented
141 280 my $value = (my $self = shift)->[0];
142 280 ▶ JE::Boolean->new($$self[1],
Condition100%3/3
$value && $value == $value
!ll&&!rl&&r
1243153
MC/DC100%2/2
$value && $value == $value
$value $value == $value
Truth table100%3/3
A && B
A= $valueB= $value == $value
ABresult
0X0
100
111
143 $value && $value == $value);
144 }
145  
146 13176 ▶sub to_string { # ~~~ I need to find out whether Perl's number
Subroutine to_string: called CC=4 SCAR=13.9
Pod undocumented
147 # stringification is consistent with E 9.8.1 for
148 # finite numbers.
149 13176 my $value = (my $self = shift)->[0];
150 13176 ▶ JE::String->_new($$self[1],
Branch: 3 branches
BranchTrueFalseTotal
$value == 9**9**9 ? : 397 12779 13176
$value == -9**9**9 ? : 394 12385 12779
$value == $value ? : 11989 396 12385
151 $value == inf ? 'Infinity' :
152 $value == -(inf) ? '-Infinity' :
153 $value == $value ? $value :
154 'NaN'
155 );
156 }
157  
158 101 *to_number = \& to_primitive;
159  
160 14 ▶sub to_object {
Subroutine to_object: called CC=1 SCAR=0.0
Pod undocumented
161 14 my $self = shift;
162 14 JE::Object::Number->new($$self[1], $self);
163 }
164  
165 2 ▶sub global { $_[0][1] }
Subroutine global: called CC=1 SCAR=0.0
Pod undocumented
166  
167 16 ▶sub taint {
Subroutine taint: called CC=2 SCAR=8.1
Pod undocumented
168 16 my $self = shift;
169 16 ▶ tainted $self->[0] and return $self;
Branch: 1 branch
BranchTrueFalseTotal
&tainted($self->[0]) and return $self 16 0 16
170 0 my $alter_ego = [@$self];
171 101 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 0 $alter_ego->[0] += shift();
173 0 return bless $alter_ego, ref $self;
174 }
175  
176  
177 =head1 NAME
178  
179 JE::Number - JavaScript number value
180  
181 =head1 SYNOPSIS
182  
183 use JE;
184 use JE::Number;
185  
186 $j = JE->new;
187  
188 $js_num = new JE::Number $j, 17;
189  
190 $perl_num = $js_num->value;
191  
192 $js_num->to_object; # returns a new JE::Object::Number
193  
194 =head1 DESCRIPTION
195  
196 This class implements JavaScript number values for JE. The difference
197 between this and JE::Object::Number is that that module implements
198 number
199 I<objects,> while this module implements the I<primitive> values.
200  
201 Right now, this module simply uses Perl numbers underneath for storing
202 the JavaScript numbers. It seems that whether Perl numbers are in accord with the IEEE 754 standard that
203 ECMAScript uses is system-dependent. If anyone requires IEEE 754
204 compliancy,
205 a patch would be welcome. :-)
206  
207 The C<new> method accepts a global (JE) object and a number as its
208 two arguments. If the latter is an object with a C<to_number> method whose
209 return value isa JE::Number, that object's internal value
210 will be used. Otherwise the arg itself is used. (The precise details of
211 the behaviour of C<new> when the second arg is a object are subject to
212 change.) It is numified Perl-style,
213 so 'nancy' becomes NaN
214 and 'information' becomes Infinity.
215  
216 The C<value> method produces a Perl scalar. The C<0+> numeric operator is
217 overloaded and produces the same.
218  
219 Stringification and boolification are overloaded and produce the same
220 results as in JavaScript
221  
222 The C<typeof> and C<class> methods produce the strings 'number' and
223 'Number', respectively.
224  
225 =head1 SEE ALSO
226  
227 =over 4
228  
229 =item L<JE>
230  
231 =item L<JE::Types>
232  
233 =item L<JE::Object::Number>
234  
235 =back
236  
237 =cut
238  
239  
240  
241  
« JE/String.pm ↑ Summary JE/Object/Boolean.pm »