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
879 lines (793 sloc)
27 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)}
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)
735
var $int = $B.$getattr(value, "__int__", _b_.None)
736
if($int !== _b_.None){return $int()}
737
738
var $index = $B.$getattr(value, "__index__", _b_.None)
739
if($index !== _b_.None){return $index()}
741
var $trunc = $B.$getattr(value, "__trunc__", _b_.None)
742
if($trunc !== _b_.None){
743
var res = $truc(),
744
int_func = $int
745
if(int_func === _b_.None){
746
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
750
if(_b_.isinstance(res, int)){return int_value(res)}
751
throw _b_.TypeError.$factory("__trunc__ returned non-Integral (type "+
754
throw _b_.TypeError.$factory(
755
"int() argument must be a string, a bytes-like " +
765
if(obj === null || obj === undefined ){ return false}
766
switch(typeof obj){
767
case "boolean":
768
return obj
769
case "number":
770
case "string":
771
if(obj){return true}
772
return false
773
default:
775
var klass = obj.__class__ || $B.get_class(obj),
776
missing = {},
777
bool_method = $B.$getattr(klass, "__bool__", missing)
778
if(bool_method === missing){
779
try{return _b_.len(obj) > 0}
782
var res = $B.$call(bool_method)(obj)
783
if(res !== true && res !== false){
784
throw _b_.TypeError.$factory("__bool__ should return " +
785
"bool, returned " + $B.class_name(res))
786
}
787
return res
804
var methods = $B.op2method.subset("operations", "binary", "comparisons",
805
"boolean")
806
for(var op in methods){
807
var method = "__" + methods[op] + "__"
808
bool[method] = (function(op){
809
return function(self, other){
810
var value = self ? 1 : 0
811
if(int[op] !== undefined){
812
return int[op](value, other)
813
}
814
}
815
})(method)
819
if(_b_.isinstance(other, bool)){
820
return self && other
821
}else if(_b_.isinstance(other, int)){
822
return int.__and__(bool.__index__(self), int.__index__(other))
823
}
824
return _b_.NotImplemented
835
if(_b_.isinstance(other, bool)){
836
return self || other
837
}else if(_b_.isinstance(other, int)){
838
return int.__or__(bool.__index__(self), int.__index__(other))
839
}
840
return _b_.NotImplemented
850
if(_b_.dir(self).indexOf(attr) > -1){
851
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
852
}else{
853
var msg = "'bool' object has no attribute '" + attr + "'"
854
}
855
throw _b_.AttributeError.$factory(msg)
859
if(_b_.isinstance(other, bool)){
860
return self ^ other ? true : false
861
}else if(_b_.isinstance(other, int)){
862
return int.__xor__(bool.__index__(self), int.__index__(other))
863
}
864
return _b_.NotImplemented
867
bool.$factory = function(){
868
// Calls $B.$bool, which is used inside the generated JS code and skips
869
// arguments control.