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
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
Nov 2, 2019
Feb 22, 2019
Feb 22, 2019
Feb 22, 2019
Mar 10, 2018
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
884 lines (797 sloc)
27.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
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(
317
var res = self * other
318
if(res > $B.min_int && res < $B.max_int){return res}
319
else{
320
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
321
$B.long_int.$factory(other)))
322
}
332
return $B.make_complex(int.__mul__(self, other.$real),
333
int.__mul__(self, other.$imag))
338
var $temp = other.slice(0, other.length)
339
for(var i = 0; i < val; i++){res = res.concat($temp)}
343
if(_b_.hasattr(other, "__rmul__")){
344
return $B.$getattr(other, "__rmul__")(self)
345
}
349
int.__ne__ = function(self, other){
350
var res = int.__eq__(self, other)
351
return (res === _b_.NotImplemented) ? res : !res
352
}
353
357
if(cls === undefined){
358
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
360
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
361
}
362
if(cls === int){return int.$factory(value)}
363
return {
364
__class__: cls,
374
other = int_value(other)
375
switch(other.valueOf()) {
376
case 0:
377
return int.$factory(1)
378
case 1:
379
return int.$factory(self.valueOf())
381
if(z !== undefined && z !== null){
382
// If z is provided, the algorithm is faster than computing
383
// self ** other then applying the modulo z
384
if(z == 1){return 0}
385
var result = 1,
386
base = self % z,
387
exponent = other,
388
long_int = $B.long_int
389
while(exponent > 0){
390
if(exponent % 2 == 1){
391
if(result * base > $B.max_int){
392
result = long_int.__mul__(
393
long_int.$factory(result),
394
long_int.$factory(base))
395
result = long_int.__mod__(result, z)
396
}else{
397
result = (result * base) % z
398
}
399
}
400
exponent = exponent >> 1
401
if(base * base > $B.max_int){
402
base = long_int.__mul__(long_int.$factory(base),
403
long_int.$factory(base))
404
base = long_int.__mod__(base, z)
405
}else{
406
base = (base * base) % z
407
}
411
var res = Math.pow(self.valueOf(), other.valueOf())
412
if(res > $B.min_int && res < $B.max_int){return res}
415
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
416
$B.long_int.$factory(other)))
443
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
444
$B.long_int.$factory(other)))
446
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
447
if(rrshift !== _b_.None){return rrshift(self)}
452
if(typeof self == "number"){
453
if(int.$factory[attr] === undefined){
454
throw _b_.AttributeError.$factory(
455
"'int' object has no attribute '" + attr + "'")
457
throw _b_.AttributeError.$factory(
458
"'int' object attribute '" + attr + "' is read-only")
474
if(other.__class__ === $B.long_int){
475
return new Number(self / parseInt(other.value))
476
}
477
return new Number(self / other)
491
if(_b_.hasattr(other, "__rtruediv__")){
492
return $B.$getattr(other, "__rtruediv__")(self)
498
s = _b_.bin(self)
499
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
504
int.numerator = function(self){return self}
505
int.denominator = function(self){return int.$factory(1)}
506
int.imag = function(self){return int.$factory(0)}
507
int.real = function(self){return self}
515
if(other.__class__ === $B.long_int){
516
return $B.long_int.__sub__($B.long_int.$factory(self),
517
$B.long_int.$factory(other))
520
if(self > $B.max_int32 || self < $B.min_int32 ||
521
other > $B.max_int32 || other < $B.min_int32){
522
return $B.long_int.__sub__($B.long_int.$factory(self),
523
$B.long_int.$factory(other))
527
if(_b_.isinstance(other, _b_.bool)){return self - other}
528
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
529
if(rsub !== _b_.None){return rsub(self)}
536
var opf = $op_func.replace(/-/gm, $op)
537
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
538
eval("int.__" + $ops[$op] + "__ = " + opf)
545
if(typeof other == "number"){
546
var res = self.valueOf() - other.valueOf()
547
if(res > $B.min_int && res < $B.max_int){return res}
548
else{return $B.long_int.__sub__($B.long_int.$factory(self),
549
$B.long_int.$factory(other))}
553
return $B.long_int.__sub__($B.long_int.$factory(self),
554
$B.long_int.$factory(other))
564
var bool_value = 0;
565
if(other.valueOf()){bool_value = 1}
566
return self - bool_value
571
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
572
if(rsub !== _b_.None){return rsub(self)}
578
var opf = $op_func.replace(/-/gm, $op)
579
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
580
eval("int.__" + $ops[$op] + "__ = " + opf)
605
eval("int.__"+$B.$comps[$op] + "__ = " +
606
$comp_func.replace(/>/gm, $op).
607
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
608
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
614
var $valid_digits = function(base) {
615
var digits = ""
616
if(base === 0){return "0"}
617
if(base < 10){
633
if(typeof value == "number" &&
634
(base === undefined || base == 10)){return parseInt(value)}
640
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
641
{"base": 10}, null, null),
642
value = $ns["x"],
643
base = $ns["base"]
659
if(base == 10){
660
if(value < $B.min_int || value > $B.max_int){
661
return $B.long_int.$factory(value)
662
}
668
var res = parseInt(value, base)
669
if(value < $B.min_int || value > $B.max_int){
670
return $B.long_int.$factory(value, base)
671
}
676
if(value === true){return Number(1)}
677
if(value === false){return Number(0)}
678
if(value.__class__ === $B.long_int){
685
function invalid(value, base){
686
throw _b_.ValueError.$factory("invalid literal for int() with base " +
687
base + ": '" + _b_.str.$factory(value) + "'")
688
}
691
if(typeof value == "string") {
692
var _value = value.trim() // remove leading/trailing whitespace
693
if(_value.length == 2 && base == 0 &&
694
(_value == "0b" || _value == "0o" || _value == "0x")){
695
throw _b_.ValueError.$factory("invalid value")
696
}
697
if(_value.length >2) {
698
var _pre = _value.substr(0, 2).toUpperCase()
699
if(base == 0){
700
if(_pre == "0B"){base = 2}
701
if(_pre == "0O"){base = 8}
702
if(_pre == "0X"){base = 16}
703
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
704
else if(_pre == "0O" && base != 8){invalid(_value, base)}
715
var _digits = $valid_digits(base),
716
_re = new RegExp("^[+-]?[" + _digits + "]" +
717
"[" + _digits + "_]*$", "i"),
718
match = _re.exec(_value)
736
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
737
if(num_value === null){
738
throw _b_.TypeError.$factory(
739
"int() argument must be a string, a bytes-like " +
740
"object or a number, not '" + $B.class_name(value) + "'")
741
}
742
return num_value
745
var $trunc = $B.$getattr(value, "__trunc__", _b_.None)
746
if($trunc !== _b_.None){
748
int_func = $int
749
if(int_func === _b_.None){
750
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
754
if(_b_.isinstance(res, int)){return int_value(res)}
755
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
758
throw _b_.TypeError.$factory(
759
"int() argument must be a string, a bytes-like " +
770
if(obj === null || obj === undefined ){ return false}
771
switch(typeof obj){
772
case "boolean":
773
return obj
774
case "number":
775
case "string":
776
if(obj){return true}
777
return false
778
default:
780
var klass = obj.__class__ || $B.get_class(obj),
781
missing = {},
782
bool_method = $B.$getattr(klass, "__bool__", missing)
783
if(bool_method === missing){
784
try{return _b_.len(obj) > 0}
787
var res = $B.$call(bool_method)(obj)
788
if(res !== true && res !== false){
789
throw _b_.TypeError.$factory("__bool__ should return " +
790
"bool, returned " + $B.class_name(res))
791
}
792
return res
809
var methods = $B.op2method.subset("operations", "binary", "comparisons",
810
"boolean")
811
for(var op in methods){
812
var method = "__" + methods[op] + "__"
813
bool[method] = (function(op){
814
return function(self, other){
815
var value = self ? 1 : 0
816
if(int[op] !== undefined){
817
return int[op](value, other)
818
}
819
}
820
})(method)
824
if(_b_.isinstance(other, bool)){
825
return self && other
826
}else if(_b_.isinstance(other, int)){
827
return int.__and__(bool.__index__(self), int.__index__(other))
828
}
829
return _b_.NotImplemented
840
if(_b_.isinstance(other, bool)){
841
return self || other
842
}else if(_b_.isinstance(other, int)){
843
return int.__or__(bool.__index__(self), int.__index__(other))
844
}
845
return _b_.NotImplemented
855
if(_b_.dir(self).indexOf(attr) > -1){
856
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
857
}else{
858
var msg = "'bool' object has no attribute '" + attr + "'"
859
}
860
throw _b_.AttributeError.$factory(msg)
864
if(_b_.isinstance(other, bool)){
865
return self ^ other ? true : false
866
}else if(_b_.isinstance(other, int)){
867
return int.__xor__(bool.__index__(self), int.__index__(other))
868
}
869
return _b_.NotImplemented
872
bool.$factory = function(){
873
// Calls $B.$bool, which is used inside the generated JS code and skips
874
// arguments control.