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
Nov 8, 2018
Feb 11, 2018
Feb 11, 2018
Oct 27, 2018
Feb 11, 2018
Newer
100644
880 lines (781 sloc)
26.7 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)
274
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
275
$B.long_int.$factory(other)))
277
var rlshift = getattr(other, "__rlshift__", None)
278
if(rlshift !== None){return rlshift(self)}
279
$err("<<", other)
285
if(other.__class__ === $B.long_int){
286
return $B.long_int.__mod__($B.long_int.$factory(self), other)
287
}
290
if(other === false){other = 0}
291
else if(other === true){other = 1}
292
if(other == 0){throw _b_.ZeroDivisionError.$factory(
296
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
297
$err("%", other)
313
var res = self * other
314
if(res > $B.min_int && res < $B.max_int){return res}
315
else{
316
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
317
$B.long_int.$factory(other)))
318
}
328
return $B.make_complex(int.__mul__(self, other.$real),
329
int.__mul__(self, other.$imag))
334
var $temp = other.slice(0, other.length)
335
for(var i = 0; i < val; i++){res = res.concat($temp)}
336
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
339
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
340
$err("*", other)
346
if(cls === undefined){
347
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
348
}else if(! isinstance(cls, _b_.type)){
349
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
350
}
351
if(cls === int){return int.$factory(value)}
352
return {
353
__class__: cls,
354
$value: value || 0
361
if(isinstance(other, int)){
362
other = int_value(other)
363
switch(other.valueOf()) {
364
case 0:
365
return int.$factory(1)
366
case 1:
367
return int.$factory(self.valueOf())
369
if(z !== undefined && z !== null){
370
// If z is provided, the algorithm is faster than computing
371
// self ** other then applying the modulo z
372
if(z == 1){return 0}
373
var result = 1,
374
base = self % z,
375
exponent = other,
376
long_int = $B.long_int
377
while(exponent > 0){
378
if(exponent % 2 == 1){
379
if(result * base > $B.max_int){
380
result = long_int.__mul__(
381
long_int.$factory(result),
382
long_int.$factory(base))
383
result = long_int.__mod__(result, z)
384
}else{
385
result = (result * base) % z
386
}
387
}
388
exponent = exponent >> 1
389
if(base * base > $B.max_int){
390
base = long_int.__mul__(long_int.$factory(base),
391
long_int.$factory(base))
392
base = long_int.__mod__(base, z)
393
}else{
394
base = (base * base) % z
395
}
399
var res = Math.pow(self.valueOf(), other.valueOf())
400
if(res > $B.min_int && res < $B.max_int){return res}
403
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
404
$B.long_int.$factory(other)))
418
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
419
$err("**", other)
431
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
432
$B.long_int.$factory(other)))
434
var rrshift = getattr(other, "__rrshift__", None)
435
if(rrshift !== None){return rrshift(self)}
440
if(typeof self == "number"){
441
if(int.$factory[attr] === undefined){
442
throw _b_.AttributeError.$factory(
443
"'int' object has no attribute '" + attr + "'")
445
throw _b_.AttributeError.$factory(
446
"'int' object attribute '" + attr + "' is read-only")
459
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
460
if(other.__class__ === $B.long_int){
461
return new Number(self / parseInt(other.value))
462
}
463
return new Number(self / other)
465
if(isinstance(other, _b_.float)){
466
if(!other.valueOf()){
467
throw ZeroDivisionError.$factory("division by zero")
468
}
469
return new Number(self / other)
471
if(isinstance(other, _b_.complex)){
472
var cmod = other.$real * other.$real + other.$imag * other.$imag
473
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
474
return $B.make_complex(self * other.$real / cmod,
475
-self * other.$imag / cmod)
477
if(hasattr(other, "__rtruediv__")){
478
return getattr(other, "__rtruediv__")(self)
479
}
480
$err("/", other)
492
int.numerator = function(self){return self}
493
int.denominator = function(self){return int.$factory(1)}
494
int.imag = function(self){return int.$factory(0)}
495
int.real = function(self){return self}
501
var $op_func = function(self, other){
502
if(isinstance(other, int)) {
503
if(other.__class__ === $B.long_int){
504
return $B.long_int.__sub__($B.long_int.$factory(self),
505
$B.long_int.$factory(other))
508
if(self > $B.max_int32 || self < $B.min_int32 ||
509
other > $B.max_int32 || other < $B.min_int32){
510
return $B.long_int.__sub__($B.long_int.$factory(self),
511
$B.long_int.$factory(other))
515
if(isinstance(other, _b_.bool)){return self - other}
516
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
517
$err("-", other)
523
var opf = $op_func.replace(/-/gm, $op)
524
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
525
eval("int.__" + $ops[$op] + "__ = " + opf)
532
if(typeof other == "number"){
533
var res = self.valueOf() - other.valueOf()
534
if(res > $B.min_int && res < $B.max_int){return res}
535
else{return $B.long_int.__sub__($B.long_int.$factory(self),
536
$B.long_int.$factory(other))}
540
return $B.long_int.__sub__($B.long_int.$factory(self),
541
$B.long_int.$factory(other))
547
if(isinstance(other, _b_.complex)){
548
return $B.make_complex(self - other.$real, -other.$imag)
550
if(isinstance(other, _b_.bool)){
551
var bool_value = 0;
552
if(other.valueOf()){bool_value = 1}
553
return self - bool_value
558
var rsub = $B.$getattr(other, "__rsub__", None)
559
if(rsub !== None){return rsub(self)}
560
throw $err("-", other)
565
var opf = $op_func.replace(/-/gm, $op)
566
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
567
eval("int.__" + $ops[$op] + "__ = " + opf)
575
if(isinstance(other, int)){
576
other = int_value(other)
577
return self.valueOf() > other.valueOf()
578
}else if(isinstance(other, _b_.float)){
579
return self.valueOf() > other.valueOf()
580
}else if(isinstance(other, _b_.bool)) {
592
eval("int.__"+$B.$comps[$op] + "__ = " +
593
$comp_func.replace(/>/gm, $op).
594
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
595
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
601
var $valid_digits = function(base) {
602
var digits = ""
603
if(base === 0){return "0"}
604
if(base < 10){
620
if(typeof value == "number" &&
621
(base === undefined || base == 10)){return parseInt(value)}
623
if(base !== undefined){
624
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
625
throw TypeError.$factory(
626
"int() can't convert non-string with explicit base")
633
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
634
{"base": 10}, null, null),
635
value = $ns["x"],
636
base = $ns["base"]
638
if(isinstance(value, _b_.float) && base == 10){
639
if(value < $B.min_int || value > $B.max_int){
652
if(base == 10){
653
if(value < $B.min_int || value > $B.max_int){
654
return $B.long_int.$factory(value)
655
}
661
var res = parseInt(value, base)
662
if(value < $B.min_int || value > $B.max_int){
663
return $B.long_int.$factory(value, base)
664
}
669
if(value === true){return Number(1)}
670
if(value === false){return Number(0)}
671
if(value.__class__ === $B.long_int){
679
if(isinstance(value, _b_.str)){value = value.valueOf()}
680
if(typeof value == "string") {
681
var _value = value.trim() // remove leading/trailing whitespace
682
if(_value.length == 2 && base == 0 &&
683
(_value == "0b" || _value == "0o" || _value == "0x")){
684
throw _b_.ValueError.$factory("invalid value")
685
}
686
if(_value.length >2) {
687
var _pre = _value.substr(0, 2).toUpperCase()
688
if(base == 0){
689
if(_pre == "0B"){base = 2}
690
if(_pre == "0O"){base = 8}
691
if(_pre == "0X"){base = 16}
692
}
693
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
694
_value = _value.substr(2)
700
var _digits = $valid_digits(base),
701
_re = new RegExp("^[+-]?[" + _digits + "]" +
702
"[" + _digits + "_]*$", "i"),
703
match = _re.exec(_value)
704
if(match === null){
705
throw _b_.ValueError.$factory(
706
"invalid literal for int() with base " + base + ": '" +
707
_b_.str.$factory(value) + "'")
710
}
711
if(base <= 10 && ! isFinite(value)){
712
throw _b_.ValueError.$factory(
713
"invalid literal for int() with base " + base + ": '" +
714
_b_.str.$factory(value) + "'")
715
}
725
for(var i = 0; i < value.source.length; i++){
726
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
727
throw _b_.ValueError.$factory(
728
"invalid literal for int() with base " + base + ": " +
729
_b_.repr(value))
735
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
736
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
737
if(hasattr(value, "__trunc__")){
738
var res = getattr(value, "__trunc__")(),
739
int_func = _b_.getattr(res, "__int__", null)
740
if(int_func === null){
741
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
749
throw _b_.TypeError.$factory(
750
"int() argument must be a string, a bytes-like " +
751
"object or a number, not '" + $B.get_class(value).__name__ + "'")
760
if(obj === null || obj === undefined ){ return false}
761
switch(typeof obj){
762
case "boolean":
763
return obj
764
case "number":
765
case "string":
766
if(obj){return true}
767
return false
768
default:
769
var missing = {},
770
bool_func = $B.$getattr(obj, "__bool__", missing)
771
if(bool_func === missing){
783
__module__: "builtins",
784
__mro__: [int, object],
785
__name__: "bool",
786
$is_class: true,
787
$native: true
788
}
794
bool.__and__ = function(self, other){
795
return $B.$bool(int.__and__(self, other))
798
bool.__eq__ = function(self, other){
799
if(other === self){return True}
800
else if(typeof other == "number"){
801
return self ? other == 1 : other == 0
802
}else if(isinstance(other, _b_.int)){
803
return self ? other.$value == 1 : other.$value == 0
804
}else if(other instanceof Number){
805
return self ? other == 1 : other == 0
806
}else{
807
return false
808
}
809
//return self ? $B.$bool(other) : !$B.$bool(other)
816
bool.__ge__ = function(self,other){
817
return _b_.int.__ge__(bool.__hash__(self),other)
820
bool.__gt__ = function(self,other){
821
return _b_.int.__gt__(bool.__hash__(self),other)
852
if(_b_.dir(self).indexOf(attr) > -1){
853
var msg = "attribute '" + attr + "' of 'int' objects is not writable"
854
}else{
855
var msg = "'bool' object has no attribute '" + attr + "'"
856
}
857
throw _b_.AttributeError.$factory(msg)
868
bool.$factory = function(){
869
// Calls $B.$bool, which is used inside the generated JS code and skips
870
// arguments control.