Permalink
Jul 28, 2018
Mar 10, 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
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 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
Feb 22, 2019
Feb 22, 2019
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Oct 15, 2018
Feb 22, 2019
Feb 11, 2018
Oct 27, 2018
Feb 11, 2018
Newer
100644
854 lines (768 sloc)
26.1 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
56
case "big":
57
var num = _bytes[_len - 1]
58
var _mult = 256
59
for(var i = _len - 2; i >= 0; i--){
60
// For operations, use the functions that can take or return
61
// big integers
62
num = $B.add($B.mul(_mult, _bytes[i]), num)
63
_mult = $B.mul(_mult,256)
64
}
65
if(! signed){return num}
66
if(_bytes[0] < 128){return num}
67
return $B.sub(num, _mult)
68
case "little":
69
var num = _bytes[0]
70
if(num >= 128){num = num - 256}
71
var _mult = 256
72
for(var i = 1; i < _len; i++){
73
num = $B.add($B.mul(_mult, _bytes[i]), num)
74
_mult = $B.mul(_mult, 256)
75
}
76
if(! signed){return num}
77
if(_bytes[_len - 1] < 128){return num}
78
return $B.sub(num, _mult)
84
int.to_bytes = function(){
85
var $ = $B.args("to_bytes", 3,
86
{self: null, len: null, byteorder: null},
87
["self", "len", "byteorder"],
88
arguments, {}, "args", "kw"),
89
self = $.self,
90
len = $.len,
91
byteorder = $.byteorder,
92
kwargs = $.kw
93
if(! _b_.isinstance(len, _b_.int)){
94
throw _b_.TypeError.$factory("integer argument expected, got " +
96
}
97
if(["little", "big"].indexOf(byteorder) == -1){
98
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
99
}
100
var signed = kwargs.$string_dict["signed"] || false,
101
res = []
102
103
if(self < 0){
104
if(! signed){
105
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
106
}
107
self = Math.pow(256, len) + self
108
}
109
var value = self
110
while(true){
111
var quotient = Math.floor(value / 256),
112
rest = value - 256 * quotient
113
res.push(rest)
114
if(quotient == 0){
115
break
116
}
117
value = quotient
118
}
119
if(res.length > len){
120
throw _b_.OverflowError.$factory("int too big to convert")
121
}
122
if(byteorder == "big"){res = res.reverse()}
123
return {
124
__class__: _b_.bytes,
125
source: res
126
}
131
int.__bool__ = function(self){
132
return int_value(self).valueOf() == 0 ? false : true
133
}
145
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
146
if(_b_.isinstance(other, _b_.complex)){
159
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
160
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
188
if(fmt.sign !== undefined){
189
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
190
res = fmt.sign + res
191
}
192
}
211
for(var i = 0; i < nb; i++){
212
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
243
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
268
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
269
$B.long_int.$factory(other)))
271
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
272
if(rlshift !== _b_.None){return rlshift(self)}
279
if(other.__class__ === $B.long_int){
280
return $B.long_int.__mod__($B.long_int.$factory(self), other)
281
}
284
if(other === false){other = 0}
285
else if(other === true){other = 1}
286
if(other == 0){throw _b_.ZeroDivisionError.$factory(
307
var res = self * other
308
if(res > $B.min_int && res < $B.max_int){return res}
309
else{
310
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
311
$B.long_int.$factory(other)))
312
}
322
return $B.make_complex(int.__mul__(self, other.$real),
323
int.__mul__(self, other.$imag))
328
var $temp = other.slice(0, other.length)
329
for(var i = 0; i < val; i++){res = res.concat($temp)}
333
if(_b_.hasattr(other, "__rmul__")){
334
return $B.$getattr(other, "__rmul__")(self)
335
}
339
int.__ne__ = function(self, other){
340
var res = int.__eq__(self, other)
341
return (res === _b_.NotImplemented) ? res : !res
342
}
343
347
if(cls === undefined){
348
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
350
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
351
}
352
if(cls === int){return int.$factory(value)}
353
return {
354
__class__: cls,
364
other = int_value(other)
365
switch(other.valueOf()) {
366
case 0:
367
return int.$factory(1)
368
case 1:
369
return int.$factory(self.valueOf())
371
if(z !== undefined && z !== null){
372
// If z is provided, the algorithm is faster than computing
373
// self ** other then applying the modulo z
374
if(z == 1){return 0}
375
var result = 1,
376
base = self % z,
377
exponent = other,
378
long_int = $B.long_int
379
while(exponent > 0){
380
if(exponent % 2 == 1){
381
if(result * base > $B.max_int){
382
result = long_int.__mul__(
383
long_int.$factory(result),
384
long_int.$factory(base))
385
result = long_int.__mod__(result, z)
386
}else{
387
result = (result * base) % z
388
}
389
}
390
exponent = exponent >> 1
391
if(base * base > $B.max_int){
392
base = long_int.__mul__(long_int.$factory(base),
393
long_int.$factory(base))
394
base = long_int.__mod__(base, z)
395
}else{
396
base = (base * base) % z
397
}
401
var res = Math.pow(self.valueOf(), other.valueOf())
402
if(res > $B.min_int && res < $B.max_int){return res}
405
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
406
$B.long_int.$factory(other)))
433
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
434
$B.long_int.$factory(other)))
436
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
437
if(rrshift !== _b_.None){return rrshift(self)}
442
if(typeof self == "number"){
443
if(int.$factory[attr] === undefined){
444
throw _b_.AttributeError.$factory(
445
"'int' object has no attribute '" + attr + "'")
447
throw _b_.AttributeError.$factory(
448
"'int' object attribute '" + attr + "' is read-only")
464
if(other.__class__ === $B.long_int){
465
return new Number(self / parseInt(other.value))
466
}
467
return new Number(self / other)
481
if(_b_.hasattr(other, "__rtruediv__")){
482
return $B.$getattr(other, "__rtruediv__")(self)
488
s = _b_.bin(self)
489
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
494
int.numerator = function(self){return self}
495
int.denominator = function(self){return int.$factory(1)}
496
int.imag = function(self){return int.$factory(0)}
497
int.real = function(self){return self}
505
if(other.__class__ === $B.long_int){
506
return $B.long_int.__sub__($B.long_int.$factory(self),
507
$B.long_int.$factory(other))
510
if(self > $B.max_int32 || self < $B.min_int32 ||
511
other > $B.max_int32 || other < $B.min_int32){
512
return $B.long_int.__sub__($B.long_int.$factory(self),
513
$B.long_int.$factory(other))
517
if(_b_.isinstance(other, _b_.bool)){return self - other}
518
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
519
if(rsub !== _b_.None){return rsub(self)}
526
var opf = $op_func.replace(/-/gm, $op)
527
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
528
eval("int.__" + $ops[$op] + "__ = " + opf)
535
if(typeof other == "number"){
536
var res = self.valueOf() - other.valueOf()
537
if(res > $B.min_int && res < $B.max_int){return res}
538
else{return $B.long_int.__sub__($B.long_int.$factory(self),
539
$B.long_int.$factory(other))}
543
return $B.long_int.__sub__($B.long_int.$factory(self),
544
$B.long_int.$factory(other))
554
var bool_value = 0;
555
if(other.valueOf()){bool_value = 1}
556
return self - bool_value
561
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
562
if(rsub !== _b_.None){return rsub(self)}
568
var opf = $op_func.replace(/-/gm, $op)
569
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
570
eval("int.__" + $ops[$op] + "__ = " + opf)
595
eval("int.__"+$B.$comps[$op] + "__ = " +
596
$comp_func.replace(/>/gm, $op).
597
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
598
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
604
var $valid_digits = function(base) {
605
var digits = ""
606
if(base === 0){return "0"}
607
if(base < 10){
623
if(typeof value == "number" &&
624
(base === undefined || base == 10)){return parseInt(value)}
636
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
637
{"base": 10}, null, null),
638
value = $ns["x"],
639
base = $ns["base"]
655
if(base == 10){
656
if(value < $B.min_int || value > $B.max_int){
657
return $B.long_int.$factory(value)
658
}
664
var res = parseInt(value, base)
665
if(value < $B.min_int || value > $B.max_int){
666
return $B.long_int.$factory(value, base)
667
}
672
if(value === true){return Number(1)}
673
if(value === false){return Number(0)}
674
if(value.__class__ === $B.long_int){
681
function invalid(value, base){
682
throw _b_.ValueError.$factory("invalid literal for int() with base " +
683
base + ": '" + _b_.str.$factory(value) + "'")
684
}
687
if(typeof value == "string") {
688
var _value = value.trim() // remove leading/trailing whitespace
689
if(_value.length == 2 && base == 0 &&
690
(_value == "0b" || _value == "0o" || _value == "0x")){
691
throw _b_.ValueError.$factory("invalid value")
692
}
693
if(_value.length >2) {
694
var _pre = _value.substr(0, 2).toUpperCase()
695
if(base == 0){
696
if(_pre == "0B"){base = 2}
697
if(_pre == "0O"){base = 8}
698
if(_pre == "0X"){base = 16}
699
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
700
else if(_pre == "0O" && base != 8){invalid(_value, base)}
711
var _digits = $valid_digits(base),
712
_re = new RegExp("^[+-]?[" + _digits + "]" +
713
"[" + _digits + "_]*$", "i"),
714
match = _re.exec(_value)
731
var $int = $B.$getattr(value, "__int__", _b_.None)
732
if($int !== _b_.None){return $int()}
733
734
var $index = $B.$getattr(value, "__index__", _b_.None)
735
if($index !== _b_.None){return $index()}
737
var $trunc = $B.$getattr(value, "__trunc__", _b_.None)
738
if($trunc !== _b_.None){
739
var res = $truc(),
740
int_func = $int
741
if(int_func === _b_.None){
742
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
746
if(_b_.isinstance(res, int)){return int_value(res)}
747
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
750
throw _b_.TypeError.$factory(
751
"int() argument must be a string, a bytes-like " +
761
if(obj === null || obj === undefined ){ return false}
762
switch(typeof obj){
763
case "boolean":
764
return obj
765
case "number":
766
case "string":
767
if(obj){return true}
768
return false
769
default:
771
var missing = {},
772
bool_func = $B.$getattr(obj, "__bool__", missing)
773
if(bool_func === missing){
794
var methods = $B.op2method.subset("operations", "binary", "comparisons",
795
"boolean")
796
for(var op in methods){
797
var method = "__" + methods[op] + "__"
798
bool[method] = (function(op){
799
return function(self, other){
800
var value = self ? 1 : 0
801
if(int[op] !== undefined){
802
return int[op](value, other)
803
}
804
}
805
})(method)
808
bool.__and__ = function(self, other){
809
return $B.$bool(int.__and__(self, other))
830
if(_b_.dir(self).indexOf(attr) > -1){
831
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
832
}else{
833
var msg = "'bool' object has no attribute '" + attr + "'"
834
}
835
throw _b_.AttributeError.$factory(msg)
842
bool.$factory = function(){
843
// Calls $B.$bool, which is used inside the generated JS code and skips
844
// arguments control.