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
Jun 14, 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
811 lines (711 sloc)
24.4 KB
9
function $err(op, other){
10
var msg = "unsupported operand type(s) for " + op +
11
": 'int' and '" + $B.get_class(other).__name__ + "'"
37
signed = $.signed,
38
_bytes, _len
39
if(isinstance(x, [_b_.bytes, _b_.bytearray])){
40
_bytes = x.source
41
_len = x.source.length
42
}else{
50
case "big":
51
var num = _bytes[_len - 1]
52
var _mult = 256
53
for(var i = _len - 2; i >= 0; i--){
54
// For operations, use the functions that can take or return
55
// big integers
56
num = $B.add($B.mul(_mult, _bytes[i]), num)
57
_mult = $B.mul(_mult,256)
58
}
59
if(! signed){return num}
60
if(_bytes[0] < 128){return num}
61
return $B.sub(num, _mult)
62
case "little":
63
var num = _bytes[0]
64
if(num >= 128){num = num - 256}
65
var _mult = 256
66
for(var i = 1; i < _len; i++){
67
num = $B.add($B.mul(_mult, _bytes[i]), num)
68
_mult = $B.mul(_mult, 256)
69
}
70
if(! signed){return num}
71
if(_bytes[_len - 1] < 128){return num}
72
return $B.sub(num, _mult)
78
int.to_bytes = function(){
79
var $ = $B.args("to_bytes", 3,
80
{self: null, len: null, byteorder: null},
81
["self", "len", "byteorder"],
82
arguments, {}, "args", "kw"),
83
self = $.self,
84
len = $.len,
85
byteorder = $.byteorder,
86
kwargs = $.kw
87
if(! _b_.isinstance(len, _b_.int)){
88
throw _b_.TypeError.$factory("integer argument expected, got " +
89
$B.get_class(len).__name__)
90
}
91
if(["little", "big"].indexOf(byteorder) == -1){
92
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
93
}
94
var signed = kwargs.$string_dict["signed"] || false,
95
res = []
96
97
if(self < 0){
98
if(! signed){
99
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
100
}
101
self = Math.pow(256, len) + self
102
}
103
var value = self
104
while(true){
105
var quotient = Math.floor(value / 256),
106
rest = value - 256 * quotient
107
res.push(rest)
108
if(quotient == 0){
109
break
110
}
111
value = quotient
112
}
113
if(res.length > len){
114
throw _b_.OverflowError.$factory("int too big to convert")
115
}
116
if(byteorder == "big"){res = res.reverse()}
117
return {
118
__class__: _b_.bytes,
119
source: res
120
}
136
if(other === undefined){return self === int}
137
if(isinstance(other, int)){return self.valueOf() == other.valueOf()}
138
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
139
if(isinstance(other, _b_.complex)){
140
if(other.$imag != 0){return False}
141
return self.valueOf() == other.$real
155
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
156
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
184
if(fmt.sign !== undefined){
185
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
186
res = fmt.sign + res
187
}
188
}
207
for(var i = 0; i < nb; i++){
208
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
217
if(isinstance(other, int)){
218
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
219
return Math.floor(self / other)
221
if(isinstance(other, _b_.float)){
222
if(!other.valueOf()){
223
throw ZeroDivisionError.$factory("division by zero")
224
}
225
return Math.floor(self / other)
227
if(hasattr(other, "__rfloordiv__")){
228
return getattr(other, "__rfloordiv__")(self)
235
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
257
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
258
$B.long_int.$factory(other)))
260
var rlshift = getattr(other, "__rlshift__", None)
261
if(rlshift !== None){return rlshift(self)}
262
$err("<<", other)
267
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
268
if(isinstance(other, [int, _b_.float, bool])){
269
if(other === false){other = 0}
270
else if(other === true){other = 1}
271
if(other == 0){throw _b_.ZeroDivisionError.$factory(
275
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
276
$err("%", other)
290
if(isinstance(other, int)){
291
var res = self * other
292
if(res > $B.min_int && res < $B.max_int){return res}
293
else{
294
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
295
$B.long_int.$factory(other)))
296
}
306
return $B.make_complex(int.__mul__(self, other.$real),
307
int.__mul__(self, other.$imag))
312
var $temp = other.slice(0, other.length)
313
for(var i = 0; i < val; i++){res = res.concat($temp)}
314
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
317
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
318
$err("*", other)
324
if(cls === undefined){
325
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
326
}
335
case 0:
336
return int.$factory(1)
337
case 1:
338
return int.$factory(self.valueOf())
340
if(z !== undefined && z !== null){
341
// If z is provided, the algorithm is faster than computing
342
// self ** other then applying the modulo z
343
var res = (self % z + z) % z
350
var res = Math.pow(self.valueOf(), other.valueOf())
351
if(res > $B.min_int && res < $B.max_int){return res}
354
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
355
$B.long_int.$factory(other)))
369
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
370
$err("**", other)
381
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
382
$B.long_int.$factory(other)))
384
var rrshift = getattr(other, "__rrshift__", None)
385
if(rrshift !== None){return rrshift(self)}
390
if(typeof self == "number"){
391
if(int.$factory[attr] === undefined){
392
throw _b_.AttributeError.$factory(
393
"'int' object has no attribute '" + attr + "'")
395
throw _b_.AttributeError.$factory(
396
"'int' object attribute '" + attr + "' is read-only")
406
int.__truediv__ = function(self, other){
407
if(isinstance(other, int)){
408
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
409
if(other.__class__ === $B.long_int){
410
return new Number(self / parseInt(other.value))
411
}
412
return new Number(self / other)
414
if(isinstance(other, _b_.float)){
415
if(!other.valueOf()){
416
throw ZeroDivisionError.$factory("division by zero")
417
}
418
return new Number(self / other)
420
if(isinstance(other, _b_.complex)){
421
var cmod = other.$real * other.$real + other.$imag * other.$imag
422
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
423
return $B.make_complex(self * other.$real / cmod,
424
-self * other.$imag / cmod)
426
if(hasattr(other, "__rtruediv__")){
427
return getattr(other, "__rtruediv__")(self)
428
}
429
$err("/", other)
441
int.numerator = function(self){return self}
442
int.denominator = function(self){return int.$factory(1)}
443
int.imag = function(self){return int.$factory(0)}
444
int.real = function(self){return self}
450
var $op_func = function(self, other){
451
if(isinstance(other, int)) {
452
if(other.__class__ === $B.long_int){
453
return $B.long_int.__sub__($B.long_int.$factory(self),
454
$B.long_int.$factory(other))
456
if(self > $B.max_int32 || self < $B.min_int32 ||
457
other > $B.max_int32 || other < $B.min_int32){
458
return $B.long_int.__sub__($B.long_int.$factory(self),
459
$B.long_int.$factory(other))
463
if(isinstance(other, _b_.bool)){return self - other}
464
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
465
$err("-", other)
471
var opf = $op_func.replace(/-/gm, $op)
472
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
473
eval("int.__" + $ops[$op] + "__ = " + opf)
477
var $op_func = function(self, other){
478
if(isinstance(other, int)){
479
if(typeof other == "number"){
480
var res = self.valueOf() - other.valueOf()
481
if(res > $B.min_int && res < $B.max_int){return res}
482
else{return $B.long_int.__sub__($B.long_int.$factory(self),
483
$B.long_int.$factory(other))}
487
return $B.long_int.__sub__($B.long_int.$factory(self),
488
$B.long_int.$factory(other))
494
if(isinstance(other, _b_.complex)){
495
return $B.make_complex(self - other.$real, -other.$imag)
497
if(isinstance(other, _b_.bool)){
498
var bool_value = 0;
499
if(other.valueOf()){bool_value = 1}
500
return self - bool_value
505
var rsub = $B.$getattr(other, "__rsub__", None)
506
if(rsub !== None){return rsub(self)}
507
throw $err("-", other)
512
var opf = $op_func.replace(/-/gm, $op)
513
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
514
eval("int.__" + $ops[$op] + "__ = " + opf)
522
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
523
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
524
if(isinstance(other, _b_.bool)) {
532
var inv_op = $B.$getattr(other, "__lt__", None)
533
if(inv_op !== None){return inv_op(self)}
541
eval("int.__"+$B.$comps[$op] + "__ = " +
542
$comp_func.replace(/>/gm, $op).
543
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
544
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
550
var $valid_digits = function(base) {
551
var digits = ""
552
if(base === 0){return "0"}
553
if(base < 10){
569
if(typeof value == "number" &&
570
(base === undefined || base == 10)){return parseInt(value)}
572
if(base !== undefined){
573
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
574
throw TypeError.$factory(
575
"int() can't convert non-string with explicit base")
583
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
584
{"base": 10}, null, null),
585
value = $ns["x"],
586
base = $ns["base"]
588
if(isinstance(value, _b_.float) && base == 10){
589
if(value < $B.min_int || value > $B.max_int){
602
if(base == 10){
603
if(value < $B.min_int || value > $B.max_int){
604
return $B.long_int.$factory(value)
605
}
611
var res = parseInt(value, base)
612
if(value < $B.min_int || value > $B.max_int){
613
return $B.long_int.$factory(value, base)
614
}
619
if(value === true){return Number(1)}
620
if(value === false){return Number(0)}
621
if(value.__class__ === $B.long_int){
629
if(isinstance(value, _b_.str)){value = value.valueOf()}
630
if(typeof value == "string") {
631
var _value = value.trim() // remove leading/trailing whitespace
632
if(_value.length == 2 && base == 0 &&
633
(_value == "0b" || _value == "0o" || _value == "0x")){
634
throw _b_.ValueError.$factory("invalid value")
635
}
636
if(_value.length >2) {
637
var _pre = _value.substr(0, 2).toUpperCase()
638
if(base == 0){
639
if(_pre == "0B"){base = 2}
640
if(_pre == "0O"){base = 8}
641
if(_pre == "0X"){base = 16}
642
}
643
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
644
_value = _value.substr(2)
650
var _digits = $valid_digits(base),
651
_re = new RegExp("^[+-]?[" + _digits + "]" +
652
"[" + _digits + "_]*$", "i"),
653
match = _re.exec(_value)
654
if(match === null){
655
throw _b_.ValueError.$factory(
656
"invalid literal for int() with base " + base + ": '" +
657
_b_.str.$factory(value) + "'")
660
}
661
if(base <= 10 && ! isFinite(value)){
662
throw _b_.ValueError.$factory(
663
"invalid literal for int() with base " + base + ": '" +
664
_b_.str.$factory(value) + "'")
665
}
675
for(var i = 0; i < value.source.length; i++){
676
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
677
throw _b_.ValueError.$factory(
678
"invalid literal for int() with base " + base + ": " +
679
_b_.repr(value))
685
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
686
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
687
if(hasattr(value, "__trunc__")){
688
var res = getattr(value, "__trunc__")(),
689
int_func = _b_.getattr(res, "__int__", null)
690
if(int_func === null){
691
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
699
throw _b_.TypeError.$factory(
700
"int() argument must be a string, a bytes-like " +
701
"object or a number, not '" + $B.get_class(value).__name__ + "'")
710
if(obj === null || obj === undefined ){ return false}
711
switch(typeof obj){
712
case "boolean":
713
return obj
714
case "number":
715
case "string":
716
if(obj){return true}
717
return false
718
default:
719
try{return getattr(obj, "__bool__")()}
720
catch(err){
729
__module__: "builtins",
730
__mro__: [int, object],
731
__name__: "bool",
732
$is_class: true,
733
$native: true
734
}
740
bool.__and__ = function(self, other){
741
return $B.$bool(int.__and__(self, other))
744
bool.__eq__ = function(self,other){
745
return self ? $B.$bool(other) : !$B.$bool(other)
748
bool.__ne__ = function(self,other){
749
return self ? !$B.$bool(other) : $B.$bool(other)
752
bool.__ge__ = function(self,other){
753
return _b_.int.__ge__(bool.__hash__(self),other)
756
bool.__gt__ = function(self,other){
757
return _b_.int.__gt__(bool.__hash__(self),other)
799
bool.$factory = function(){
800
// Calls $B.$bool, which is used inside the generated JS code and skips
801
// arguments control.