/usr/share/perl5/JE/Object/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 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | package JE::Object::Number;
our $VERSION = '0.060';
use strict;
use warnings;
use constant inf => 9**9**9;
our @ISA = 'JE::Object';
use Scalar::Util 'blessed';
require JE::Code;
require JE::Number;
require JE::Object;
require JE::Object::Function;
require JE::String;
import JE::Code 'add_line_number';
sub add_line_number;
=head1 NAME
JE::Object::Number - JavaScript Number object class
=head1 SYNOPSIS
use JE;
use JE::Object::Number;
$j = new JE;
$js_num_obj = new JE::Object::Number $j, 953.7;
$perl_scalar = $js_num_obj->value;
0 + $js_num_obj; # 953.7
=head1 DESCRIPTION
This class implements JavaScript Number objects for JE. The difference
between this and JE::Number is that that module implements
I<primitive> number values, while this module implements the I<objects.>
=head1 METHODS
See L<JE::Types> for descriptions of most of the methods. Only what
is specific to JE::Object::Number is explained here.
=over
=cut
sub new {
my($class, $global, $val) = @_;
my $self = $class->SUPER::new($global, {
prototype => $global->prototype_for('Number')
|| $global->prop('Number')->prop('prototype')
});
$$$self{value} = defined blessed $val && $val->can('to_number')
? $val->to_number->[0]
: JE::Number::_numify($val);
$self;
}
=item value
Returns a Perl scalar containing the number that the object holds.
=cut
sub value { $${$_[0]}{value} }
=item class
Returns the string 'Number'.
=cut
sub class { 'Number' }
our @_digits = (0..9, 'a' .. 'z');
sub _new_constructor {
my $global = shift;
my $f = JE::Object::Function->new({
name => 'Number',
scope => $global,
argnames => [qw/value/],
function => sub {
defined $_[0] ? $_[0]->to_number :
JE'Number->new($global, 0);
},
function_args => ['args'],
constructor => sub {
unshift @_, __PACKAGE__;
goto &new;
},
constructor_args => ['scope','args'],
});
# The max according to ECMA-262 ≈ 1.7976931348623157e+308.
# The max I can get in Perl with a literal is 1.797693134862314659999e+308,
# probably as a result of perl bug #41202. Using ECMA’s maximum does not
# make sense in our case, anyway, as we are using perl’s (i.e., the sys-
# tem’s) floating point.
# So I am using routines borrowed from Data::Float to get what are the
# actual minimum and maximum values that we can handle.
$f->prop({
name => 'MAX_VALUE',
autoload => '
require "JE/Object/Number/maxvalue.pl";
$JE::Object::Number::max_finite
',
dontenum => 1,
dontdel => 1,
readonly => 1,
});
$f->prop({
name => 'MIN_VALUE',
autoload => '
require "JE/Object/Number/maxvalue.pl";
$JE::Object::Number::min_finite
',
dontenum => 1,
dontdel => 1,
readonly => 1,
});
$f->prop({
name => 'NaN',
value => JE::Number->new($global, 'nan'),
dontenum => 1,
dontdel => 1,
readonly => 1,
});
$f->prop({
name => 'NEGATIVE_INFINITY',
value => JE::Number->new($global, '-inf'),
dontenum => 1,
dontdel => 1,
readonly => 1,
});
$f->prop({
name => 'POSITIVE_INFINITY', # positively infinite
value => JE::Number->new($global, 'inf'),
dontenum => 1,
dontdel => 1,
readonly => 1,
});
my $proto = bless $f->prop({
name => 'prototype',
dontenum => 1,
readonly => 1,
}), __PACKAGE__;
$global->prototype_for(Number=>$proto);
$$$proto{value} = 0;
$proto->prop({
name => 'toString',
value => JE::Object::Function->new({
scope => $global,
name => 'toString',
argnames => ['radix'],
no_proto => 1,
function_args => ['this','args'],
function => sub {
my $self = shift;
die JE::Object::Error::TypeError->new(
$global, add_line_number
"Argument to " .
"Number.prototype.toString is not"
. " a " .
"Number object"
) unless $self->class eq 'Number';
my $radix = shift;
!defined $radix || $radix->id eq 'undef'
and return
$self->to_primitive->to_string;
($radix = $radix->to_number->value)
== 10 || $radix < 2 || $radix > 36 ||
$radix =~ /\./ and return $self->to_string;
if ($radix == 2) {
return JE::String->new($global,
sprintf '%b', $self->value);
}
elsif($radix == 8) {
return JE::String->new($global,
sprintf '%o', $self->value);
}
elsif($radix == 16) {
return JE::String->new($global,
sprintf '%x', $self->value);
}
my $num = $self->value;
my $result = '';
while($num >= 1) {
substr($result,0,0) =
$_digits[$num % $radix];
$num /= $radix;
}
return JE::String->new($global, $result);
},
}),
dontenum => 1,
});
$proto->prop({
name => 'toLocaleString',
value => JE::Object::Function->new({
scope => $global,
name => 'toLocaleString',
no_proto => 1,
function_args => ['this'],
function => sub {
my $self = shift;
die JE::Object::Error::TypeError->new(
$global, add_line_number
"Argument to " .
"Number.prototype.toLocaleString ".
"is not"
. " a " .
"Number object"
) unless $self->class eq 'Number';
# ~~~ locale stuff
return JE::String->_new($global,
$self->value);
},
}),
dontenum => 1,
});
$proto->prop({
name => 'valueOf',
value => JE::Object::Function->new({
scope => $global,
name => 'valueOf',
no_proto => 1,
function_args => ['this'],
function => sub {
my $self = shift;
die JE::Object::Error::TypeError->new(
$global, add_line_number
"Argument to " .
"Number.prototype.valueOf is not"
. " a " .
"Number object"
) unless $self->class eq 'Number';
# We also deal with plain JE::Numbers here
return
ref $self eq 'JE::Number'
? $self
: JE::Number->new($global,$$$self{value});
},
}),
dontenum => 1,
});
$proto->prop({
name => 'toFixed',
value => JE::Object::Function->new({
scope => $global,
name => 'toFixed',
no_proto => 1,
argnames => ['fractionDigits'],
function_args => ['this','args'],
function => sub {
my $self = shift;
die JE::Object::Error::TypeError->new(
$global, add_line_number
"Argument to " .
"Number.prototype.toFixed is not"
. " a " .
"Number object"
) unless $self->class eq 'Number';
my $places = shift;
if(defined $places) {
$places = ($places = int $places->to_number) == $places && $places;
}
else { $places = 0 }
$places < 0 and throw JE::Object::Error::RangeError->new($global,
"Invalid number of decimal places: $places " .
"(negative numbers not supported)"
);
my $num = $self->value;
$num == $num or return JE::String->_new($global, 'NaN');
abs $num >= 1000000000000000000000
and return JE::String->_new($global, $num);
# ~~~ if/when JE::Number::to_string is rewritten, make this use the same
# algorithm
# Deal with numbers ending with 5. perl (in Snow Leopard at least) rounds
# 30.125 down, whereas ECMAScript says that it should round up. (15.7.4.5:
# ‘Let n be an integer for which the exact mathematical value of
# n ÷ 10^f – x is as close to zero as possible. If there are two such n,
# pick the larger n.’)
if((my $sprintfed = sprintf "%." . ($places+1) . 'f', $num) =~ /5\z/) {
(my $upper = $sprintfed) =~ s/\.?.\z//;
my $lower = $upper;
++substr $upper,-1,1;
return JE::String->_new(
$global, $upper-$num <= $num-$lower ? $upper : $lower
);
}
return JE::String->_new($global, sprintf "%.${places}f", $num);
},
}),
dontenum => 1,
});
$proto->prop({
name => 'toExponential',
value => JE::Object::Function->new({
scope => $global,
name => 'toExponential',
no_proto => 1,
argnames => ['fractionDigits'],
function_args => ['this','args'],
function => sub {
my $self = shift;
die JE::Object::Error::TypeError->new(
$global, add_line_number
"Argument to " .
"Number.prototype. toExponential is not"
. " a " .
"Number object"
) unless $self->class eq 'Number';
my $num = $self->value;
$num == $num or return JE::String->_new($global, 'NaN');
abs $num == inf && return JE::String->_new($global,
($num < 0 && '-') . 'Infinity');
my $places = shift;
if(defined $places) {
$places
= 0+(($places = int $places->to_number) == $places) && $places;
}
else { $places = !1 }
$places < 0 and throw JE::Object::Error::RangeError->new($global,
"Invalid number of decimal places: $places " .
"(negative numbers not supported)"
);
# Deal with half-way rounding. See the note above in toFixed. It applies to
# toExponential as well (except that this is section 15.7.4.6).
if((my $sprintfed = sprintf "%." . ($places+1) . 'e', $num) =~ /5e/) {
(my $upper = $sprintfed) =~ s/\.?.(e.*)\z//;
my $lower = $upper;
++substr $upper,-1,1;
(my $ret = ($upper-$num <= $num-$lower ? $upper : $lower) . $1)
=~ s/\.?0*e([+-])0*(?!\z)/e$1/; # convert 0.0000e+00 to 0e+0
return JE::String->_new(
$global, $ret
);
}
my $result = sprintf "%"."."x!!length($places)."${places}e", $num;
$result =~ s/\.?0*e([+-])0*(?!\z)/e$1/; # convert 0.0000e+00 to 0e+0
return JE::String->_new($global, $result);
},
}),
dontenum => 1,
});
$proto->prop({
name => 'toPrecision',
value => JE::Object::Function->new({
scope => $global,
name => 'toPrecision',
no_proto => 1,
argnames => ['precision'],
function_args => ['this','args'],
function => sub {
my $self = shift;
die JE::Object::Error::TypeError->new(
$global, add_line_number
"Argument to " .
"Number.prototype. toPrecision is not"
. " a " .
"Number object"
) unless $self->class eq 'Number';
my $num = $self->value;
$num == $num or return JE::String->_new($global, 'NaN');
abs $num == inf && return JE::String->_new($global,
($num < 0 && '-') . 'Infinity');
my $prec = shift;
if(!defined $prec || $prec->id eq 'undef') {
return JE::String->_new($global, $num);
# ~~~ if/when JE::Number::to_string is rewritten, make this use the same
# algorithm
}
$prec = ($prec = int $prec->to_number) == $prec && $prec;
$prec < 1 and throw JE::Object::Error::RangeError->new($global,
"Precision out of range: $prec " .
"(must be >= 1)"
);
# ~~~ Probably not the most efficient alrogithm. maybe I coould optimimse
# it later. OD yI have tot proooofrfreoad my aown tiyping.?
if ($num == 0) {
$prec == 1 or $num = '0.' . '0' x ($prec-1);
}
else {
$num = sprintf "%.${prec}g", $num; # round it off
my($e) = sprintf "%.0e", $num, =~ /e(.*)/;
if($e < -6 || $e >= $prec) {
($num = sprintf "%.".($prec-1)."e", $num)
=~ s/(?<=e[+-])0+(?!\z)//; # convert 0e+00 to 0e+0
$num =~ /\./ or $num =~ s/e/.e/;
}
else { $num = sprintf "%." . ($prec - 1 - $e) . 'f', $num }
}
return JE::String->_new($global, $num);
},
}),
dontenum => 1,
});
$f;
}
return "a true value";
=back
=head1 SEE ALSO
=over 4
=item JE
=item JE::Types
=item JE::Object
=item JE::Number
=back
=cut
|