Permalink
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
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
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
755 lines (654 sloc)
22.9 KB
8
function $err(op, other){
9
var msg = "unsupported operand type(s) for " + op +
10
": 'int' and '" + $B.get_class(other).__name__ + "'"
36
signed = $.signed,
37
_bytes, _len
38
if(isinstance(x, [_b_.bytes, _b_.bytearray])){
39
_bytes = x.source
40
_len = x.source.length
41
}else{
49
case "big":
50
var num = _bytes[_len - 1]
51
var _mult = 256
52
for(var i = _len - 2; i >= 0; i--){
53
// For operations, use the functions that can take or return
54
// big integers
55
num = $B.add($B.mul(_mult, _bytes[i]), num)
56
_mult = $B.mul(_mult,256)
57
}
58
if(! signed){return num}
59
if(_bytes[0] < 128){return num}
60
return $B.sub(num, _mult)
61
case "little":
62
var num = _bytes[0]
63
if(num >= 128){num = num - 256}
64
var _mult = 256
65
for(var i = 1; i < _len; i++){
66
num = $B.add($B.mul(_mult, _bytes[i]), num)
67
_mult = $B.mul(_mult, 256)
68
}
69
if(! signed){return num}
70
if(_bytes[_len - 1] < 128){return num}
71
return $B.sub(num, _mult)
95
if(other === undefined){return self === int}
96
if(isinstance(other, int)){return self.valueOf() == other.valueOf()}
97
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
98
if(isinstance(other, _b_.complex)){
99
if(other.$imag != 0){return False}
100
return self.valueOf() == other.$real
114
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
115
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
127
case "o":
128
return (fmt.alternate ? "0o" : "") + self.toString(8)
129
case "x":
130
return (fmt.alternate ? "0x" : "") + self.toString(16)
131
case "X":
132
return (fmt.alternate ? "0X" : "") + self.toString(16).toUpperCase()
133
case "n":
155
for(var i = 0; i < nb; i++){
156
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
165
if(isinstance(other, int)){
166
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
167
return Math.floor(self / other)
169
if(isinstance(other, _b_.float)){
170
if(!other.valueOf()){
171
throw ZeroDivisionError.$factory("division by zero")
172
}
173
return Math.floor(self / other)
175
if(hasattr(other, "__rfloordiv__")){
176
return getattr(other, "__rfloordiv__")(self)
183
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
205
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
206
$B.long_int.$factory(other)))
208
var rlshift = getattr(other, "__rlshift__", None)
209
if(rlshift !== None){return rlshift(self)}
210
$err("<<", other)
215
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
216
if(isinstance(other, [int, _b_.float, bool])){
217
if(other === false){other = 0}
218
else if(other === true){other = 1}
219
if(other == 0){throw _b_.ZeroDivisionError.$factory(
223
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
224
$err("%", other)
238
if(isinstance(other, int)){
239
var res = self * other
240
if(res > $B.min_int && res < $B.max_int){return res}
241
else{
242
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
243
$B.long_int.$factory(other)))
244
}
254
return $B.make_complex(int.__mul__(self, other.$real),
255
int.__mul__(self, other.$imag))
260
var $temp = other.slice(0, other.length)
261
for(var i = 0; i < val; i++){res = res.concat($temp)}
262
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
265
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
266
$err("*", other)
272
if(cls === undefined){
273
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
274
}
283
case 0:
284
return int.$factory(1)
285
case 1:
286
return int.$factory(self.valueOf())
288
if(z !== undefined && z !== null){
289
// If z is provided, the algorithm is faster than computing
290
// self ** other then applying the modulo z
291
var res = (self % z + z) % z
298
var res = Math.pow(self.valueOf(), other.valueOf())
299
if(res > $B.min_int && res < $B.max_int){return res}
302
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
303
$B.long_int.$factory(other)))
317
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
318
$err("**", other)
329
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
330
$B.long_int.$factory(other)))
332
var rrshift = getattr(other, "__rrshift__", None)
333
if(rrshift !== None){return rrshift(self)}
338
if(typeof self == "number"){
339
if(int.$factory[attr] === undefined){
340
throw _b_.AttributeError.$factory(
341
"'int' object has no attribute '" + attr + "'")
343
throw _b_.AttributeError.$factory(
344
"'int' object attribute '" + attr + "' is read-only")
354
int.__truediv__ = function(self, other){
355
if(isinstance(other, int)){
356
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
357
if(other.__class__ === $B.long_int){
358
return new Number(self / parseInt(other.value))
359
}
360
return new Number(self / other)
362
if(isinstance(other, _b_.float)){
363
if(!other.valueOf()){
364
throw ZeroDivisionError.$factory("division by zero")
365
}
366
return new Number(self / other)
368
if(isinstance(other, _b_.complex)){
369
var cmod = other.$real * other.$real + other.$imag * other.$imag
370
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
371
return $B.make_complex(self * other.$real / cmod,
372
-self * other.$imag / cmod)
374
if(hasattr(other, "__rtruediv__")){
375
return getattr(other, "__rtruediv__")(self)
376
}
377
$err("/", other)
389
int.numerator = function(self){return self}
390
int.denominator = function(self){return int.$factory(1)}
391
int.imag = function(self){return int.$factory(0)}
392
int.real = function(self){return self}
398
var $op_func = function(self, other){
399
if(isinstance(other, int)) {
400
if(other.__class__ === $B.long_int){
401
return $B.long_int.__sub__($B.long_int.$factory(self),
402
$B.long_int.$factory(other))
404
if(self > $B.max_int32 || self < $B.min_int32 ||
405
other > $B.max_int32 || other < $B.min_int32){
406
return $B.long_int.__sub__($B.long_int.$factory(self),
407
$B.long_int.$factory(other))
411
if(isinstance(other, _b_.bool)){return self - other}
412
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
413
$err("-", other)
419
var opf = $op_func.replace(/-/gm, $op)
420
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
421
eval("int.__" + $ops[$op] + "__ = " + opf)
425
var $op_func = function(self, other){
426
if(isinstance(other, int)){
427
if(typeof other == "number"){
428
var res = self.valueOf() - other.valueOf()
429
if(res > $B.min_int && res < $B.max_int){return res}
430
else{return $B.long_int.__sub__($B.long_int.$factory(self),
431
$B.long_int.$factory(other))}
435
return $B.long_int.__sub__($B.long_int.$factory(self),
436
$B.long_int.$factory(other))
442
if(isinstance(other, _b_.complex)){
443
return $B.make_complex(self - other.$real, -other.$imag)
445
if(isinstance(other, _b_.bool)){
446
var bool_value = 0;
447
if(other.valueOf()){bool_value = 1}
448
return self - bool_value
453
var rsub = $B.$getattr(other, "__rsub__", None)
454
if(rsub !== None){return rsub(self)}
455
throw $err("-", other)
460
var opf = $op_func.replace(/-/gm, $op)
461
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
462
eval("int.__" + $ops[$op] + "__ = " + opf)
470
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
471
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
472
if(isinstance(other, _b_.bool)) {
480
var inv_op = $B.$getattr(other, "__lt__", None)
481
if(inv_op !== None){return inv_op(self)}
489
eval("int.__"+$B.$comps[$op] + "__ = " +
490
$comp_func.replace(/>/gm, $op).
491
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
492
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
498
var $valid_digits = function(base) {
499
var digits = ""
500
if(base === 0){return "0"}
501
if(base < 10){
517
if(typeof value == "number" &&
518
(base === undefined || base == 10)){return parseInt(value)}
520
if(base !== undefined){
521
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
522
throw TypeError.$factory(
523
"int() can't convert non-string with explicit base")
531
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
532
{"base": 10}, null, null),
533
value = $ns["x"],
534
base = $ns["base"]
536
if(isinstance(value, _b_.float) && base == 10){
537
if(value < $B.min_int || value > $B.max_int){
550
if(base == 10){
551
if(value < $B.min_int || value > $B.max_int){
552
return $B.long_int.$factory(value)
553
}
559
var res = parseInt(value, base)
560
if(value < $B.min_int || value > $B.max_int){
561
return $B.long_int.$factory(value, base)
562
}
567
if(value === true){return Number(1)}
568
if(value === false){return Number(0)}
569
if(value.__class__ === $B.long_int){
577
if(isinstance(value, _b_.str)){value = value.valueOf()}
578
if(typeof value == "string") {
579
var _value = value.trim() // remove leading/trailing whitespace
580
if(_value.length == 2 && base == 0 &&
581
(_value == "0b" || _value == "0o" || _value == "0x")){
582
throw _b_.ValueError.$factory("invalid value")
583
}
584
if(_value.length >2) {
585
var _pre = _value.substr(0, 2).toUpperCase()
586
if(base == 0){
587
if(_pre == "0B"){base = 2}
588
if(_pre == "0O"){base = 8}
589
if(_pre == "0X"){base = 16}
590
}
591
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
592
_value = _value.substr(2)
593
}
594
}
595
var _digits = $valid_digits(base)
596
var _re = new RegExp("^[+-]?[" + _digits + "]+$", "i")
597
if(! _re.test(_value)){
598
throw _b_.ValueError.$factory(
599
"invalid literal for int() with base " + base + ": '" +
600
_b_.str.$factory(value) + "'")
601
}
602
if(base <= 10 && ! isFinite(value)){
603
throw _b_.ValueError.$factory(
604
"invalid literal for int() with base " + base + ": '" +
605
_b_.str.$factory(value) + "'")
606
}
607
var res = parseInt(_value, base)
608
if(res < $B.min_int || res > $B.max_int){
609
return $B.long_int.$factory(_value, base)
610
}
611
return res
616
for(var i = 0; i < value.source.length; i++){
617
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
618
throw _b_.ValueError.$factory(
619
"invalid literal for int() with base " + base + ": " +
620
_b_.repr(value))
626
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
627
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
628
if(hasattr(value, "__trunc__")){
629
var res = getattr(value, "__trunc__")(),
630
int_func = _b_.getattr(res, "__int__", null)
631
if(int_func === null){
632
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
640
throw _b_.TypeError.$factory(
641
"int() argument must be a string, a bytes-like " +
642
"object or a number, not '" + $B.get_class(value).__name__ + "'")
651
if(obj === null || obj === undefined ){ return false}
652
switch(typeof obj){
653
case "boolean":
654
return obj
655
case "number":
656
case "string":
657
if(obj){return true}
658
return false
659
default:
660
var ce = $B.current_exception
661
try{return getattr(obj, "__bool__")()}
662
catch(err){
663
$B.current_exception = ce
672
__module__: "builtins",
673
__mro__: [int, object],
674
__name__: "bool",
675
$is_class: true,
676
$native: true
677
}
683
bool.__and__ = function(self, other){
684
return $B.$bool(int.__and__(self, other))
687
bool.__eq__ = function(self,other){
688
return self ? $B.$bool(other) : !$B.$bool(other)
691
bool.__ne__ = function(self,other){
692
return self ? !$B.$bool(other) : $B.$bool(other)
695
bool.__ge__ = function(self,other){
696
return _b_.int.__ge__(bool.__hash__(self),other)
699
bool.__gt__ = function(self,other){
700
return _b_.int.__gt__(bool.__hash__(self),other)
742
bool.$factory = function(){
743
// Calls $B.$bool, which is used inside the generated JS code and skips
744
// arguments control.