Permalink
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Apr 23, 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 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
May 19, 2017
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
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
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Newer
100644
805 lines (705 sloc)
24.3 KB
8
function $err(op, other){
9
var msg = "unsupported operand type(s) for " + op +
10
": 'int' and '" + $B.get_class(other).__name__ + "'"
36
signed = $.signed,
37
_bytes, _len
38
if(isinstance(x, [_b_.bytes, _b_.bytearray])){
39
_bytes = x.source
40
_len = x.source.length
41
}else{
49
case "big":
50
var num = _bytes[_len - 1]
51
var _mult = 256
52
for(var i = _len - 2; i >= 0; i--){
53
// For operations, use the functions that can take or return
54
// big integers
55
num = $B.add($B.mul(_mult, _bytes[i]), num)
56
_mult = $B.mul(_mult,256)
57
}
58
if(! signed){return num}
59
if(_bytes[0] < 128){return num}
60
return $B.sub(num, _mult)
61
case "little":
62
var num = _bytes[0]
63
if(num >= 128){num = num - 256}
64
var _mult = 256
65
for(var i = 1; i < _len; i++){
66
num = $B.add($B.mul(_mult, _bytes[i]), num)
67
_mult = $B.mul(_mult, 256)
68
}
69
if(! signed){return num}
70
if(_bytes[_len - 1] < 128){return num}
71
return $B.sub(num, _mult)
77
int.to_bytes = function(){
78
var $ = $B.args("to_bytes", 3,
79
{self: null, len: null, byteorder: null},
80
["self", "len", "byteorder"],
81
arguments, {}, "args", "kw"),
82
self = $.self,
83
len = $.len,
84
byteorder = $.byteorder,
85
kwargs = $.kw
86
if(! _b_.isinstance(len, _b_.int)){
87
throw _b_.TypeError.$factory("integer argument expected, got " +
88
$B.get_class(len).__name__)
89
}
90
if(["little", "big"].indexOf(byteorder) == -1){
91
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
92
}
93
var signed = kwargs.$string_dict["signed"] || false,
94
res = []
95
96
if(self < 0){
97
if(! signed){
98
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
99
}
100
self = Math.pow(256, len) + self
101
}
102
var value = self
103
while(true){
104
var quotient = Math.floor(value / 256),
105
rest = value - 256 * quotient
106
res.push(rest)
107
if(quotient == 0){
108
break
109
}
110
value = quotient
111
}
112
if(res.length > len){
113
throw _b_.OverflowError.$factory("int too big to convert")
114
}
115
if(byteorder == "big"){res = res.reverse()}
116
return {
117
__class__: _b_.bytes,
118
source: res
119
}
135
if(other === undefined){return self === int}
136
if(isinstance(other, int)){return self.valueOf() == other.valueOf()}
137
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
138
if(isinstance(other, _b_.complex)){
139
if(other.$imag != 0){return False}
140
return self.valueOf() == other.$real
154
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
155
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
183
if(fmt.sign !== undefined){
184
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
185
res = fmt.sign + res
186
}
187
}
206
for(var i = 0; i < nb; i++){
207
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
216
if(isinstance(other, int)){
217
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
218
return Math.floor(self / other)
220
if(isinstance(other, _b_.float)){
221
if(!other.valueOf()){
222
throw ZeroDivisionError.$factory("division by zero")
223
}
224
return Math.floor(self / other)
226
if(hasattr(other, "__rfloordiv__")){
227
return getattr(other, "__rfloordiv__")(self)
234
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
256
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
257
$B.long_int.$factory(other)))
259
var rlshift = getattr(other, "__rlshift__", None)
260
if(rlshift !== None){return rlshift(self)}
261
$err("<<", other)
266
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
267
if(isinstance(other, [int, _b_.float, bool])){
268
if(other === false){other = 0}
269
else if(other === true){other = 1}
270
if(other == 0){throw _b_.ZeroDivisionError.$factory(
274
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
275
$err("%", other)
289
if(isinstance(other, int)){
290
var res = self * other
291
if(res > $B.min_int && res < $B.max_int){return res}
292
else{
293
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
294
$B.long_int.$factory(other)))
295
}
305
return $B.make_complex(int.__mul__(self, other.$real),
306
int.__mul__(self, other.$imag))
311
var $temp = other.slice(0, other.length)
312
for(var i = 0; i < val; i++){res = res.concat($temp)}
313
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
316
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
317
$err("*", other)
323
if(cls === undefined){
324
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
325
}
334
case 0:
335
return int.$factory(1)
336
case 1:
337
return int.$factory(self.valueOf())
339
if(z !== undefined && z !== null){
340
// If z is provided, the algorithm is faster than computing
341
// self ** other then applying the modulo z
342
var res = (self % z + z) % z
349
var res = Math.pow(self.valueOf(), other.valueOf())
350
if(res > $B.min_int && res < $B.max_int){return res}
353
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
354
$B.long_int.$factory(other)))
368
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
369
$err("**", other)
380
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
381
$B.long_int.$factory(other)))
383
var rrshift = getattr(other, "__rrshift__", None)
384
if(rrshift !== None){return rrshift(self)}
389
if(typeof self == "number"){
390
if(int.$factory[attr] === undefined){
391
throw _b_.AttributeError.$factory(
392
"'int' object has no attribute '" + attr + "'")
394
throw _b_.AttributeError.$factory(
395
"'int' object attribute '" + attr + "' is read-only")
405
int.__truediv__ = function(self, other){
406
if(isinstance(other, int)){
407
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
408
if(other.__class__ === $B.long_int){
409
return new Number(self / parseInt(other.value))
410
}
411
return new Number(self / other)
413
if(isinstance(other, _b_.float)){
414
if(!other.valueOf()){
415
throw ZeroDivisionError.$factory("division by zero")
416
}
417
return new Number(self / other)
419
if(isinstance(other, _b_.complex)){
420
var cmod = other.$real * other.$real + other.$imag * other.$imag
421
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
422
return $B.make_complex(self * other.$real / cmod,
423
-self * other.$imag / cmod)
425
if(hasattr(other, "__rtruediv__")){
426
return getattr(other, "__rtruediv__")(self)
427
}
428
$err("/", other)
440
int.numerator = function(self){return self}
441
int.denominator = function(self){return int.$factory(1)}
442
int.imag = function(self){return int.$factory(0)}
443
int.real = function(self){return self}
449
var $op_func = function(self, other){
450
if(isinstance(other, int)) {
451
if(other.__class__ === $B.long_int){
452
return $B.long_int.__sub__($B.long_int.$factory(self),
453
$B.long_int.$factory(other))
455
if(self > $B.max_int32 || self < $B.min_int32 ||
456
other > $B.max_int32 || other < $B.min_int32){
457
return $B.long_int.__sub__($B.long_int.$factory(self),
458
$B.long_int.$factory(other))
462
if(isinstance(other, _b_.bool)){return self - other}
463
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
464
$err("-", other)
470
var opf = $op_func.replace(/-/gm, $op)
471
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
472
eval("int.__" + $ops[$op] + "__ = " + opf)
476
var $op_func = function(self, other){
477
if(isinstance(other, int)){
478
if(typeof other == "number"){
479
var res = self.valueOf() - other.valueOf()
480
if(res > $B.min_int && res < $B.max_int){return res}
481
else{return $B.long_int.__sub__($B.long_int.$factory(self),
482
$B.long_int.$factory(other))}
486
return $B.long_int.__sub__($B.long_int.$factory(self),
487
$B.long_int.$factory(other))
493
if(isinstance(other, _b_.complex)){
494
return $B.make_complex(self - other.$real, -other.$imag)
496
if(isinstance(other, _b_.bool)){
497
var bool_value = 0;
498
if(other.valueOf()){bool_value = 1}
499
return self - bool_value
504
var rsub = $B.$getattr(other, "__rsub__", None)
505
if(rsub !== None){return rsub(self)}
506
throw $err("-", other)
511
var opf = $op_func.replace(/-/gm, $op)
512
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
513
eval("int.__" + $ops[$op] + "__ = " + opf)
521
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
522
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
523
if(isinstance(other, _b_.bool)) {
531
var inv_op = $B.$getattr(other, "__lt__", None)
532
if(inv_op !== None){return inv_op(self)}
540
eval("int.__"+$B.$comps[$op] + "__ = " +
541
$comp_func.replace(/>/gm, $op).
542
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
543
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
549
var $valid_digits = function(base) {
550
var digits = ""
551
if(base === 0){return "0"}
552
if(base < 10){
568
if(typeof value == "number" &&
569
(base === undefined || base == 10)){return parseInt(value)}
571
if(base !== undefined){
572
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
573
throw TypeError.$factory(
574
"int() can't convert non-string with explicit base")
582
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
583
{"base": 10}, null, null),
584
value = $ns["x"],
585
base = $ns["base"]
587
if(isinstance(value, _b_.float) && base == 10){
588
if(value < $B.min_int || value > $B.max_int){
601
if(base == 10){
602
if(value < $B.min_int || value > $B.max_int){
603
return $B.long_int.$factory(value)
604
}
610
var res = parseInt(value, base)
611
if(value < $B.min_int || value > $B.max_int){
612
return $B.long_int.$factory(value, base)
613
}
618
if(value === true){return Number(1)}
619
if(value === false){return Number(0)}
620
if(value.__class__ === $B.long_int){
628
if(isinstance(value, _b_.str)){value = value.valueOf()}
629
if(typeof value == "string") {
630
var _value = value.trim() // remove leading/trailing whitespace
631
if(_value.length == 2 && base == 0 &&
632
(_value == "0b" || _value == "0o" || _value == "0x")){
633
throw _b_.ValueError.$factory("invalid value")
634
}
635
if(_value.length >2) {
636
var _pre = _value.substr(0, 2).toUpperCase()
637
if(base == 0){
638
if(_pre == "0B"){base = 2}
639
if(_pre == "0O"){base = 8}
640
if(_pre == "0X"){base = 16}
641
}
642
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
643
_value = _value.substr(2)
644
}
645
}
646
var _digits = $valid_digits(base)
647
var _re = new RegExp("^[+-]?[" + _digits + "]+$", "i")
648
if(! _re.test(_value)){
649
throw _b_.ValueError.$factory(
650
"invalid literal for int() with base " + base + ": '" +
651
_b_.str.$factory(value) + "'")
652
}
653
if(base <= 10 && ! isFinite(value)){
654
throw _b_.ValueError.$factory(
655
"invalid literal for int() with base " + base + ": '" +
656
_b_.str.$factory(value) + "'")
657
}
658
var res = parseInt(_value, base)
659
if(res < $B.min_int || res > $B.max_int){
660
return $B.long_int.$factory(_value, base)
661
}
662
return res
667
for(var i = 0; i < value.source.length; i++){
668
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
669
throw _b_.ValueError.$factory(
670
"invalid literal for int() with base " + base + ": " +
671
_b_.repr(value))
677
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
678
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
679
if(hasattr(value, "__trunc__")){
680
var res = getattr(value, "__trunc__")(),
681
int_func = _b_.getattr(res, "__int__", null)
682
if(int_func === null){
683
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
691
throw _b_.TypeError.$factory(
692
"int() argument must be a string, a bytes-like " +
693
"object or a number, not '" + $B.get_class(value).__name__ + "'")
702
if(obj === null || obj === undefined ){ return false}
703
switch(typeof obj){
704
case "boolean":
705
return obj
706
case "number":
707
case "string":
708
if(obj){return true}
709
return false
710
default:
711
var ce = $B.current_exception
712
try{return getattr(obj, "__bool__")()}
713
catch(err){
714
$B.current_exception = ce
723
__module__: "builtins",
724
__mro__: [int, object],
725
__name__: "bool",
726
$is_class: true,
727
$native: true
728
}
734
bool.__and__ = function(self, other){
735
return $B.$bool(int.__and__(self, other))
738
bool.__eq__ = function(self,other){
739
return self ? $B.$bool(other) : !$B.$bool(other)
742
bool.__ne__ = function(self,other){
743
return self ? !$B.$bool(other) : $B.$bool(other)
746
bool.__ge__ = function(self,other){
747
return _b_.int.__ge__(bool.__hash__(self),other)
750
bool.__gt__ = function(self,other){
751
return _b_.int.__gt__(bool.__hash__(self),other)
793
bool.$factory = function(){
794
// Calls $B.$bool, which is used inside the generated JS code and skips
795
// arguments control.