Permalink
Mar 10, 2018
Jul 28, 2018
Mar 10, 2018
Mar 10, 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
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 11, 2018
Nov 8, 2018
Feb 11, 2018
Feb 11, 2018
Oct 27, 2018
Feb 11, 2018
Newer
100644
879 lines (782 sloc)
26.6 KB
9
function $err(op, other){
10
var msg = "unsupported operand type(s) for " + op +
11
": 'int' and '" + $B.get_class(other).__name__ + "'"
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 " +
98
$B.get_class(len).__name__)
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)
346
if(cls === undefined){
347
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
348
}else if(! isinstance(cls, _b_.type)){
349
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
350
}
351
if(cls === int){return int.$factory(value)}
352
return {
353
__class__: cls,
362
if(isinstance(other, int)){
363
other = int_value(other)
364
switch(other.valueOf()) {
365
case 0:
366
return int.$factory(1)
367
case 1:
368
return int.$factory(self.valueOf())
370
if(z !== undefined && z !== null){
371
// If z is provided, the algorithm is faster than computing
372
// self ** other then applying the modulo z
373
if(z == 1){return 0}
374
var result = 1,
375
base = self % z,
376
exponent = other,
377
long_int = $B.long_int
378
while(exponent > 0){
379
if(exponent % 2 == 1){
380
if(result * base > $B.max_int){
381
result = long_int.__mul__(
382
long_int.$factory(result),
383
long_int.$factory(base))
384
result = long_int.__mod__(result, z)
385
}else{
386
result = (result * base) % z
387
}
388
}
389
exponent = exponent >> 1
390
if(base * base > $B.max_int){
391
base = long_int.__mul__(long_int.$factory(base),
392
long_int.$factory(base))
393
base = long_int.__mod__(base, z)
394
}else{
395
base = (base * base) % z
396
}
400
var res = Math.pow(self.valueOf(), other.valueOf())
401
if(res > $B.min_int && res < $B.max_int){return res}
404
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
405
$B.long_int.$factory(other)))
419
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
420
$err("**", other)
432
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
433
$B.long_int.$factory(other)))
435
var rrshift = getattr(other, "__rrshift__", None)
436
if(rrshift !== None){return rrshift(self)}
441
if(typeof self == "number"){
442
if(int.$factory[attr] === undefined){
443
throw _b_.AttributeError.$factory(
444
"'int' object has no attribute '" + attr + "'")
446
throw _b_.AttributeError.$factory(
447
"'int' object attribute '" + attr + "' is read-only")
460
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
461
if(other.__class__ === $B.long_int){
462
return new Number(self / parseInt(other.value))
463
}
464
return new Number(self / other)
466
if(isinstance(other, _b_.float)){
467
if(!other.valueOf()){
468
throw ZeroDivisionError.$factory("division by zero")
469
}
470
return new Number(self / other)
472
if(isinstance(other, _b_.complex)){
473
var cmod = other.$real * other.$real + other.$imag * other.$imag
474
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
475
return $B.make_complex(self * other.$real / cmod,
476
-self * other.$imag / cmod)
478
if(hasattr(other, "__rtruediv__")){
479
return getattr(other, "__rtruediv__")(self)
480
}
481
$err("/", other)
493
int.numerator = function(self){return self}
494
int.denominator = function(self){return int.$factory(1)}
495
int.imag = function(self){return int.$factory(0)}
496
int.real = function(self){return self}
502
var $op_func = function(self, other){
503
if(isinstance(other, int)) {
504
if(other.__class__ === $B.long_int){
505
return $B.long_int.__sub__($B.long_int.$factory(self),
506
$B.long_int.$factory(other))
509
if(self > $B.max_int32 || self < $B.min_int32 ||
510
other > $B.max_int32 || other < $B.min_int32){
511
return $B.long_int.__sub__($B.long_int.$factory(self),
512
$B.long_int.$factory(other))
516
if(isinstance(other, _b_.bool)){return self - other}
517
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
518
$err("-", other)
524
var opf = $op_func.replace(/-/gm, $op)
525
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
526
eval("int.__" + $ops[$op] + "__ = " + opf)
533
if(typeof other == "number"){
534
var res = self.valueOf() - other.valueOf()
535
if(res > $B.min_int && res < $B.max_int){return res}
536
else{return $B.long_int.__sub__($B.long_int.$factory(self),
537
$B.long_int.$factory(other))}
541
return $B.long_int.__sub__($B.long_int.$factory(self),
542
$B.long_int.$factory(other))
548
if(isinstance(other, _b_.complex)){
549
return $B.make_complex(self - other.$real, -other.$imag)
551
if(isinstance(other, _b_.bool)){
552
var bool_value = 0;
553
if(other.valueOf()){bool_value = 1}
554
return self - bool_value
559
var rsub = $B.$getattr(other, "__rsub__", None)
560
if(rsub !== None){return rsub(self)}
561
throw $err("-", other)
566
var opf = $op_func.replace(/-/gm, $op)
567
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
568
eval("int.__" + $ops[$op] + "__ = " + opf)
576
if(isinstance(other, int)){
577
other = int_value(other)
578
return self.valueOf() > other.valueOf()
579
}else if(isinstance(other, _b_.float)){
580
return self.valueOf() > other.valueOf()
581
}else if(isinstance(other, _b_.bool)) {
593
eval("int.__"+$B.$comps[$op] + "__ = " +
594
$comp_func.replace(/>/gm, $op).
595
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
596
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
602
var $valid_digits = function(base) {
603
var digits = ""
604
if(base === 0){return "0"}
605
if(base < 10){
621
if(typeof value == "number" &&
622
(base === undefined || base == 10)){return parseInt(value)}
624
if(base !== undefined){
625
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
626
throw TypeError.$factory(
627
"int() can't convert non-string with explicit base")
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"]
639
if(isinstance(value, _b_.float) && base == 10){
640
if(value < $B.min_int || value > $B.max_int){
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
}
684
if(isinstance(value, _b_.str)){value = value.valueOf()}
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)}
699
else if(_pre == "0B" && base != 2){invalid(_value, base)
700
}
701
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
702
_value = _value.substr(2)
711
var _digits = $valid_digits(base),
712
_re = new RegExp("^[+-]?[" + _digits + "]" +
713
"[" + _digits + "_]*$", "i"),
714
match = _re.exec(_value)
732
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
733
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
734
if(hasattr(value, "__trunc__")){
735
var res = getattr(value, "__trunc__")(),
736
int_func = _b_.getattr(res, "__int__", null)
737
if(int_func === null){
738
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
746
throw _b_.TypeError.$factory(
747
"int() argument must be a string, a bytes-like " +
748
"object or a number, not '" + $B.get_class(value).__name__ + "'")
757
if(obj === null || obj === undefined ){ return false}
758
switch(typeof obj){
759
case "boolean":
760
return obj
761
case "number":
762
case "string":
763
if(obj){return true}
764
return false
765
default:
766
var missing = {},
767
bool_func = $B.$getattr(obj, "__bool__", missing)
768
if(bool_func === missing){
793
bool.__and__ = function(self, other){
794
return $B.$bool(int.__and__(self, other))
797
bool.__eq__ = function(self, other){
798
if(other === self){return True}
799
else if(typeof other == "number"){
800
return self ? other == 1 : other == 0
801
}else if(isinstance(other, _b_.int)){
802
return self ? other.$value == 1 : other.$value == 0
803
}else if(other instanceof Number){
804
return self ? other == 1 : other == 0
805
}else{
806
return false
807
}
808
//return self ? $B.$bool(other) : !$B.$bool(other)
815
bool.__ge__ = function(self,other){
816
return _b_.int.__ge__(bool.__hash__(self),other)
819
bool.__gt__ = function(self,other){
820
return _b_.int.__gt__(bool.__hash__(self),other)
851
if(_b_.dir(self).indexOf(attr) > -1){
852
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
853
}else{
854
var msg = "'bool' object has no attribute '" + attr + "'"
855
}
856
throw _b_.AttributeError.$factory(msg)
867
bool.$factory = function(){
868
// Calls $B.$bool, which is used inside the generated JS code and skips
869
// arguments control.