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
853 lines (767 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
55
case "big":
56
var num = _bytes[_len - 1]
57
var _mult = 256
58
for(var i = _len - 2; i >= 0; i--){
59
// For operations, use the functions that can take or return
60
// big integers
61
num = $B.add($B.mul(_mult, _bytes[i]), num)
62
_mult = $B.mul(_mult,256)
63
}
64
if(! signed){return num}
65
if(_bytes[0] < 128){return num}
66
return $B.sub(num, _mult)
67
case "little":
68
var num = _bytes[0]
69
if(num >= 128){num = num - 256}
70
var _mult = 256
71
for(var i = 1; i < _len; i++){
72
num = $B.add($B.mul(_mult, _bytes[i]), num)
73
_mult = $B.mul(_mult, 256)
74
}
75
if(! signed){return num}
76
if(_bytes[_len - 1] < 128){return num}
77
return $B.sub(num, _mult)
83
int.to_bytes = function(){
84
var $ = $B.args("to_bytes", 3,
85
{self: null, len: null, byteorder: null},
86
["self", "len", "byteorder"],
87
arguments, {}, "args", "kw"),
88
self = $.self,
89
len = $.len,
90
byteorder = $.byteorder,
91
kwargs = $.kw
92
if(! _b_.isinstance(len, _b_.int)){
93
throw _b_.TypeError.$factory("integer argument expected, got " +
95
}
96
if(["little", "big"].indexOf(byteorder) == -1){
97
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
98
}
99
var signed = kwargs.$string_dict["signed"] || false,
100
res = []
101
102
if(self < 0){
103
if(! signed){
104
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
105
}
106
self = Math.pow(256, len) + self
107
}
108
var value = self
109
while(true){
110
var quotient = Math.floor(value / 256),
111
rest = value - 256 * quotient
112
res.push(rest)
113
if(quotient == 0){
114
break
115
}
116
value = quotient
117
}
118
if(res.length > len){
119
throw _b_.OverflowError.$factory("int too big to convert")
120
}
121
if(byteorder == "big"){res = res.reverse()}
122
return {
123
__class__: _b_.bytes,
124
source: res
125
}
130
int.__bool__ = function(self){
131
return int_value(self).valueOf() == 0 ? false : true
132
}
144
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
145
if(_b_.isinstance(other, _b_.complex)){
158
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
159
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
187
if(fmt.sign !== undefined){
188
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
189
res = fmt.sign + res
190
}
191
}
210
for(var i = 0; i < nb; i++){
211
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
242
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
267
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
268
$B.long_int.$factory(other)))
270
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
271
if(rlshift !== _b_.None){return rlshift(self)}
278
if(other.__class__ === $B.long_int){
279
return $B.long_int.__mod__($B.long_int.$factory(self), other)
280
}
283
if(other === false){other = 0}
284
else if(other === true){other = 1}
285
if(other == 0){throw _b_.ZeroDivisionError.$factory(
306
var res = self * other
307
if(res > $B.min_int && res < $B.max_int){return res}
308
else{
309
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
310
$B.long_int.$factory(other)))
311
}
321
return $B.make_complex(int.__mul__(self, other.$real),
322
int.__mul__(self, other.$imag))
327
var $temp = other.slice(0, other.length)
328
for(var i = 0; i < val; i++){res = res.concat($temp)}
332
if(_b_.hasattr(other, "__rmul__")){
333
return $B.$getattr(other, "__rmul__")(self)
334
}
338
int.__ne__ = function(self, other){
339
var res = int.__eq__(self, other)
340
return (res === _b_.NotImplemented) ? res : !res
341
}
342
346
if(cls === undefined){
347
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
349
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
350
}
351
if(cls === int){return int.$factory(value)}
352
return {
353
__class__: cls,
363
other = int_value(other)
364
switch(other.valueOf()) {
365
case 0:
366
return int.$factory(1)
367
case 1:
368
return int.$factory(self.valueOf())
370
if(z !== undefined && z !== null){
371
// If z is provided, the algorithm is faster than computing
372
// self ** other then applying the modulo z
373
if(z == 1){return 0}
374
var result = 1,
375
base = self % z,
376
exponent = other,
377
long_int = $B.long_int
378
while(exponent > 0){
379
if(exponent % 2 == 1){
380
if(result * base > $B.max_int){
381
result = long_int.__mul__(
382
long_int.$factory(result),
383
long_int.$factory(base))
384
result = long_int.__mod__(result, z)
385
}else{
386
result = (result * base) % z
387
}
388
}
389
exponent = exponent >> 1
390
if(base * base > $B.max_int){
391
base = long_int.__mul__(long_int.$factory(base),
392
long_int.$factory(base))
393
base = long_int.__mod__(base, z)
394
}else{
395
base = (base * base) % z
396
}
400
var res = Math.pow(self.valueOf(), other.valueOf())
401
if(res > $B.min_int && res < $B.max_int){return res}
404
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
405
$B.long_int.$factory(other)))
432
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
433
$B.long_int.$factory(other)))
435
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
436
if(rrshift !== _b_.None){return rrshift(self)}
441
if(typeof self == "number"){
442
if(int.$factory[attr] === undefined){
443
throw _b_.AttributeError.$factory(
444
"'int' object has no attribute '" + attr + "'")
446
throw _b_.AttributeError.$factory(
447
"'int' object attribute '" + attr + "' is read-only")
463
if(other.__class__ === $B.long_int){
464
return new Number(self / parseInt(other.value))
465
}
466
return new Number(self / other)
480
if(_b_.hasattr(other, "__rtruediv__")){
481
return $B.$getattr(other, "__rtruediv__")(self)
487
s = _b_.bin(self)
488
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
493
int.numerator = function(self){return self}
494
int.denominator = function(self){return int.$factory(1)}
495
int.imag = function(self){return int.$factory(0)}
496
int.real = function(self){return self}
504
if(other.__class__ === $B.long_int){
505
return $B.long_int.__sub__($B.long_int.$factory(self),
506
$B.long_int.$factory(other))
509
if(self > $B.max_int32 || self < $B.min_int32 ||
510
other > $B.max_int32 || other < $B.min_int32){
511
return $B.long_int.__sub__($B.long_int.$factory(self),
512
$B.long_int.$factory(other))
516
if(_b_.isinstance(other, _b_.bool)){return self - other}
517
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
518
if(rsub !== _b_.None){return rsub(self)}
525
var opf = $op_func.replace(/-/gm, $op)
526
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
527
eval("int.__" + $ops[$op] + "__ = " + opf)
534
if(typeof other == "number"){
535
var res = self.valueOf() - other.valueOf()
536
if(res > $B.min_int && res < $B.max_int){return res}
537
else{return $B.long_int.__sub__($B.long_int.$factory(self),
538
$B.long_int.$factory(other))}
542
return $B.long_int.__sub__($B.long_int.$factory(self),
543
$B.long_int.$factory(other))
553
var bool_value = 0;
554
if(other.valueOf()){bool_value = 1}
555
return self - bool_value
560
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
561
if(rsub !== _b_.None){return rsub(self)}
567
var opf = $op_func.replace(/-/gm, $op)
568
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
569
eval("int.__" + $ops[$op] + "__ = " + opf)
594
eval("int.__"+$B.$comps[$op] + "__ = " +
595
$comp_func.replace(/>/gm, $op).
596
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
597
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
603
var $valid_digits = function(base) {
604
var digits = ""
605
if(base === 0){return "0"}
606
if(base < 10){
622
if(typeof value == "number" &&
623
(base === undefined || base == 10)){return parseInt(value)}
635
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
636
{"base": 10}, null, null),
637
value = $ns["x"],
638
base = $ns["base"]
654
if(base == 10){
655
if(value < $B.min_int || value > $B.max_int){
656
return $B.long_int.$factory(value)
657
}
663
var res = parseInt(value, base)
664
if(value < $B.min_int || value > $B.max_int){
665
return $B.long_int.$factory(value, base)
666
}
671
if(value === true){return Number(1)}
672
if(value === false){return Number(0)}
673
if(value.__class__ === $B.long_int){
680
function invalid(value, base){
681
throw _b_.ValueError.$factory("invalid literal for int() with base " +
682
base + ": '" + _b_.str.$factory(value) + "'")
683
}
686
if(typeof value == "string") {
687
var _value = value.trim() // remove leading/trailing whitespace
688
if(_value.length == 2 && base == 0 &&
689
(_value == "0b" || _value == "0o" || _value == "0x")){
690
throw _b_.ValueError.$factory("invalid value")
691
}
692
if(_value.length >2) {
693
var _pre = _value.substr(0, 2).toUpperCase()
694
if(base == 0){
695
if(_pre == "0B"){base = 2}
696
if(_pre == "0O"){base = 8}
697
if(_pre == "0X"){base = 16}
698
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
699
else if(_pre == "0O" && base != 8){invalid(_value, base)}
710
var _digits = $valid_digits(base),
711
_re = new RegExp("^[+-]?[" + _digits + "]" +
712
"[" + _digits + "_]*$", "i"),
713
match = _re.exec(_value)
730
var $int = $B.$getattr(value, "__int__", _b_.None)
731
if($int !== _b_.None){return $int()}
732
733
var $index = $B.$getattr(value, "__index__", _b_.None)
734
if($index !== _b_.None){return $index()}
736
var $trunc = $B.$getattr(value, "__trunc__", _b_.None)
737
if($trunc !== _b_.None){
738
var res = $truc(),
739
int_func = $int
740
if(int_func === _b_.None){
741
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
745
if(_b_.isinstance(res, int)){return int_value(res)}
746
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
749
throw _b_.TypeError.$factory(
750
"int() argument must be a string, a bytes-like " +
760
if(obj === null || obj === undefined ){ return false}
761
switch(typeof obj){
762
case "boolean":
763
return obj
764
case "number":
765
case "string":
766
if(obj){return true}
767
return false
768
default:
770
var missing = {},
771
bool_func = $B.$getattr(obj, "__bool__", missing)
772
if(bool_func === missing){
793
var methods = $B.op2method.subset("operations", "binary", "comparisons",
794
"boolean")
795
for(var op in methods){
796
var method = "__" + methods[op] + "__"
797
bool[method] = (function(op){
798
return function(self, other){
799
var value = self ? 1 : 0
800
if(int[op] !== undefined){
801
return int[op](value, other)
802
}
803
}
804
})(method)
807
bool.__and__ = function(self, other){
808
return $B.$bool(int.__and__(self, other))
829
if(_b_.dir(self).indexOf(attr) > -1){
830
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
831
}else{
832
var msg = "'bool' object has no attribute '" + attr + "'"
833
}
834
throw _b_.AttributeError.$factory(msg)
841
bool.$factory = function(){
842
// Calls $B.$bool, which is used inside the generated JS code and skips
843
// arguments control.