Permalink
Jul 28, 2018
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
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
873 lines (787 sloc)
26.8 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")
125
}
126
if(byteorder == "big"){res = res.reverse()}
127
return {
128
__class__: _b_.bytes,
129
source: res
130
}
135
int.__bool__ = function(self){
136
return int_value(self).valueOf() == 0 ? false : true
137
}
149
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
150
if(_b_.isinstance(other, _b_.complex)){
163
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
164
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
192
if(fmt.sign !== undefined){
193
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
194
res = fmt.sign + res
195
}
196
}
215
for(var i = 0; i < nb; i++){
216
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
247
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
272
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
273
$B.long_int.$factory(other)))
275
var rlshift = $B.$getattr(other, "__rlshift__", _b_.None)
276
if(rlshift !== _b_.None){return rlshift(self)}
283
if(other.__class__ === $B.long_int){
284
return $B.long_int.__mod__($B.long_int.$factory(self), other)
285
}
288
if(other === false){other = 0}
289
else if(other === true){other = 1}
290
if(other == 0){throw _b_.ZeroDivisionError.$factory(
311
var res = self * other
312
if(res > $B.min_int && res < $B.max_int){return res}
313
else{
314
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
315
$B.long_int.$factory(other)))
316
}
326
return $B.make_complex(int.__mul__(self, other.$real),
327
int.__mul__(self, other.$imag))
332
var $temp = other.slice(0, other.length)
333
for(var i = 0; i < val; i++){res = res.concat($temp)}
337
if(_b_.hasattr(other, "__rmul__")){
338
return $B.$getattr(other, "__rmul__")(self)
339
}
343
int.__ne__ = function(self, other){
344
var res = int.__eq__(self, other)
345
return (res === _b_.NotImplemented) ? res : !res
346
}
347
351
if(cls === undefined){
352
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
354
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
355
}
356
if(cls === int){return int.$factory(value)}
357
return {
358
__class__: cls,
368
other = int_value(other)
369
switch(other.valueOf()) {
370
case 0:
371
return int.$factory(1)
372
case 1:
373
return int.$factory(self.valueOf())
375
if(z !== undefined && z !== null){
376
// If z is provided, the algorithm is faster than computing
377
// self ** other then applying the modulo z
378
if(z == 1){return 0}
379
var result = 1,
380
base = self % z,
381
exponent = other,
382
long_int = $B.long_int
383
while(exponent > 0){
384
if(exponent % 2 == 1){
385
if(result * base > $B.max_int){
386
result = long_int.__mul__(
387
long_int.$factory(result),
388
long_int.$factory(base))
389
result = long_int.__mod__(result, z)
390
}else{
391
result = (result * base) % z
392
}
393
}
394
exponent = exponent >> 1
395
if(base * base > $B.max_int){
396
base = long_int.__mul__(long_int.$factory(base),
397
long_int.$factory(base))
398
base = long_int.__mod__(base, z)
399
}else{
400
base = (base * base) % z
401
}
405
var res = Math.pow(self.valueOf(), other.valueOf())
406
if(res > $B.min_int && res < $B.max_int){return res}
409
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
410
$B.long_int.$factory(other)))
437
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
438
$B.long_int.$factory(other)))
440
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
441
if(rrshift !== _b_.None){return rrshift(self)}
446
if(typeof self == "number"){
447
if(int.$factory[attr] === undefined){
448
throw _b_.AttributeError.$factory(
449
"'int' object has no attribute '" + attr + "'")
451
throw _b_.AttributeError.$factory(
452
"'int' object attribute '" + attr + "' is read-only")
468
if(other.__class__ === $B.long_int){
469
return new Number(self / parseInt(other.value))
470
}
471
return new Number(self / other)
485
if(_b_.hasattr(other, "__rtruediv__")){
486
return $B.$getattr(other, "__rtruediv__")(self)
492
s = _b_.bin(self)
493
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
498
int.numerator = function(self){return self}
499
int.denominator = function(self){return int.$factory(1)}
500
int.imag = function(self){return int.$factory(0)}
501
int.real = function(self){return self}
509
if(other.__class__ === $B.long_int){
510
return $B.long_int.__sub__($B.long_int.$factory(self),
511
$B.long_int.$factory(other))
514
if(self > $B.max_int32 || self < $B.min_int32 ||
515
other > $B.max_int32 || other < $B.min_int32){
516
return $B.long_int.__sub__($B.long_int.$factory(self),
517
$B.long_int.$factory(other))
521
if(_b_.isinstance(other, _b_.bool)){return self - other}
522
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
523
if(rsub !== _b_.None){return rsub(self)}
530
var opf = $op_func.replace(/-/gm, $op)
531
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
532
eval("int.__" + $ops[$op] + "__ = " + opf)
539
if(typeof other == "number"){
540
var res = self.valueOf() - other.valueOf()
541
if(res > $B.min_int && res < $B.max_int){return res}
542
else{return $B.long_int.__sub__($B.long_int.$factory(self),
543
$B.long_int.$factory(other))}
547
return $B.long_int.__sub__($B.long_int.$factory(self),
548
$B.long_int.$factory(other))
558
var bool_value = 0;
559
if(other.valueOf()){bool_value = 1}
560
return self - bool_value
565
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
566
if(rsub !== _b_.None){return rsub(self)}
572
var opf = $op_func.replace(/-/gm, $op)
573
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
574
eval("int.__" + $ops[$op] + "__ = " + opf)
599
eval("int.__"+$B.$comps[$op] + "__ = " +
600
$comp_func.replace(/>/gm, $op).
601
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
602
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
608
var $valid_digits = function(base) {
609
var digits = ""
610
if(base === 0){return "0"}
611
if(base < 10){
627
if(typeof value == "number" &&
628
(base === undefined || base == 10)){return parseInt(value)}
634
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
635
{"base": 10}, null, null),
636
value = $ns["x"],
637
base = $ns["base"]
653
if(base == 10){
654
if(value < $B.min_int || value > $B.max_int){
655
return $B.long_int.$factory(value)
656
}
662
var res = parseInt(value, base)
663
if(value < $B.min_int || value > $B.max_int){
664
return $B.long_int.$factory(value, base)
665
}
670
if(value === true){return Number(1)}
671
if(value === false){return Number(0)}
672
if(value.__class__ === $B.long_int){
679
function invalid(value, base){
680
throw _b_.ValueError.$factory("invalid literal for int() with base " +
681
base + ": '" + _b_.str.$factory(value) + "'")
682
}
685
if(typeof value == "string") {
686
var _value = value.trim() // remove leading/trailing whitespace
687
if(_value.length == 2 && base == 0 &&
688
(_value == "0b" || _value == "0o" || _value == "0x")){
689
throw _b_.ValueError.$factory("invalid value")
690
}
691
if(_value.length >2) {
692
var _pre = _value.substr(0, 2).toUpperCase()
693
if(base == 0){
694
if(_pre == "0B"){base = 2}
695
if(_pre == "0O"){base = 8}
696
if(_pre == "0X"){base = 16}
697
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
698
else if(_pre == "0O" && base != 8){invalid(_value, base)}
709
var _digits = $valid_digits(base),
710
_re = new RegExp("^[+-]?[" + _digits + "]" +
711
"[" + _digits + "_]*$", "i"),
712
match = _re.exec(_value)
729
var $int = $B.$getattr(value, "__int__", _b_.None)
730
if($int !== _b_.None){return $int()}
731
732
var $index = $B.$getattr(value, "__index__", _b_.None)
733
if($index !== _b_.None){return $index()}
735
var $trunc = $B.$getattr(value, "__trunc__", _b_.None)
736
if($trunc !== _b_.None){
737
var res = $truc(),
738
int_func = $int
739
if(int_func === _b_.None){
740
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
744
if(_b_.isinstance(res, int)){return int_value(res)}
745
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
748
throw _b_.TypeError.$factory(
749
"int() argument must be a string, a bytes-like " +
759
if(obj === null || obj === undefined ){ return false}
760
switch(typeof obj){
761
case "boolean":
762
return obj
763
case "number":
764
case "string":
765
if(obj){return true}
766
return false
767
default:
769
var klass = obj.__class__ || $B.get_class(obj),
770
missing = {},
771
bool_method = $B.$getattr(klass, "__bool__", missing)
772
if(bool_method === missing){
773
try{return _b_.len(obj) > 0}
776
var res = $B.$call(bool_method)(obj)
777
if(res !== true && res !== false){
778
throw _b_.TypeError.$factory("__bool__ should return " +
779
"bool, returned " + $B.class_name(res))
780
}
781
return res
798
var methods = $B.op2method.subset("operations", "binary", "comparisons",
799
"boolean")
800
for(var op in methods){
801
var method = "__" + methods[op] + "__"
802
bool[method] = (function(op){
803
return function(self, other){
804
var value = self ? 1 : 0
805
if(int[op] !== undefined){
806
return int[op](value, other)
807
}
808
}
809
})(method)
813
if(_b_.isinstance(other, bool)){
814
return self && other
815
}else if(_b_.isinstance(other, int)){
816
return int.__and__(bool.__index__(self), int.__index__(other))
817
}
818
return _b_.NotImplemented
829
if(_b_.isinstance(other, bool)){
830
return self || other
831
}else if(_b_.isinstance(other, int)){
832
return int.__or__(bool.__index__(self), int.__index__(other))
833
}
834
return _b_.NotImplemented
844
if(_b_.dir(self).indexOf(attr) > -1){
845
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
846
}else{
847
var msg = "'bool' object has no attribute '" + attr + "'"
848
}
849
throw _b_.AttributeError.$factory(msg)
853
if(_b_.isinstance(other, bool)){
854
return self ^ other ? true : false
855
}else if(_b_.isinstance(other, int)){
856
return int.__xor__(bool.__index__(self), int.__index__(other))
857
}
858
return _b_.NotImplemented
861
bool.$factory = function(){
862
// Calls $B.$bool, which is used inside the generated JS code and skips
863
// arguments control.