Permalink
Jul 28, 2018
Mar 10, 2018
Mar 10, 2018
Apr 23, 2018
Apr 23, 2018
Jul 28, 2018
Jul 28, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Aug 2, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Mar 10, 2018
Aug 2, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Jul 28, 2018
Jul 28, 2018
May 19, 2017
Jul 28, 2018
Mar 10, 2018
Feb 11, 2018
Mar 10, 2018
Feb 11, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Mar 23, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 11, 2018
Feb 11, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Jul 28, 2018
Mar 10, 2018
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
Mar 10, 2018
Jun 14, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Oct 15, 2018
Feb 22, 2019
Feb 11, 2018
Oct 27, 2018
Feb 11, 2018
Newer
100644
855 lines (766 sloc)
26 KB
15
function int_value(obj){
16
// Instances of int subclasses that call int.__new__(cls, value)
17
// have an attribute $value set
18
return obj.$value !== undefined ? obj.$value : obj
19
}
20
46
signed = $.signed,
47
_bytes, _len
48
if(isinstance(x, [_b_.bytes, _b_.bytearray])){
49
_bytes = x.source
50
_len = x.source.length
51
}else{
59
case "big":
60
var num = _bytes[_len - 1]
61
var _mult = 256
62
for(var i = _len - 2; i >= 0; i--){
63
// For operations, use the functions that can take or return
64
// big integers
65
num = $B.add($B.mul(_mult, _bytes[i]), num)
66
_mult = $B.mul(_mult,256)
67
}
68
if(! signed){return num}
69
if(_bytes[0] < 128){return num}
70
return $B.sub(num, _mult)
71
case "little":
72
var num = _bytes[0]
73
if(num >= 128){num = num - 256}
74
var _mult = 256
75
for(var i = 1; i < _len; i++){
76
num = $B.add($B.mul(_mult, _bytes[i]), num)
77
_mult = $B.mul(_mult, 256)
78
}
79
if(! signed){return num}
80
if(_bytes[_len - 1] < 128){return num}
81
return $B.sub(num, _mult)
87
int.to_bytes = function(){
88
var $ = $B.args("to_bytes", 3,
89
{self: null, len: null, byteorder: null},
90
["self", "len", "byteorder"],
91
arguments, {}, "args", "kw"),
92
self = $.self,
93
len = $.len,
94
byteorder = $.byteorder,
95
kwargs = $.kw
96
if(! _b_.isinstance(len, _b_.int)){
97
throw _b_.TypeError.$factory("integer argument expected, got " +
99
}
100
if(["little", "big"].indexOf(byteorder) == -1){
101
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
102
}
103
var signed = kwargs.$string_dict["signed"] || false,
104
res = []
105
106
if(self < 0){
107
if(! signed){
108
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
109
}
110
self = Math.pow(256, len) + self
111
}
112
var value = self
113
while(true){
114
var quotient = Math.floor(value / 256),
115
rest = value - 256 * quotient
116
res.push(rest)
117
if(quotient == 0){
118
break
119
}
120
value = quotient
121
}
122
if(res.length > len){
123
throw _b_.OverflowError.$factory("int too big to convert")
124
}
125
if(byteorder == "big"){res = res.reverse()}
126
return {
127
__class__: _b_.bytes,
128
source: res
129
}
137
int.__bool__ = function(self){
138
return int_value(self).valueOf() == 0 ? false : true
139
}
148
if(isinstance(other, int)){
149
return self.valueOf() == int_value(other).valueOf()
150
}
151
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
152
if(isinstance(other, _b_.complex)){
153
if(other.$imag != 0){return False}
154
return self.valueOf() == other.$real
165
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
166
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
194
if(fmt.sign !== undefined){
195
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
196
res = fmt.sign + res
197
}
198
}
217
for(var i = 0; i < nb; i++){
218
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
227
if(other.__class__ == $B.long_int){
228
return $B.long_int.__floordiv__($B.long_int.$factory(self), other)
229
}
232
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
233
return Math.floor(self / other)
235
if(isinstance(other, _b_.float)){
236
if(!other.valueOf()){
237
throw ZeroDivisionError.$factory("division by zero")
238
}
239
return Math.floor(self / other)
241
if(hasattr(other, "__rfloordiv__")){
242
return getattr(other, "__rfloordiv__")(self)
249
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
274
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
275
$B.long_int.$factory(other)))
277
var rlshift = getattr(other, "__rlshift__", None)
278
if(rlshift !== None){return rlshift(self)}
279
$err("<<", other)
285
if(other.__class__ === $B.long_int){
286
return $B.long_int.__mod__($B.long_int.$factory(self), other)
287
}
290
if(other === false){other = 0}
291
else if(other === true){other = 1}
292
if(other == 0){throw _b_.ZeroDivisionError.$factory(
296
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
297
$err("%", other)
313
var res = self * other
314
if(res > $B.min_int && res < $B.max_int){return res}
315
else{
316
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
317
$B.long_int.$factory(other)))
318
}
328
return $B.make_complex(int.__mul__(self, other.$real),
329
int.__mul__(self, other.$imag))
334
var $temp = other.slice(0, other.length)
335
for(var i = 0; i < val; i++){res = res.concat($temp)}
336
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
339
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
340
$err("*", other)
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")
353
}else if(! isinstance(cls, _b_.type)){
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,
367
if(isinstance(other, int)){
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)))
424
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
425
$err("**", other)
437
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
438
$B.long_int.$factory(other)))
440
var rrshift = getattr(other, "__rrshift__", None)
441
if(rrshift !== 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")
465
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
466
if(other.__class__ === $B.long_int){
467
return new Number(self / parseInt(other.value))
468
}
469
return new Number(self / other)
471
if(isinstance(other, _b_.float)){
472
if(!other.valueOf()){
473
throw ZeroDivisionError.$factory("division by zero")
474
}
475
return new Number(self / other)
477
if(isinstance(other, _b_.complex)){
478
var cmod = other.$real * other.$real + other.$imag * other.$imag
479
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
480
return $B.make_complex(self * other.$real / cmod,
481
-self * other.$imag / cmod)
483
if(hasattr(other, "__rtruediv__")){
484
return getattr(other, "__rtruediv__")(self)
485
}
486
$err("/", other)
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}
507
var $op_func = function(self, other){
508
if(isinstance(other, int)) {
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(isinstance(other, _b_.bool)){return self - other}
522
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
523
$err("-", other)
529
var opf = $op_func.replace(/-/gm, $op)
530
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
531
eval("int.__" + $ops[$op] + "__ = " + opf)
538
if(typeof other == "number"){
539
var res = self.valueOf() - other.valueOf()
540
if(res > $B.min_int && res < $B.max_int){return res}
541
else{return $B.long_int.__sub__($B.long_int.$factory(self),
542
$B.long_int.$factory(other))}
546
return $B.long_int.__sub__($B.long_int.$factory(self),
547
$B.long_int.$factory(other))
553
if(isinstance(other, _b_.complex)){
554
return $B.make_complex(self - other.$real, -other.$imag)
556
if(isinstance(other, _b_.bool)){
557
var bool_value = 0;
558
if(other.valueOf()){bool_value = 1}
559
return self - bool_value
564
var rsub = $B.$getattr(other, "__rsub__", None)
565
if(rsub !== None){return rsub(self)}
566
throw $err("-", other)
571
var opf = $op_func.replace(/-/gm, $op)
572
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
573
eval("int.__" + $ops[$op] + "__ = " + opf)
581
if(isinstance(other, int)){
582
other = int_value(other)
583
return self.valueOf() > other.valueOf()
584
}else if(isinstance(other, _b_.float)){
585
return self.valueOf() > other.valueOf()
586
}else if(isinstance(other, _b_.bool)) {
598
eval("int.__"+$B.$comps[$op] + "__ = " +
599
$comp_func.replace(/>/gm, $op).
600
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
601
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
607
var $valid_digits = function(base) {
608
var digits = ""
609
if(base === 0){return "0"}
610
if(base < 10){
626
if(typeof value == "number" &&
627
(base === undefined || base == 10)){return parseInt(value)}
629
if(base !== undefined){
630
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
631
throw TypeError.$factory(
632
"int() can't convert non-string with explicit base")
639
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
640
{"base": 10}, null, null),
641
value = $ns["x"],
642
base = $ns["base"]
644
if(isinstance(value, _b_.float) && base == 10){
645
if(value < $B.min_int || value > $B.max_int){
658
if(base == 10){
659
if(value < $B.min_int || value > $B.max_int){
660
return $B.long_int.$factory(value)
661
}
667
var res = parseInt(value, base)
668
if(value < $B.min_int || value > $B.max_int){
669
return $B.long_int.$factory(value, base)
670
}
675
if(value === true){return Number(1)}
676
if(value === false){return Number(0)}
677
if(value.__class__ === $B.long_int){
684
function invalid(value, base){
685
throw _b_.ValueError.$factory("invalid literal for int() with base " +
686
base + ": '" + _b_.str.$factory(value) + "'")
687
}
689
if(isinstance(value, _b_.str)){value = value.valueOf()}
690
if(typeof value == "string") {
691
var _value = value.trim() // remove leading/trailing whitespace
692
if(_value.length == 2 && base == 0 &&
693
(_value == "0b" || _value == "0o" || _value == "0x")){
694
throw _b_.ValueError.$factory("invalid value")
695
}
696
if(_value.length >2) {
697
var _pre = _value.substr(0, 2).toUpperCase()
698
if(base == 0){
699
if(_pre == "0B"){base = 2}
700
if(_pre == "0O"){base = 8}
701
if(_pre == "0X"){base = 16}
702
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
703
else if(_pre == "0O" && base != 8){invalid(_value, base)}
704
else if(_pre == "0B" && base != 2){invalid(_value, base)
705
}
706
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
707
_value = _value.substr(2)
716
var _digits = $valid_digits(base),
717
_re = new RegExp("^[+-]?[" + _digits + "]" +
718
"[" + _digits + "_]*$", "i"),
719
match = _re.exec(_value)
737
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
738
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
739
if(hasattr(value, "__trunc__")){
740
var res = getattr(value, "__trunc__")(),
741
int_func = _b_.getattr(res, "__int__", null)
742
if(int_func === null){
743
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
751
throw _b_.TypeError.$factory(
752
"int() argument must be a string, a bytes-like " +
762
if(obj === null || obj === undefined ){ return false}
763
switch(typeof obj){
764
case "boolean":
765
return obj
766
case "number":
767
case "string":
768
if(obj){return true}
769
return false
770
default:
772
var missing = {},
773
bool_func = $B.$getattr(obj, "__bool__", missing)
774
if(bool_func === missing){
795
var methods = $B.op2method.subset("operations", "binary", "comparisons",
796
"boolean")
797
for(var op in methods){
798
var method = "__" + methods[op] + "__"
799
bool[method] = (function(op){
800
return function(self, other){
801
var value = self ? 1 : 0
802
if(int[op] !== undefined){
803
return int[op](value, other)
804
}
805
}
806
})(method)
809
bool.__and__ = function(self, other){
810
return $B.$bool(int.__and__(self, other))
831
if(_b_.dir(self).indexOf(attr) > -1){
832
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
833
}else{
834
var msg = "'bool' object has no attribute '" + attr + "'"
835
}
836
throw _b_.AttributeError.$factory(msg)
843
bool.$factory = function(){
844
// Calls $B.$bool, which is used inside the generated JS code and skips
845
// arguments control.