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
Feb 15, 2020
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Feb 22, 2019
Feb 22, 2019
Mar 10, 2018
Jul 28, 2018
Feb 20, 2020
Jul 28, 2018
May 19, 2017
Jul 28, 2018
Feb 20, 2020
Jul 28, 2018
Mar 10, 2018
Feb 11, 2018
Feb 15, 2020
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
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
889 lines (802 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
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(
300
if(_b_.hasattr(other, "__rmod__")){
301
return $B.$getattr(other, "__rmod__")(self)
302
}
319
var res = self * other
320
if(res > $B.min_int && res < $B.max_int){return res}
321
else{
322
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
323
$B.long_int.$factory(other)))
324
}
334
return $B.make_complex(int.__mul__(self, other.$real),
335
int.__mul__(self, other.$imag))
340
var $temp = other.slice(0, other.length)
341
for(var i = 0; i < val; i++){res = res.concat($temp)}
345
if(_b_.hasattr(other, "__rmul__")){
346
return $B.$getattr(other, "__rmul__")(self)
347
}
351
int.__ne__ = function(self, other){
352
var res = int.__eq__(self, other)
353
return (res === _b_.NotImplemented) ? res : !res
354
}
355
359
if(cls === undefined){
360
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
362
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
363
}
364
if(cls === int){return int.$factory(value)}
365
return {
366
__class__: cls,
374
function extended_euclidean(a, b){
375
var d, u, v
376
if(b == 0){
377
return [a, 1, 0]
378
}else{
379
[d, u, v] = extended_euclidean(b, a % b)
380
return [d, v, u - Math.floor(a / b) * v]
381
}
382
}
383
386
other = int_value(other)
387
switch(other.valueOf()) {
388
case 0:
389
return int.$factory(1)
390
case 1:
391
return int.$factory(self.valueOf())
394
// If z is provided, the algorithm is faster than computing
395
// self ** other then applying the modulo z
396
if(z == 1){return 0}
397
var result = 1,
398
base = self % z,
399
exponent = other,
400
long_int = $B.long_int
401
if(exponent < 0){
402
var gcd, inv, _
403
[gcd, inv, _] = extended_euclidean(self, z)
404
if(gcd !== 1){
405
throw _b_.ValueError.$factory("not relative primes: " +
406
self + ' and ' + z)
407
}
408
return int.__pow__(inv, -exponent, z)
409
}
410
while(exponent > 0){
411
if(exponent % 2 == 1){
412
if(result * base > $B.max_int){
413
result = long_int.__mul__(
414
long_int.$factory(result),
415
long_int.$factory(base))
416
result = long_int.__mod__(result, z)
417
}else{
418
result = (result * base) % z
419
}
420
}
421
exponent = exponent >> 1
422
if(base * base > $B.max_int){
423
base = long_int.__mul__(long_int.$factory(base),
424
long_int.$factory(base))
425
base = long_int.__mod__(base, z)
426
}else{
427
base = (base * base) % z
428
}
432
var res = Math.pow(self.valueOf(), other.valueOf())
433
if(res > $B.min_int && res < $B.max_int){return res}
436
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
437
$B.long_int.$factory(other)))
451
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
452
if(rpow !== _b_.None){
453
return rpow(self)
454
}
467
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
468
$B.long_int.$factory(other)))
470
var rrshift = $B.$getattr(other, "__rrshift__", _b_.None)
471
if(rrshift !== _b_.None){return rrshift(self)}
476
if(typeof self == "number"){
477
if(int.$factory[attr] === undefined){
478
throw _b_.AttributeError.$factory(
479
"'int' object has no attribute '" + attr + "'")
481
throw _b_.AttributeError.$factory(
482
"'int' object attribute '" + attr + "' is read-only")
498
if(other.__class__ === $B.long_int){
499
return new Number(self / parseInt(other.value))
500
}
501
return new Number(self / other)
515
if(_b_.hasattr(other, "__rtruediv__")){
516
return $B.$getattr(other, "__rtruediv__")(self)
522
s = _b_.bin(self)
523
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
528
int.numerator = function(self){return self}
529
int.denominator = function(self){return int.$factory(1)}
530
int.imag = function(self){return int.$factory(0)}
531
int.real = function(self){return self}
539
if(other.__class__ === $B.long_int){
540
return $B.long_int.__sub__($B.long_int.$factory(self),
541
$B.long_int.$factory(other))
544
if(self > $B.max_int32 || self < $B.min_int32 ||
545
other > $B.max_int32 || other < $B.min_int32){
546
return $B.long_int.__sub__($B.long_int.$factory(self),
547
$B.long_int.$factory(other))
551
if(_b_.isinstance(other, _b_.bool)){return self - other}
552
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
553
if(rsub !== _b_.None){return rsub(self)}
560
var opf = $op_func.replace(/-/gm, $op)
561
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
562
eval("int.__" + $ops[$op] + "__ = " + opf)
569
if(typeof other == "number"){
570
var res = self.valueOf() - other.valueOf()
571
if(res > $B.min_int && res < $B.max_int){return res}
572
else{return $B.long_int.__sub__($B.long_int.$factory(self),
573
$B.long_int.$factory(other))}
577
return $B.long_int.__sub__($B.long_int.$factory(self),
578
$B.long_int.$factory(other))
588
var bool_value = 0;
589
if(other.valueOf()){bool_value = 1}
590
return self - bool_value
595
var rsub = $B.$getattr(other, "__rsub__", _b_.None)
596
if(rsub !== _b_.None){return rsub(self)}
602
var opf = $op_func.replace(/-/gm, $op)
603
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
604
eval("int.__" + $ops[$op] + "__ = " + opf)
629
eval("int.__"+$B.$comps[$op] + "__ = " +
630
$comp_func.replace(/>/gm, $op).
631
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
632
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
638
var $valid_digits = function(base) {
639
var digits = ""
640
if(base === 0){return "0"}
641
if(base < 10){
657
if(typeof value == "number" &&
658
(base === undefined || base == 10)){return parseInt(value)}
664
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
665
{"base": 10}, null, null),
666
value = $ns["x"],
667
base = $ns["base"]
683
if(base == 10){
684
if(value < $B.min_int || value > $B.max_int){
685
return $B.long_int.$factory(value)
686
}
692
var res = parseInt(value, base)
693
if(value < $B.min_int || value > $B.max_int){
694
return $B.long_int.$factory(value, base)
695
}
700
if(value === true){return Number(1)}
701
if(value === false){return Number(0)}
702
if(value.__class__ === $B.long_int){
709
function invalid(value, base){
710
throw _b_.ValueError.$factory("invalid literal for int() with base " +
711
base + ": '" + _b_.str.$factory(value) + "'")
712
}
715
if(typeof value == "string") {
716
var _value = value.trim() // remove leading/trailing whitespace
717
if(_value.length == 2 && base == 0 &&
718
(_value == "0b" || _value == "0o" || _value == "0x")){
719
throw _b_.ValueError.$factory("invalid value")
720
}
721
if(_value.length >2) {
722
var _pre = _value.substr(0, 2).toUpperCase()
723
if(base == 0){
724
if(_pre == "0B"){base = 2}
725
if(_pre == "0O"){base = 8}
726
if(_pre == "0X"){base = 16}
727
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
728
else if(_pre == "0O" && base != 8){invalid(_value, base)}
739
var _digits = $valid_digits(base),
740
_re = new RegExp("^[+-]?[" + _digits + "]" +
741
"[" + _digits + "_]*$", "i"),
742
match = _re.exec(_value)
760
var num_value = $B.to_num(value, ["__int__", "__index__", "__trunc__"])
761
if(num_value === null){
762
throw _b_.TypeError.$factory(
763
"int() argument must be a string, a bytes-like " +
764
"object or a number, not '" + $B.class_name(value) + "'")
765
}
766
return num_value
775
if(obj === null || obj === undefined ){ return false}
776
switch(typeof obj){
777
case "boolean":
778
return obj
779
case "number":
780
case "string":
781
if(obj){return true}
782
return false
783
default:
785
var klass = obj.__class__ || $B.get_class(obj),
786
missing = {},
787
bool_method = $B.$getattr(klass, "__bool__", missing)
788
if(bool_method === missing){
789
try{return _b_.len(obj) > 0}
792
var res = $B.$call(bool_method)(obj)
793
if(res !== true && res !== false){
794
throw _b_.TypeError.$factory("__bool__ should return " +
795
"bool, returned " + $B.class_name(res))
796
}
797
return res
814
var methods = $B.op2method.subset("operations", "binary", "comparisons",
815
"boolean")
816
for(var op in methods){
817
var method = "__" + methods[op] + "__"
818
bool[method] = (function(op){
819
return function(self, other){
820
var value = self ? 1 : 0
821
if(int[op] !== undefined){
822
return int[op](value, other)
823
}
824
}
825
})(method)
829
if(_b_.isinstance(other, bool)){
830
return self && other
831
}else if(_b_.isinstance(other, int)){
832
return int.__and__(bool.__index__(self), int.__index__(other))
833
}
834
return _b_.NotImplemented
845
if(_b_.isinstance(other, bool)){
846
return self || other
847
}else if(_b_.isinstance(other, int)){
848
return int.__or__(bool.__index__(self), int.__index__(other))
849
}
850
return _b_.NotImplemented
860
if(_b_.dir(self).indexOf(attr) > -1){
861
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
862
}else{
863
var msg = "'bool' object has no attribute '" + attr + "'"
864
}
865
throw _b_.AttributeError.$factory(msg)
869
if(_b_.isinstance(other, bool)){
870
return self ^ other ? true : false
871
}else if(_b_.isinstance(other, int)){
872
return int.__xor__(bool.__index__(self), int.__index__(other))
873
}
874
return _b_.NotImplemented
877
bool.$factory = function(){
878
// Calls $B.$bool, which is used inside the generated JS code and skips
879
// arguments control.