Permalink
Jul 28, 2018
Dec 18, 2019
Mar 10, 2018
Apr 23, 2018
Apr 23, 2018
Apr 23, 2018
Jul 28, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Feb 22, 2019
Aug 2, 2018
Mar 10, 2018
Feb 15, 2020
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Feb 22, 2019
Feb 22, 2019
Mar 10, 2018
Jul 28, 2018
Jul 28, 2018
May 19, 2017
Jul 28, 2018
Mar 10, 2018
Feb 11, 2018
Feb 15, 2020
Feb 11, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 22, 2019
Feb 22, 2019
Feb 10, 2018
Mar 10, 2018
Mar 23, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Feb 11, 2018
Feb 11, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Nov 28, 2018
Mar 10, 2018
Nov 28, 2018
Jun 14, 2018
Nov 2, 2019
Mar 10, 2018
Oct 27, 2019
Oct 27, 2019
Feb 22, 2019
Oct 22, 2019
Oct 22, 2019
Oct 27, 2018
Oct 22, 2019
Feb 11, 2018
Newer
100644
870 lines (784 sloc)
26.5 KB
11
function int_value(obj){
12
// Instances of int subclasses that call int.__new__(cls, value)
13
// have an attribute $value set
14
return obj.$value !== undefined ? obj.$value : obj
15
}
16
35
int.as_integer_ratio = function(){
36
var $ = $B.args("as_integer_ratio", 1, {self:null}, ["self"],
37
arguments, {}, null, null)
38
return $B.$list([$.self, 1])
39
}
40
62
case "big":
63
var num = _bytes[_len - 1]
64
var _mult = 256
65
for(var i = _len - 2; i >= 0; i--){
66
// For operations, use the functions that can take or return
67
// big integers
68
num = $B.add($B.mul(_mult, _bytes[i]), num)
69
_mult = $B.mul(_mult,256)
70
}
71
if(! signed){return num}
72
if(_bytes[0] < 128){return num}
73
return $B.sub(num, _mult)
74
case "little":
75
var num = _bytes[0]
76
if(num >= 128){num = num - 256}
77
var _mult = 256
78
for(var i = 1; i < _len; i++){
79
num = $B.add($B.mul(_mult, _bytes[i]), num)
80
_mult = $B.mul(_mult, 256)
81
}
82
if(! signed){return num}
83
if(_bytes[_len - 1] < 128){return num}
84
return $B.sub(num, _mult)
90
int.to_bytes = function(){
91
var $ = $B.args("to_bytes", 3,
92
{self: null, len: null, byteorder: null},
93
["self", "len", "byteorder"],
94
arguments, {}, "args", "kw"),
95
self = $.self,
96
len = $.len,
97
byteorder = $.byteorder,
98
kwargs = $.kw
99
if(! _b_.isinstance(len, _b_.int)){
100
throw _b_.TypeError.$factory("integer argument expected, got " +
102
}
103
if(["little", "big"].indexOf(byteorder) == -1){
104
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
105
}
106
var signed = kwargs.$string_dict["signed"] || false,
107
res = []
108
109
if(self < 0){
110
if(! signed){
111
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
112
}
113
self = Math.pow(256, len) + self
114
}
115
var value = self
116
while(true){
117
var quotient = Math.floor(value / 256),
118
rest = value - 256 * quotient
119
res.push(rest)
120
if(quotient == 0){
121
break
122
}
123
value = quotient
124
}
125
if(res.length > len){
126
throw _b_.OverflowError.$factory("int too big to convert")
131
}
132
if(byteorder == "big"){res = res.reverse()}
133
return {
134
__class__: _b_.bytes,
135
source: res
136
}
141
int.__bool__ = function(self){
142
return int_value(self).valueOf() == 0 ? false : true
143
}
155
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
156
if(_b_.isinstance(other, _b_.complex)){
169
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
170
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
198
if(fmt.sign !== undefined){
199
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
200
res = fmt.sign + res
201
}
202
}
221
for(var i = 0; i < nb; i++){
222
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
253
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
278
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
279
$B.long_int.$factory(other)))
281
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
282
if(rlshift !== _b_.None){return rlshift(self)}
289
if(other.__class__ === $B.long_int){
290
return $B.long_int.__mod__($B.long_int.$factory(self), other)
291
}
294
if(other === false){other = 0}
295
else if(other === true){other = 1}
296
if(other == 0){throw _b_.ZeroDivisionError.$factory(
300
if(_b_.hasattr(other, "__rmod__")){
301
return $B.$getattr(other, "__rmod__")(self)
302
}
319
var res = self * other
320
if(res > $B.min_int && res < $B.max_int){return res}
321
else{
322
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
323
$B.long_int.$factory(other)))
324
}
334
return $B.make_complex(int.__mul__(self, other.$real),
335
int.__mul__(self, other.$imag))
340
var $temp = other.slice(0, other.length)
341
for(var i = 0; i < val; i++){res = res.concat($temp)}
345
if(_b_.hasattr(other, "__rmul__")){
346
return $B.$getattr(other, "__rmul__")(self)
347
}
351
int.__ne__ = function(self, other){
352
var res = int.__eq__(self, other)
353
return (res === _b_.NotImplemented) ? res : !res
354
}
355
359
if(cls === undefined){
360
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
362
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
363
}
364
if(cls === int){return int.$factory(value)}
365
return {
366
__class__: cls,
376
other = int_value(other)
377
switch(other.valueOf()) {
378
case 0:
379
return int.$factory(1)
380
case 1:
381
return int.$factory(self.valueOf())
384
// If z is provided, the algorithm is faster than computing
385
// self ** other then applying the modulo z
386
if(z == 1){return 0}
387
var result = 1,
388
base = self % z,
389
exponent = other,
390
long_int = $B.long_int
391
while(exponent > 0){
392
if(exponent % 2 == 1){
393
if(result * base > $B.max_int){
394
result = long_int.__mul__(
395
long_int.$factory(result),
396
long_int.$factory(base))
397
result = long_int.__mod__(result, z)
398
}else{
399
result = (result * base) % z
400
}
401
}
402
exponent = exponent >> 1
403
if(base * base > $B.max_int){
404
base = long_int.__mul__(long_int.$factory(base),
405
long_int.$factory(base))
406
base = long_int.__mod__(base, z)
407
}else{
408
base = (base * base) % z
409
}
413
var res = Math.pow(self.valueOf(), other.valueOf())
414
if(res > $B.min_int && res < $B.max_int){return res}
417
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
418
$B.long_int.$factory(other)))
432
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
433
if(rpow !== _b_.None){
434
return rpow(self)
435
}
448
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
449
$B.long_int.$factory(other)))
451
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
452
if(rrshift !== _b_.None){return rrshift(self)}
457
if(typeof self == "number"){
458
if(int.$factory[attr] === undefined){
459
throw _b_.AttributeError.$factory(
460
"'int' object has no attribute '" + attr + "'")
462
throw _b_.AttributeError.$factory(
463
"'int' object attribute '" + attr + "' is read-only")
479
if(other.__class__ === $B.long_int){
480
return new Number(self / parseInt(other.value))
481
}
482
return new Number(self / other)
496
if(_b_.hasattr(other, "__rtruediv__")){
497
return $B.$getattr(other, "__rtruediv__")(self)
503
s = _b_.bin(self)
504
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
509
int.numerator = function(self){return self}
510
int.denominator = function(self){return int.$factory(1)}
511
int.imag = function(self){return int.$factory(0)}
512
int.real = function(self){return self}
520
if(other.__class__ === $B.long_int){
521
return $B.long_int.__sub__($B.long_int.$factory(self),
522
$B.long_int.$factory(other))
525
if(self > $B.max_int32 || self < $B.min_int32 ||
526
other > $B.max_int32 || other < $B.min_int32){
527
return $B.long_int.__sub__($B.long_int.$factory(self),
528
$B.long_int.$factory(other))
532
if(_b_.isinstance(other, _b_.bool)){return self - other}
533
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
534
if(rsub !== _b_.None){return rsub(self)}
541
var opf = $op_func.replace(/-/gm, $op)
542
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
543
eval("int.__" + $ops[$op] + "__ = " + opf)
550
if(typeof other == "number"){
551
var res = self.valueOf() - other.valueOf()
552
if(res > $B.min_int && res < $B.max_int){return res}
553
else{return $B.long_int.__sub__($B.long_int.$factory(self),
554
$B.long_int.$factory(other))}
558
return $B.long_int.__sub__($B.long_int.$factory(self),
559
$B.long_int.$factory(other))
569
var bool_value = 0;
570
if(other.valueOf()){bool_value = 1}
571
return self - bool_value
576
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
577
if(rsub !== _b_.None){return rsub(self)}
583
var opf = $op_func.replace(/-/gm, $op)
584
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
585
eval("int.__" + $ops[$op] + "__ = " + opf)
610
eval("int.__"+$B.$comps[$op] + "__ = " +
611
$comp_func.replace(/>/gm, $op).
612
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
613
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
619
var $valid_digits = function(base) {
620
var digits = ""
621
if(base === 0){return "0"}
622
if(base < 10){
638
if(typeof value == "number" &&
639
(base === undefined || base == 10)){return parseInt(value)}
645
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
646
{"base": 10}, null, null),
647
value = $ns["x"],
648
base = $ns["base"]
664
if(base == 10){
665
if(value < $B.min_int || value > $B.max_int){
666
return $B.long_int.$factory(value)
667
}
673
var res = parseInt(value, base)
674
if(value < $B.min_int || value > $B.max_int){
675
return $B.long_int.$factory(value, base)
676
}
681
if(value === true){return Number(1)}
682
if(value === false){return Number(0)}
683
if(value.__class__ === $B.long_int){
690
function invalid(value, base){
691
throw _b_.ValueError.$factory("invalid literal for int() with base " +
692
base + ": '" + _b_.str.$factory(value) + "'")
693
}
696
if(typeof value == "string") {
697
var _value = value.trim() // remove leading/trailing whitespace
698
if(_value.length == 2 && base == 0 &&
699
(_value == "0b" || _value == "0o" || _value == "0x")){
700
throw _b_.ValueError.$factory("invalid value")
701
}
702
if(_value.length >2) {
703
var _pre = _value.substr(0, 2).toUpperCase()
704
if(base == 0){
705
if(_pre == "0B"){base = 2}
706
if(_pre == "0O"){base = 8}
707
if(_pre == "0X"){base = 16}
708
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
709
else if(_pre == "0O" && base != 8){invalid(_value, base)}
720
var _digits = $valid_digits(base),
721
_re = new RegExp("^[+-]?[" + _digits + "]" +
722
"[" + _digits + "_]*$", "i"),
723
match = _re.exec(_value)
741
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
742
if(num_value === null){
743
throw _b_.TypeError.$factory(
744
"int() argument must be a string, a bytes-like " +
745
"object or a number, not '" + $B.class_name(value) + "'")
746
}
747
return num_value
756
if(obj === null || obj === undefined ){ return false}
757
switch(typeof obj){
758
case "boolean":
759
return obj
760
case "number":
761
case "string":
762
if(obj){return true}
763
return false
764
default:
766
var klass = obj.__class__ || $B.get_class(obj),
767
missing = {},
768
bool_method = $B.$getattr(klass, "__bool__", missing)
769
if(bool_method === missing){
770
try{return _b_.len(obj) > 0}
773
var res = $B.$call(bool_method)(obj)
774
if(res !== true && res !== false){
775
throw _b_.TypeError.$factory("__bool__ should return " +
776
"bool, returned " + $B.class_name(res))
777
}
778
return res
795
var methods = $B.op2method.subset("operations", "binary", "comparisons",
796
"boolean")
797
for(var op in methods){
798
var method = "__" + methods[op] + "__"
799
bool[method] = (function(op){
800
return function(self, other){
801
var value = self ? 1 : 0
802
if(int[op] !== undefined){
803
return int[op](value, other)
804
}
805
}
806
})(method)
810
if(_b_.isinstance(other, bool)){
811
return self && other
812
}else if(_b_.isinstance(other, int)){
813
return int.__and__(bool.__index__(self), int.__index__(other))
814
}
815
return _b_.NotImplemented
826
if(_b_.isinstance(other, bool)){
827
return self || other
828
}else if(_b_.isinstance(other, int)){
829
return int.__or__(bool.__index__(self), int.__index__(other))
830
}
831
return _b_.NotImplemented
841
if(_b_.dir(self).indexOf(attr) > -1){
842
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
843
}else{
844
var msg = "'bool' object has no attribute '" + attr + "'"
845
}
846
throw _b_.AttributeError.$factory(msg)
850
if(_b_.isinstance(other, bool)){
851
return self ^ other ? true : false
852
}else if(_b_.isinstance(other, int)){
853
return int.__xor__(bool.__index__(self), int.__index__(other))
854
}
855
return _b_.NotImplemented
858
bool.$factory = function(){
859
// Calls $B.$bool, which is used inside the generated JS code and skips
860
// arguments control.