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
Mar 10, 2018
Jun 14, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Mar 10, 2018
Oct 15, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Newer
100644
863 lines (764 sloc)
26.1 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
43
signed = $.signed,
44
_bytes, _len
45
if(isinstance(x, [_b_.bytes, _b_.bytearray])){
46
_bytes = x.source
47
_len = x.source.length
48
}else{
56
case "big":
57
var num = _bytes[_len - 1]
58
var _mult = 256
59
for(var i = _len - 2; i >= 0; i--){
60
// For operations, use the functions that can take or return
61
// big integers
62
num = $B.add($B.mul(_mult, _bytes[i]), num)
63
_mult = $B.mul(_mult,256)
64
}
65
if(! signed){return num}
66
if(_bytes[0] < 128){return num}
67
return $B.sub(num, _mult)
68
case "little":
69
var num = _bytes[0]
70
if(num >= 128){num = num - 256}
71
var _mult = 256
72
for(var i = 1; i < _len; i++){
73
num = $B.add($B.mul(_mult, _bytes[i]), num)
74
_mult = $B.mul(_mult, 256)
75
}
76
if(! signed){return num}
77
if(_bytes[_len - 1] < 128){return num}
78
return $B.sub(num, _mult)
84
int.to_bytes = function(){
85
var $ = $B.args("to_bytes", 3,
86
{self: null, len: null, byteorder: null},
87
["self", "len", "byteorder"],
88
arguments, {}, "args", "kw"),
89
self = $.self,
90
len = $.len,
91
byteorder = $.byteorder,
92
kwargs = $.kw
93
if(! _b_.isinstance(len, _b_.int)){
94
throw _b_.TypeError.$factory("integer argument expected, got " +
95
$B.get_class(len).__name__)
96
}
97
if(["little", "big"].indexOf(byteorder) == -1){
98
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
99
}
100
var signed = kwargs.$string_dict["signed"] || false,
101
res = []
102
103
if(self < 0){
104
if(! signed){
105
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
106
}
107
self = Math.pow(256, len) + self
108
}
109
var value = self
110
while(true){
111
var quotient = Math.floor(value / 256),
112
rest = value - 256 * quotient
113
res.push(rest)
114
if(quotient == 0){
115
break
116
}
117
value = quotient
118
}
119
if(res.length > len){
120
throw _b_.OverflowError.$factory("int too big to convert")
121
}
122
if(byteorder == "big"){res = res.reverse()}
123
return {
124
__class__: _b_.bytes,
125
source: res
126
}
134
int.__bool__ = function(self){
135
return int_value(self).valueOf() == 0 ? false : true
136
}
145
if(isinstance(other, int)){
146
return self.valueOf() == int_value(other).valueOf()
147
}
148
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
149
if(isinstance(other, _b_.complex)){
150
if(other.$imag != 0){return False}
151
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)
272
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
273
$B.long_int.$factory(other)))
275
var rlshift = getattr(other, "__rlshift__", None)
276
if(rlshift !== None){return rlshift(self)}
277
$err("<<", other)
283
if(other.__class__ === $B.long_int){
284
return $B.long_int.__mod__($B.long_int.$factory(self), other)
285
}
288
if(other === false){other = 0}
289
else if(other === true){other = 1}
290
if(other == 0){throw _b_.ZeroDivisionError.$factory(
294
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
295
$err("%", other)
311
var res = self * other
312
if(res > $B.min_int && res < $B.max_int){return res}
313
else{
314
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
315
$B.long_int.$factory(other)))
316
}
326
return $B.make_complex(int.__mul__(self, other.$real),
327
int.__mul__(self, other.$imag))
332
var $temp = other.slice(0, other.length)
333
for(var i = 0; i < val; i++){res = res.concat($temp)}
334
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
337
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
338
$err("*", other)
344
if(cls === undefined){
345
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
346
}else if(! isinstance(cls, _b_.type)){
347
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
348
}
349
if(cls === int){return int.$factory(value)}
350
return {
351
__class__: cls,
352
$value: value || 0
359
if(isinstance(other, int)){
360
other = int_value(other)
361
switch(other.valueOf()) {
362
case 0:
363
return int.$factory(1)
364
case 1:
365
return int.$factory(self.valueOf())
367
if(z !== undefined && z !== null){
368
// If z is provided, the algorithm is faster than computing
369
// self ** other then applying the modulo z
370
if(z == 1){return 0}
371
var result = 1,
372
base = self % z,
373
exponent = other,
374
long_int = $B.long_int
375
while(exponent > 0){
376
if(exponent % 2 == 1){
377
if(result * base > $B.max_int){
378
result = long_int.__mul__(
379
long_int.$factory(result),
380
long_int.$factory(base))
381
result = long_int.__mod__(result, z)
382
}else{
383
result = (result * base) % z
384
}
385
}
386
exponent = exponent >> 1
387
if(base * base > $B.max_int){
388
base = long_int.__mul__(long_int.$factory(base),
389
long_int.$factory(base))
390
base = long_int.__mod__(base, z)
391
}else{
392
base = (base * base) % z
393
}
397
var res = Math.pow(self.valueOf(), other.valueOf())
398
if(res > $B.min_int && res < $B.max_int){return res}
401
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
402
$B.long_int.$factory(other)))
416
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
417
$err("**", other)
429
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
430
$B.long_int.$factory(other)))
432
var rrshift = getattr(other, "__rrshift__", None)
433
if(rrshift !== None){return rrshift(self)}
438
if(typeof self == "number"){
439
if(int.$factory[attr] === undefined){
440
throw _b_.AttributeError.$factory(
441
"'int' object has no attribute '" + attr + "'")
443
throw _b_.AttributeError.$factory(
444
"'int' object attribute '" + attr + "' is read-only")
457
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
458
if(other.__class__ === $B.long_int){
459
return new Number(self / parseInt(other.value))
460
}
461
return new Number(self / other)
463
if(isinstance(other, _b_.float)){
464
if(!other.valueOf()){
465
throw ZeroDivisionError.$factory("division by zero")
466
}
467
return new Number(self / other)
469
if(isinstance(other, _b_.complex)){
470
var cmod = other.$real * other.$real + other.$imag * other.$imag
471
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
472
return $B.make_complex(self * other.$real / cmod,
473
-self * other.$imag / cmod)
475
if(hasattr(other, "__rtruediv__")){
476
return getattr(other, "__rtruediv__")(self)
477
}
478
$err("/", other)
490
int.numerator = function(self){return self}
491
int.denominator = function(self){return int.$factory(1)}
492
int.imag = function(self){return int.$factory(0)}
493
int.real = function(self){return self}
499
var $op_func = function(self, other){
500
if(isinstance(other, int)) {
501
if(other.__class__ === $B.long_int){
502
return $B.long_int.__sub__($B.long_int.$factory(self),
503
$B.long_int.$factory(other))
506
if(self > $B.max_int32 || self < $B.min_int32 ||
507
other > $B.max_int32 || other < $B.min_int32){
508
return $B.long_int.__sub__($B.long_int.$factory(self),
509
$B.long_int.$factory(other))
513
if(isinstance(other, _b_.bool)){return self - other}
514
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
515
$err("-", other)
521
var opf = $op_func.replace(/-/gm, $op)
522
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
523
eval("int.__" + $ops[$op] + "__ = " + opf)
530
if(typeof other == "number"){
531
var res = self.valueOf() - other.valueOf()
532
if(res > $B.min_int && res < $B.max_int){return res}
533
else{return $B.long_int.__sub__($B.long_int.$factory(self),
534
$B.long_int.$factory(other))}
538
return $B.long_int.__sub__($B.long_int.$factory(self),
539
$B.long_int.$factory(other))
545
if(isinstance(other, _b_.complex)){
546
return $B.make_complex(self - other.$real, -other.$imag)
548
if(isinstance(other, _b_.bool)){
549
var bool_value = 0;
550
if(other.valueOf()){bool_value = 1}
551
return self - bool_value
556
var rsub = $B.$getattr(other, "__rsub__", None)
557
if(rsub !== None){return rsub(self)}
558
throw $err("-", other)
563
var opf = $op_func.replace(/-/gm, $op)
564
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
565
eval("int.__" + $ops[$op] + "__ = " + opf)
573
if(isinstance(other, int)){
574
other = int_value(other)
575
return self.valueOf() > other.valueOf()
576
}else if(isinstance(other, _b_.float)){
577
return self.valueOf() > other.valueOf()
578
}else if(isinstance(other, _b_.bool)) {
590
eval("int.__"+$B.$comps[$op] + "__ = " +
591
$comp_func.replace(/>/gm, $op).
592
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
593
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
599
var $valid_digits = function(base) {
600
var digits = ""
601
if(base === 0){return "0"}
602
if(base < 10){
618
if(typeof value == "number" &&
619
(base === undefined || base == 10)){return parseInt(value)}
621
if(base !== undefined){
622
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
623
throw TypeError.$factory(
624
"int() can't convert non-string with explicit base")
631
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
632
{"base": 10}, null, null),
633
value = $ns["x"],
634
base = $ns["base"]
636
if(isinstance(value, _b_.float) && base == 10){
637
if(value < $B.min_int || value > $B.max_int){
650
if(base == 10){
651
if(value < $B.min_int || value > $B.max_int){
652
return $B.long_int.$factory(value)
653
}
659
var res = parseInt(value, base)
660
if(value < $B.min_int || value > $B.max_int){
661
return $B.long_int.$factory(value, base)
662
}
667
if(value === true){return Number(1)}
668
if(value === false){return Number(0)}
669
if(value.__class__ === $B.long_int){
677
if(isinstance(value, _b_.str)){value = value.valueOf()}
678
if(typeof value == "string") {
679
var _value = value.trim() // remove leading/trailing whitespace
680
if(_value.length == 2 && base == 0 &&
681
(_value == "0b" || _value == "0o" || _value == "0x")){
682
throw _b_.ValueError.$factory("invalid value")
683
}
684
if(_value.length >2) {
685
var _pre = _value.substr(0, 2).toUpperCase()
686
if(base == 0){
687
if(_pre == "0B"){base = 2}
688
if(_pre == "0O"){base = 8}
689
if(_pre == "0X"){base = 16}
690
}
691
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
692
_value = _value.substr(2)
698
var _digits = $valid_digits(base),
699
_re = new RegExp("^[+-]?[" + _digits + "]" +
700
"[" + _digits + "_]*$", "i"),
701
match = _re.exec(_value)
702
if(match === null){
703
throw _b_.ValueError.$factory(
704
"invalid literal for int() with base " + base + ": '" +
705
_b_.str.$factory(value) + "'")
708
}
709
if(base <= 10 && ! isFinite(value)){
710
throw _b_.ValueError.$factory(
711
"invalid literal for int() with base " + base + ": '" +
712
_b_.str.$factory(value) + "'")
713
}
723
for(var i = 0; i < value.source.length; i++){
724
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
725
throw _b_.ValueError.$factory(
726
"invalid literal for int() with base " + base + ": " +
727
_b_.repr(value))
733
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
734
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
735
if(hasattr(value, "__trunc__")){
736
var res = getattr(value, "__trunc__")(),
737
int_func = _b_.getattr(res, "__int__", null)
738
if(int_func === null){
739
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
747
throw _b_.TypeError.$factory(
748
"int() argument must be a string, a bytes-like " +
749
"object or a number, not '" + $B.get_class(value).__name__ + "'")
758
if(obj === null || obj === undefined ){ return false}
759
switch(typeof obj){
760
case "boolean":
761
return obj
762
case "number":
763
case "string":
764
if(obj){return true}
765
return false
766
default:
767
var missing = {},
768
bool_func = $B.$getattr(obj, "__bool__", missing)
769
if(bool_func === missing){
781
__module__: "builtins",
782
__mro__: [int, object],
783
__name__: "bool",
784
$is_class: true,
785
$native: true
786
}
792
bool.__and__ = function(self, other){
793
return $B.$bool(int.__and__(self, other))
796
bool.__eq__ = function(self,other){
797
return self ? $B.$bool(other) : !$B.$bool(other)
800
bool.__ne__ = function(self,other){
801
return self ? !$B.$bool(other) : $B.$bool(other)
804
bool.__ge__ = function(self,other){
805
return _b_.int.__ge__(bool.__hash__(self),other)
808
bool.__gt__ = function(self,other){
809
return _b_.int.__gt__(bool.__hash__(self),other)
851
bool.$factory = function(){
852
// Calls $B.$bool, which is used inside the generated JS code and skips
853
// arguments control.