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
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
864 lines (764 sloc)
26.2 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)) {
586
var inv_op = $B.$getattr(other, "__lt__", None)
587
if(inv_op !== None){return inv_op(self)}
595
eval("int.__"+$B.$comps[$op] + "__ = " +
596
$comp_func.replace(/>/gm, $op).
597
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
598
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
604
var $valid_digits = function(base) {
605
var digits = ""
606
if(base === 0){return "0"}
607
if(base < 10){
623
if(typeof value == "number" &&
624
(base === undefined || base == 10)){return parseInt(value)}
626
if(base !== undefined){
627
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
628
throw TypeError.$factory(
629
"int() can't convert non-string with explicit base")
636
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
637
{"base": 10}, null, null),
638
value = $ns["x"],
639
base = $ns["base"]
641
if(isinstance(value, _b_.float) && base == 10){
642
if(value < $B.min_int || value > $B.max_int){
655
if(base == 10){
656
if(value < $B.min_int || value > $B.max_int){
657
return $B.long_int.$factory(value)
658
}
664
var res = parseInt(value, base)
665
if(value < $B.min_int || value > $B.max_int){
666
return $B.long_int.$factory(value, base)
667
}
672
if(value === true){return Number(1)}
673
if(value === false){return Number(0)}
674
if(value.__class__ === $B.long_int){
682
if(isinstance(value, _b_.str)){value = value.valueOf()}
683
if(typeof value == "string") {
684
var _value = value.trim() // remove leading/trailing whitespace
685
if(_value.length == 2 && base == 0 &&
686
(_value == "0b" || _value == "0o" || _value == "0x")){
687
throw _b_.ValueError.$factory("invalid value")
688
}
689
if(_value.length >2) {
690
var _pre = _value.substr(0, 2).toUpperCase()
691
if(base == 0){
692
if(_pre == "0B"){base = 2}
693
if(_pre == "0O"){base = 8}
694
if(_pre == "0X"){base = 16}
695
}
696
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
697
_value = _value.substr(2)
703
var _digits = $valid_digits(base),
704
_re = new RegExp("^[+-]?[" + _digits + "]" +
705
"[" + _digits + "_]*$", "i"),
706
match = _re.exec(_value)
707
if(match === null){
708
throw _b_.ValueError.$factory(
709
"invalid literal for int() with base " + base + ": '" +
710
_b_.str.$factory(value) + "'")
713
}
714
if(base <= 10 && ! isFinite(value)){
715
throw _b_.ValueError.$factory(
716
"invalid literal for int() with base " + base + ": '" +
717
_b_.str.$factory(value) + "'")
718
}
728
for(var i = 0; i < value.source.length; i++){
729
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
730
throw _b_.ValueError.$factory(
731
"invalid literal for int() with base " + base + ": " +
732
_b_.repr(value))
738
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
739
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
740
if(hasattr(value, "__trunc__")){
741
var res = getattr(value, "__trunc__")(),
742
int_func = _b_.getattr(res, "__int__", null)
743
if(int_func === null){
744
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
752
throw _b_.TypeError.$factory(
753
"int() argument must be a string, a bytes-like " +
754
"object or a number, not '" + $B.get_class(value).__name__ + "'")
763
if(obj === null || obj === undefined ){ return false}
764
switch(typeof obj){
765
case "boolean":
766
return obj
767
case "number":
768
case "string":
769
if(obj){return true}
770
return false
771
default:
772
try{return getattr(obj, "__bool__")()}
773
catch(err){
782
__module__: "builtins",
783
__mro__: [int, object],
784
__name__: "bool",
785
$is_class: true,
786
$native: true
787
}
793
bool.__and__ = function(self, other){
794
return $B.$bool(int.__and__(self, other))
797
bool.__eq__ = function(self,other){
798
return self ? $B.$bool(other) : !$B.$bool(other)
801
bool.__ne__ = function(self,other){
802
return self ? !$B.$bool(other) : $B.$bool(other)
805
bool.__ge__ = function(self,other){
806
return _b_.int.__ge__(bool.__hash__(self),other)
809
bool.__gt__ = function(self,other){
810
return _b_.int.__gt__(bool.__hash__(self),other)
852
bool.$factory = function(){
853
// Calls $B.$bool, which is used inside the generated JS code and skips
854
// arguments control.