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
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
795 lines (693 sloc)
24 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 +
167
case "o":
168
return (fmt.alternate ? "0o" : "") + self.toString(8)
169
case "x":
170
return (fmt.alternate ? "0x" : "") + self.toString(16)
171
case "X":
172
return (fmt.alternate ? "0X" : "") + self.toString(16).toUpperCase()
173
case "n":
195
for(var i = 0; i < nb; i++){
196
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
205
if(isinstance(other, int)){
206
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
207
return Math.floor(self / other)
209
if(isinstance(other, _b_.float)){
210
if(!other.valueOf()){
211
throw ZeroDivisionError.$factory("division by zero")
212
}
213
return Math.floor(self / other)
215
if(hasattr(other, "__rfloordiv__")){
216
return getattr(other, "__rfloordiv__")(self)
223
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
245
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
246
$B.long_int.$factory(other)))
248
var rlshift = getattr(other, "__rlshift__", None)
249
if(rlshift !== None){return rlshift(self)}
250
$err("<<", other)
255
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
256
if(isinstance(other, [int, _b_.float, bool])){
257
if(other === false){other = 0}
258
else if(other === true){other = 1}
259
if(other == 0){throw _b_.ZeroDivisionError.$factory(
263
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
264
$err("%", other)
278
if(isinstance(other, int)){
279
var res = self * other
280
if(res > $B.min_int && res < $B.max_int){return res}
281
else{
282
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
283
$B.long_int.$factory(other)))
284
}
294
return $B.make_complex(int.__mul__(self, other.$real),
295
int.__mul__(self, other.$imag))
300
var $temp = other.slice(0, other.length)
301
for(var i = 0; i < val; i++){res = res.concat($temp)}
302
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
305
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
306
$err("*", other)
312
if(cls === undefined){
313
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
314
}
323
case 0:
324
return int.$factory(1)
325
case 1:
326
return int.$factory(self.valueOf())
328
if(z !== undefined && z !== null){
329
// If z is provided, the algorithm is faster than computing
330
// self ** other then applying the modulo z
331
var res = (self % z + z) % z
338
var res = Math.pow(self.valueOf(), other.valueOf())
339
if(res > $B.min_int && res < $B.max_int){return res}
342
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
343
$B.long_int.$factory(other)))
357
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
358
$err("**", other)
369
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
370
$B.long_int.$factory(other)))
372
var rrshift = getattr(other, "__rrshift__", None)
373
if(rrshift !== None){return rrshift(self)}
378
if(typeof self == "number"){
379
if(int.$factory[attr] === undefined){
380
throw _b_.AttributeError.$factory(
381
"'int' object has no attribute '" + attr + "'")
383
throw _b_.AttributeError.$factory(
384
"'int' object attribute '" + attr + "' is read-only")
394
int.__truediv__ = function(self, other){
395
if(isinstance(other, int)){
396
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
397
if(other.__class__ === $B.long_int){
398
return new Number(self / parseInt(other.value))
399
}
400
return new Number(self / other)
402
if(isinstance(other, _b_.float)){
403
if(!other.valueOf()){
404
throw ZeroDivisionError.$factory("division by zero")
405
}
406
return new Number(self / other)
408
if(isinstance(other, _b_.complex)){
409
var cmod = other.$real * other.$real + other.$imag * other.$imag
410
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
411
return $B.make_complex(self * other.$real / cmod,
412
-self * other.$imag / cmod)
414
if(hasattr(other, "__rtruediv__")){
415
return getattr(other, "__rtruediv__")(self)
416
}
417
$err("/", other)
429
int.numerator = function(self){return self}
430
int.denominator = function(self){return int.$factory(1)}
431
int.imag = function(self){return int.$factory(0)}
432
int.real = function(self){return self}
438
var $op_func = function(self, other){
439
if(isinstance(other, int)) {
440
if(other.__class__ === $B.long_int){
441
return $B.long_int.__sub__($B.long_int.$factory(self),
442
$B.long_int.$factory(other))
444
if(self > $B.max_int32 || self < $B.min_int32 ||
445
other > $B.max_int32 || other < $B.min_int32){
446
return $B.long_int.__sub__($B.long_int.$factory(self),
447
$B.long_int.$factory(other))
451
if(isinstance(other, _b_.bool)){return self - other}
452
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
453
$err("-", other)
459
var opf = $op_func.replace(/-/gm, $op)
460
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
461
eval("int.__" + $ops[$op] + "__ = " + opf)
465
var $op_func = function(self, other){
466
if(isinstance(other, int)){
467
if(typeof other == "number"){
468
var res = self.valueOf() - other.valueOf()
469
if(res > $B.min_int && res < $B.max_int){return res}
470
else{return $B.long_int.__sub__($B.long_int.$factory(self),
471
$B.long_int.$factory(other))}
475
return $B.long_int.__sub__($B.long_int.$factory(self),
476
$B.long_int.$factory(other))
482
if(isinstance(other, _b_.complex)){
483
return $B.make_complex(self - other.$real, -other.$imag)
485
if(isinstance(other, _b_.bool)){
486
var bool_value = 0;
487
if(other.valueOf()){bool_value = 1}
488
return self - bool_value
493
var rsub = $B.$getattr(other, "__rsub__", None)
494
if(rsub !== None){return rsub(self)}
495
throw $err("-", other)
500
var opf = $op_func.replace(/-/gm, $op)
501
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
502
eval("int.__" + $ops[$op] + "__ = " + opf)
510
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
511
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
512
if(isinstance(other, _b_.bool)) {
520
var inv_op = $B.$getattr(other, "__lt__", None)
521
if(inv_op !== None){return inv_op(self)}
529
eval("int.__"+$B.$comps[$op] + "__ = " +
530
$comp_func.replace(/>/gm, $op).
531
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
532
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
538
var $valid_digits = function(base) {
539
var digits = ""
540
if(base === 0){return "0"}
541
if(base < 10){
557
if(typeof value == "number" &&
558
(base === undefined || base == 10)){return parseInt(value)}
560
if(base !== undefined){
561
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
562
throw TypeError.$factory(
563
"int() can't convert non-string with explicit base")
571
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
572
{"base": 10}, null, null),
573
value = $ns["x"],
574
base = $ns["base"]
576
if(isinstance(value, _b_.float) && base == 10){
577
if(value < $B.min_int || value > $B.max_int){
590
if(base == 10){
591
if(value < $B.min_int || value > $B.max_int){
592
return $B.long_int.$factory(value)
593
}
599
var res = parseInt(value, base)
600
if(value < $B.min_int || value > $B.max_int){
601
return $B.long_int.$factory(value, base)
602
}
607
if(value === true){return Number(1)}
608
if(value === false){return Number(0)}
609
if(value.__class__ === $B.long_int){
617
if(isinstance(value, _b_.str)){value = value.valueOf()}
618
if(typeof value == "string") {
619
var _value = value.trim() // remove leading/trailing whitespace
620
if(_value.length == 2 && base == 0 &&
621
(_value == "0b" || _value == "0o" || _value == "0x")){
622
throw _b_.ValueError.$factory("invalid value")
623
}
624
if(_value.length >2) {
625
var _pre = _value.substr(0, 2).toUpperCase()
626
if(base == 0){
627
if(_pre == "0B"){base = 2}
628
if(_pre == "0O"){base = 8}
629
if(_pre == "0X"){base = 16}
630
}
631
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
632
_value = _value.substr(2)
633
}
634
}
635
var _digits = $valid_digits(base)
636
var _re = new RegExp("^[+-]?[" + _digits + "]+$", "i")
637
if(! _re.test(_value)){
638
throw _b_.ValueError.$factory(
639
"invalid literal for int() with base " + base + ": '" +
640
_b_.str.$factory(value) + "'")
641
}
642
if(base <= 10 && ! isFinite(value)){
643
throw _b_.ValueError.$factory(
644
"invalid literal for int() with base " + base + ": '" +
645
_b_.str.$factory(value) + "'")
646
}
647
var res = parseInt(_value, base)
648
if(res < $B.min_int || res > $B.max_int){
649
return $B.long_int.$factory(_value, base)
650
}
651
return res
656
for(var i = 0; i < value.source.length; i++){
657
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
658
throw _b_.ValueError.$factory(
659
"invalid literal for int() with base " + base + ": " +
660
_b_.repr(value))
666
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
667
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
668
if(hasattr(value, "__trunc__")){
669
var res = getattr(value, "__trunc__")(),
670
int_func = _b_.getattr(res, "__int__", null)
671
if(int_func === null){
672
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
680
throw _b_.TypeError.$factory(
681
"int() argument must be a string, a bytes-like " +
682
"object or a number, not '" + $B.get_class(value).__name__ + "'")
691
if(obj === null || obj === undefined ){ return false}
692
switch(typeof obj){
693
case "boolean":
694
return obj
695
case "number":
696
case "string":
697
if(obj){return true}
698
return false
699
default:
700
var ce = $B.current_exception
701
try{return getattr(obj, "__bool__")()}
702
catch(err){
703
$B.current_exception = ce
712
__module__: "builtins",
713
__mro__: [int, object],
714
__name__: "bool",
715
$is_class: true,
716
$native: true
717
}
723
bool.__and__ = function(self, other){
724
return $B.$bool(int.__and__(self, other))
727
bool.__eq__ = function(self,other){
728
return self ? $B.$bool(other) : !$B.$bool(other)
731
bool.__ne__ = function(self,other){
732
return self ? !$B.$bool(other) : $B.$bool(other)
735
bool.__ge__ = function(self,other){
736
return _b_.int.__ge__(bool.__hash__(self),other)
739
bool.__gt__ = function(self,other){
740
return _b_.int.__gt__(bool.__hash__(self),other)
782
bool.$factory = function(){
783
// Calls $B.$bool, which is used inside the generated JS code and skips
784
// arguments control.