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
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
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
858 lines (758 sloc)
26 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))
229
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
230
return Math.floor(self / other)
232
if(isinstance(other, _b_.float)){
233
if(!other.valueOf()){
234
throw ZeroDivisionError.$factory("division by zero")
235
}
236
return Math.floor(self / other)
238
if(hasattr(other, "__rfloordiv__")){
239
return getattr(other, "__rfloordiv__")(self)
246
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
269
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
270
$B.long_int.$factory(other)))
272
var rlshift = getattr(other, "__rlshift__", None)
273
if(rlshift !== None){return rlshift(self)}
274
$err("<<", other)
279
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
280
if(isinstance(other, [int, _b_.float, bool])){
282
if(other === false){other = 0}
283
else if(other === true){other = 1}
284
if(other == 0){throw _b_.ZeroDivisionError.$factory(
288
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
289
$err("%", other)
305
var res = self * other
306
if(res > $B.min_int && res < $B.max_int){return res}
307
else{
308
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
309
$B.long_int.$factory(other)))
310
}
320
return $B.make_complex(int.__mul__(self, other.$real),
321
int.__mul__(self, other.$imag))
326
var $temp = other.slice(0, other.length)
327
for(var i = 0; i < val; i++){res = res.concat($temp)}
328
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
331
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
332
$err("*", other)
338
if(cls === undefined){
339
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
340
}else if(! isinstance(cls, _b_.type)){
341
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
342
}
343
if(cls === int){return int.$factory(value)}
344
return {
345
__class__: cls,
346
$value: value || 0
353
if(isinstance(other, int)){
354
other = int_value(other)
355
switch(other.valueOf()) {
356
case 0:
357
return int.$factory(1)
358
case 1:
359
return int.$factory(self.valueOf())
361
if(z !== undefined && z !== null){
362
// If z is provided, the algorithm is faster than computing
363
// self ** other then applying the modulo z
364
if(z == 1){return 0}
365
var result = 1,
366
base = self % z,
367
exponent = other,
368
long_int = $B.long_int
369
while(exponent > 0){
370
if(exponent % 2 == 1){
371
if(result * base > $B.max_int){
372
result = long_int.__mul__(
373
long_int.$factory(result),
374
long_int.$factory(base))
375
result = long_int.__mod__(result, z)
376
}else{
377
result = (result * base) % z
378
}
379
}
380
exponent = exponent >> 1
381
if(base * base > $B.max_int){
382
base = long_int.__mul__(long_int.$factory(base),
383
long_int.$factory(base))
384
base = long_int.__mod__(base, z)
385
}else{
386
base = (base * base) % z
387
}
391
var res = Math.pow(self.valueOf(), other.valueOf())
392
if(res > $B.min_int && res < $B.max_int){return res}
395
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
396
$B.long_int.$factory(other)))
410
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
411
$err("**", other)
423
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
424
$B.long_int.$factory(other)))
426
var rrshift = getattr(other, "__rrshift__", None)
427
if(rrshift !== None){return rrshift(self)}
432
if(typeof self == "number"){
433
if(int.$factory[attr] === undefined){
434
throw _b_.AttributeError.$factory(
435
"'int' object has no attribute '" + attr + "'")
437
throw _b_.AttributeError.$factory(
438
"'int' object attribute '" + attr + "' is read-only")
451
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
452
if(other.__class__ === $B.long_int){
453
return new Number(self / parseInt(other.value))
454
}
455
return new Number(self / other)
457
if(isinstance(other, _b_.float)){
458
if(!other.valueOf()){
459
throw ZeroDivisionError.$factory("division by zero")
460
}
461
return new Number(self / other)
463
if(isinstance(other, _b_.complex)){
464
var cmod = other.$real * other.$real + other.$imag * other.$imag
465
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
466
return $B.make_complex(self * other.$real / cmod,
467
-self * other.$imag / cmod)
469
if(hasattr(other, "__rtruediv__")){
470
return getattr(other, "__rtruediv__")(self)
471
}
472
$err("/", other)
484
int.numerator = function(self){return self}
485
int.denominator = function(self){return int.$factory(1)}
486
int.imag = function(self){return int.$factory(0)}
487
int.real = function(self){return self}
493
var $op_func = function(self, other){
494
if(isinstance(other, int)) {
495
if(other.__class__ === $B.long_int){
496
return $B.long_int.__sub__($B.long_int.$factory(self),
497
$B.long_int.$factory(other))
500
if(self > $B.max_int32 || self < $B.min_int32 ||
501
other > $B.max_int32 || other < $B.min_int32){
502
return $B.long_int.__sub__($B.long_int.$factory(self),
503
$B.long_int.$factory(other))
507
if(isinstance(other, _b_.bool)){return self - other}
508
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
509
$err("-", other)
515
var opf = $op_func.replace(/-/gm, $op)
516
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
517
eval("int.__" + $ops[$op] + "__ = " + opf)
524
if(typeof other == "number"){
525
var res = self.valueOf() - other.valueOf()
526
if(res > $B.min_int && res < $B.max_int){return res}
527
else{return $B.long_int.__sub__($B.long_int.$factory(self),
528
$B.long_int.$factory(other))}
532
return $B.long_int.__sub__($B.long_int.$factory(self),
533
$B.long_int.$factory(other))
539
if(isinstance(other, _b_.complex)){
540
return $B.make_complex(self - other.$real, -other.$imag)
542
if(isinstance(other, _b_.bool)){
543
var bool_value = 0;
544
if(other.valueOf()){bool_value = 1}
545
return self - bool_value
550
var rsub = $B.$getattr(other, "__rsub__", None)
551
if(rsub !== None){return rsub(self)}
552
throw $err("-", other)
557
var opf = $op_func.replace(/-/gm, $op)
558
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
559
eval("int.__" + $ops[$op] + "__ = " + opf)
567
if(isinstance(other, int)){
568
other = int_value(other)
569
return self.valueOf() > other.valueOf()
570
}else if(isinstance(other, _b_.float)){
571
return self.valueOf() > other.valueOf()
572
}else if(isinstance(other, _b_.bool)) {
580
var inv_op = $B.$getattr(other, "__lt__", None)
581
if(inv_op !== None){return inv_op(self)}
589
eval("int.__"+$B.$comps[$op] + "__ = " +
590
$comp_func.replace(/>/gm, $op).
591
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
592
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
598
var $valid_digits = function(base) {
599
var digits = ""
600
if(base === 0){return "0"}
601
if(base < 10){
617
if(typeof value == "number" &&
618
(base === undefined || base == 10)){return parseInt(value)}
620
if(base !== undefined){
621
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
622
throw TypeError.$factory(
623
"int() can't convert non-string with explicit base")
630
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
631
{"base": 10}, null, null),
632
value = $ns["x"],
633
base = $ns["base"]
635
if(isinstance(value, _b_.float) && base == 10){
636
if(value < $B.min_int || value > $B.max_int){
649
if(base == 10){
650
if(value < $B.min_int || value > $B.max_int){
651
return $B.long_int.$factory(value)
652
}
658
var res = parseInt(value, base)
659
if(value < $B.min_int || value > $B.max_int){
660
return $B.long_int.$factory(value, base)
661
}
666
if(value === true){return Number(1)}
667
if(value === false){return Number(0)}
668
if(value.__class__ === $B.long_int){
676
if(isinstance(value, _b_.str)){value = value.valueOf()}
677
if(typeof value == "string") {
678
var _value = value.trim() // remove leading/trailing whitespace
679
if(_value.length == 2 && base == 0 &&
680
(_value == "0b" || _value == "0o" || _value == "0x")){
681
throw _b_.ValueError.$factory("invalid value")
682
}
683
if(_value.length >2) {
684
var _pre = _value.substr(0, 2).toUpperCase()
685
if(base == 0){
686
if(_pre == "0B"){base = 2}
687
if(_pre == "0O"){base = 8}
688
if(_pre == "0X"){base = 16}
689
}
690
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
691
_value = _value.substr(2)
697
var _digits = $valid_digits(base),
698
_re = new RegExp("^[+-]?[" + _digits + "]" +
699
"[" + _digits + "_]*$", "i"),
700
match = _re.exec(_value)
701
if(match === null){
702
throw _b_.ValueError.$factory(
703
"invalid literal for int() with base " + base + ": '" +
704
_b_.str.$factory(value) + "'")
707
}
708
if(base <= 10 && ! isFinite(value)){
709
throw _b_.ValueError.$factory(
710
"invalid literal for int() with base " + base + ": '" +
711
_b_.str.$factory(value) + "'")
712
}
722
for(var i = 0; i < value.source.length; i++){
723
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
724
throw _b_.ValueError.$factory(
725
"invalid literal for int() with base " + base + ": " +
726
_b_.repr(value))
732
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
733
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
734
if(hasattr(value, "__trunc__")){
735
var res = getattr(value, "__trunc__")(),
736
int_func = _b_.getattr(res, "__int__", null)
737
if(int_func === null){
738
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
746
throw _b_.TypeError.$factory(
747
"int() argument must be a string, a bytes-like " +
748
"object or a number, not '" + $B.get_class(value).__name__ + "'")
757
if(obj === null || obj === undefined ){ return false}
758
switch(typeof obj){
759
case "boolean":
760
return obj
761
case "number":
762
case "string":
763
if(obj){return true}
764
return false
765
default:
766
try{return getattr(obj, "__bool__")()}
767
catch(err){
776
__module__: "builtins",
777
__mro__: [int, object],
778
__name__: "bool",
779
$is_class: true,
780
$native: true
781
}
787
bool.__and__ = function(self, other){
788
return $B.$bool(int.__and__(self, other))
791
bool.__eq__ = function(self,other){
792
return self ? $B.$bool(other) : !$B.$bool(other)
795
bool.__ne__ = function(self,other){
796
return self ? !$B.$bool(other) : $B.$bool(other)
799
bool.__ge__ = function(self,other){
800
return _b_.int.__ge__(bool.__hash__(self),other)
803
bool.__gt__ = function(self,other){
804
return _b_.int.__gt__(bool.__hash__(self),other)
846
bool.$factory = function(){
847
// Calls $B.$bool, which is used inside the generated JS code and skips
848
// arguments control.