/usr/share/perl5/JE/Number.pm is in libje-perl 0.060-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package JE::Number;
our $VERSION = '0.060';
use strict;
use warnings; no warnings 'utf8';
# I need constants for inf and nan, because perl 5.8.6 interprets the
# strings "inf" and "nan" as 0 in numeric context.
# This is what I get running Deparse on 5.8.6:
# $ perl -mO=Deparse -e 'print 0+"nan"'
# print 0;
# $ perl -mO=Deparse -e 'print 0+"inf"'
# print 0;
# And here is the output from 5.8.8 (PPC [big-endian]):
# $ perl -mO=Deparse -e 'print 0+"nan"'
# print unpack("F", pack("h*", "f78f000000000000"));
# $ perl -mO=Deparse -e 'print 0+"inf"'
# print 9**9**9;
# I don't know about 5.8.7.
# However, that 'unpack' does not work on little-endian Xeons running
# Linux. What I'm testing it on is running 5.8.5, so the above one-liners
# don't work. But I can use this:
# $ perl -mO=Deparse -mPOSIX=fmod -e 'use constant nan=>fmod 0,0;print nan'
# use POSIX (split(/,/, 'fmod', 0));
# use constant ('nan', fmod(0, 0));
# print sin(9**9**9);
# sin 9**9**9 also works on the PPC.
use constant nan => sin 9**9**9;
use constant inf => 9**9**9;
use overload fallback => 1,
'""' => sub {
my $value = $_[0][0];
$value == inf ? 'Infinity' :
$value == -+inf ? '-Infinity' :
$value == $value ? $value :
'NaN'
},
'0+' => 'value',
bool => sub {
my $value = $_[0][0];
$value && $value == $value;
},
'+' => sub { $_[0]->value + $_[1] }, # ~~~ I shouldn’t need this,
# but perl’s magic
# auto-generation
# isn’t so magic.
# cmp => sub { "$_[0]" cmp $_[1] };
;
use Scalar::Util qw 'blessed tainted';
require JE::String;
require JE::Boolean;
require JE::Object::Number;
# Each JE::Number object is an array ref like this: [value, global object]
sub new {
my ($class,$global,$val) = @_;
if(defined blessed $val and can $val 'to_number') {
my $new_val = $val->to_number;
ref $new_val eq $class and return $new_val;
eval { $new_val->isa(__PACKAGE__) } and
$val = $new_val->[0],
goto RETURN;
}
$val = _numify($val);
RETURN:
bless [$val, $global], $class;
}
sub _numify {
my $val = shift||0;
# For perls that don't interpret 0+"inf" as inf:
if ($val =~ /^\s*([+-]?)(inf|nan)/i) {
$val = lc $2 eq 'nan' ? nan :
$1 eq '-' ? -(inf) : inf;
}
else { $val+=0 }
$val;
}
sub prop {
if(@_ > 2) { return $_[2] } # If there is a value, just return it
my ($self, $name) = @_;
$$self[1]->prototype_for('Number')->prop($name);
}
sub keys {
my $self = shift;
$$self[1]->prototype_for('Number')->keys;
}
sub delete {1}
sub method {
my $self = shift;
$$self[1]->prototype_for('Number')->prop(shift)->apply(
$self,$$self[1]->upgrade(@_)
);
}
sub value {
shift->[0]
}
*TO_JSON=*value;
sub exists { !1 }
sub typeof { 'number' }
sub class { 'Number' }
sub id {
my $value = shift->value;
# This should (I hope) take care of systems that stringify nan and
# inf oddly:
'num:' . ($value != $value ? 'nan' :
$value == inf ? 'inf' :
$value == -+inf ? '-inf' :
$value)
}
sub primitive { 1 }
sub to_primitive { $_[0] }
sub to_boolean {
my $value = (my $self = shift)->[0];
JE::Boolean->new($$self[1],
$value && $value == $value);
}
sub to_string { # ~~~ I need to find out whether Perl's number
# stringification is consistent with E 9.8.1 for
# finite numbers.
my $value = (my $self = shift)->[0];
JE::String->_new($$self[1],
$value == inf ? 'Infinity' :
$value == -(inf) ? '-Infinity' :
$value == $value ? $value :
'NaN'
);
}
*to_number = \& to_primitive;
sub to_object {
my $self = shift;
JE::Object::Number->new($$self[1], $self);
}
sub global { $_[0][1] }
sub taint {
my $self = shift;
tainted $self->[0] and return $self;
my $alter_ego = [@$self];
no warnings 'numeric';
$alter_ego->[0] += shift();
return bless $alter_ego, ref $self;
}
=head1 NAME
JE::Number - JavaScript number value
=head1 SYNOPSIS
use JE;
use JE::Number;
$j = JE->new;
$js_num = new JE::Number $j, 17;
$perl_num = $js_num->value;
$js_num->to_object; # returns a new JE::Object::Number
=head1 DESCRIPTION
This class implements JavaScript number values for JE. The difference
between this and JE::Object::Number is that that module implements
number
I<objects,> while this module implements the I<primitive> values.
Right now, this module simply uses Perl numbers underneath for storing
the JavaScript numbers. It seems that whether Perl numbers are in accord with the IEEE 754 standard that
ECMAScript uses is system-dependent. If anyone requires IEEE 754
compliancy,
a patch would be welcome. :-)
The C<new> method accepts a global (JE) object and a number as its
two arguments. If the latter is an object with a C<to_number> method whose
return value isa JE::Number, that object's internal value
will be used. Otherwise the arg itself is used. (The precise details of
the behaviour of C<new> when the second arg is a object are subject to
change.) It is numified Perl-style,
so 'nancy' becomes NaN
and 'information' becomes Infinity.
The C<value> method produces a Perl scalar. The C<0+> numeric operator is
overloaded and produces the same.
Stringification and boolification are overloaded and produce the same
results as in JavaScript
The C<typeof> and C<class> methods produce the strings 'number' and
'Number', respectively.
=head1 SEE ALSO
=over 4
=item L<JE>
=item L<JE::Types>
=item L<JE::Object::Number>
=back
=cut
|