Permalink
Jul 28, 2018
Dec 18, 2019
Dec 17, 2020
Dec 17, 2020
Apr 23, 2018
Dec 17, 2020
Apr 23, 2018
Dec 17, 2020
Dec 17, 2020
Jul 28, 2018
Feb 22, 2019
Mar 10, 2018
Mar 10, 2018
Feb 10, 2018
Aug 2, 2018
Mar 10, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Jul 28, 2018
Feb 20, 2020
Jul 28, 2018
May 19, 2017
Jul 28, 2018
Feb 20, 2020
Jul 28, 2018
Jun 30, 2020
Feb 15, 2020
Feb 11, 2018
Mar 10, 2018
Feb 22, 2019
Mar 10, 2018
Mar 23, 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
Nov 28, 2018
Mar 10, 2018
Mar 10, 2018
Nov 28, 2018
Jun 14, 2018
Jan 11, 2021
Mar 10, 2018
Oct 27, 2019
Oct 27, 2019
Oct 22, 2019
Jan 7, 2021
Oct 22, 2019
Oct 22, 2019
Feb 11, 2018
Newer
100644
913 lines (822 sloc)
26.6 KB
11
function int_value(obj){
12
// Instances of int subclasses that call int.__new__(cls, value)
39
int.as_integer_ratio = function(){
40
var $ = $B.args("as_integer_ratio", 1, {self:null}, ["self"],
41
arguments, {}, null, null)
42
return $B.$list([$.self, 1])
43
}
44
46
var $ = $B.args("from_bytes", 3,
47
{bytes:null, byteorder:null, signed:null},
48
["bytes", "byteorder", "signed"],
49
arguments, {signed: false}, null, null)
50
51
var x = $.bytes,
52
byteorder = $.byteorder,
53
signed = $.signed,
54
_bytes, _len
55
if(_b_.isinstance(x, [_b_.bytes, _b_.bytearray])){
56
_bytes = x.source
57
_len = x.source.length
58
}else{
59
_bytes = _b_.list.$factory(x)
60
_len = _bytes.length
61
for(var i = 0; i < _len; i++){
62
_b_.bytes.$factory([_bytes[i]])
63
}
64
}
65
if(byteorder == "big"){
66
_bytes.reverse()
67
}else if(byteorder != "little"){
68
throw _b_.ValueError.$factory(
69
"byteorder must be either 'little' or 'big'")
70
}
71
var num = _bytes[0]
72
if(signed && num >= 128){
73
num = num - 256
74
}
75
var _mult = 256
76
for(var i = 1; i < _len; i++){
77
num = $B.add($B.mul(_mult, _bytes[i]), num)
78
_mult = $B.mul(_mult, 256)
79
}
80
if(! signed){
81
return num
82
}
83
if(_bytes[_len - 1] < 128){
84
return num
85
}
86
return $B.sub(num, _mult)
91
{self: null, len: null, byteorder: null, signed: null},
92
["self", "len", "byteorder", "*", "signed"],
93
arguments, {signed: false}, null, null),
98
if(! _b_.isinstance(len, _b_.int)){
99
throw _b_.TypeError.$factory("integer argument expected, got " +
103
throw _b_.ValueError.$factory(
104
"byteorder must be either 'little' or 'big'")
105
}
106
107
if(_b_.isinstance(self, $B.long_int)){
108
return $B.long_int.to_bytes(self, len, byteorder, signed)
123
var quotient = Math.floor(value / 256),
124
rest = value - 256 * quotient
125
res.push(rest)
126
if(res.length > len){
127
throw _b_.OverflowError.$factory("int too big to convert")
131
while(res.length < len){
132
res.push(0)
133
}
134
if(byteorder == "big"){
135
res.reverse()
145
int.__add__ = function(self, other){
146
self = int_value(self)
147
if(_b_.isinstance(other, int)){
148
if(other.__class__ == $B.long_int){
149
return $B.long_int.__add__($B.long_int.$factory(self),
150
$B.long_int.$factory(other))
151
}
152
other = int_value(other)
153
var res = self + other
154
if(res > $B.min_int && res < $B.max_int){
155
return res
156
}else{
157
return $B.long_int.__add__($B.long_int.$factory(self),
158
$B.long_int.$factory(other))
159
}
160
}
161
return _b_.NotImplemented
162
}
163
164
int.__bool__ = function(self){
165
return int_value(self).valueOf() == 0 ? false : true
166
}
184
if(_b_.isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
185
if(_b_.isinstance(other, _b_.complex)){
198
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
199
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
227
if(fmt.sign !== undefined){
228
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
229
res = fmt.sign + res
230
}
231
}
250
for(var i = 0; i < nb; i++){
251
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
273
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
299
try{
300
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
301
$B.long_int.$factory(other)))
302
}catch(err){
303
console.log('err in lshift', self, other)
304
throw err
305
}
313
if(other.__class__ === $B.long_int){
314
return $B.long_int.__mod__($B.long_int.$factory(self), other)
315
}
318
if(other === false){other = 0}
319
else if(other === true){other = 1}
320
if(other == 0){throw _b_.ZeroDivisionError.$factory(
330
if(other.__class__ == $B.long_int){
331
return $B.long_int.__mul__($B.long_int.$factory(self),
332
$B.long_int.$factory(other))
333
}
339
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
340
$B.long_int.$factory(other)))
341
}
346
int.__ne__ = function(self, other){
347
var res = int.__eq__(self, other)
348
return (res === _b_.NotImplemented) ? res : !res
349
}
350
354
if(cls === undefined){
355
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
357
throw _b_.TypeError.$factory("int.__new__(X): X is not a type object")
358
}
359
if(cls === int){return int.$factory(value)}
360
return {
361
__class__: cls,
369
function extended_euclidean(a, b){
370
var d, u, v
371
if(b == 0){
372
return [a, 1, 0]
373
}else{
374
[d, u, v] = extended_euclidean(b, a % b)
375
return [d, v, u - Math.floor(a / b) * v]
376
}
377
}
378
384
other = int_value(other)
385
switch(other.valueOf()) {
386
case 0:
387
return int.$factory(1)
388
case 1:
389
return int.$factory(self.valueOf())
392
// If z is provided, the algorithm is faster than computing
393
// self ** other then applying the modulo z
394
if(z == 1){return 0}
395
var result = 1,
396
base = self % z,
397
exponent = other,
398
long_int = $B.long_int
399
if(exponent < 0){
400
var gcd, inv, _
401
[gcd, inv, _] = extended_euclidean(self, z)
402
if(gcd !== 1){
403
throw _b_.ValueError.$factory("not relative primes: " +
404
self + ' and ' + z)
405
}
406
return int.__pow__(inv, -exponent, z)
407
}
408
while(exponent > 0){
409
if(exponent % 2 == 1){
410
if(result * base > $B.max_int){
411
result = long_int.__mul__(
412
long_int.$factory(result),
413
long_int.$factory(base))
414
result = long_int.__mod__(result, z)
415
}else{
416
result = (result * base) % z
417
}
418
}
419
exponent = exponent >> 1
420
if(base * base > $B.max_int){
421
base = long_int.__mul__(long_int.$factory(base),
422
long_int.$factory(base))
423
base = long_int.__mod__(base, z)
424
}else{
425
base = (base * base) % z
426
}
431
if(res > $B.min_int && res < $B.max_int){
432
return other > 0 ? res : new Number(res)
433
}else if(res !== Infinity && !isFinite(res)){
434
return res
435
}else{
436
if($B.BigInt){
437
return {
438
__class__: $B.long_int,
439
value: ($B.BigInt(self) ** $B.BigInt(other)).toString(),
440
pos: true
441
}
442
}
443
return $B.long_int.__pow__($B.long_int.$from_int(self),
444
$B.long_int.$from_int(other))
460
var rpow = $B.$getattr(other, "__rpow__", _b_.None)
461
if(rpow !== _b_.None){
462
return rpow(self)
463
}
467
function __newobj__(){
468
// __newobj__ is called with a generator as only argument
469
var $ = $B.args('__newobj__', 0, {}, [], arguments, {}, 'args', null),
470
args = $.args
471
var res = args.slice(1)
472
res.__class__ = args[0]
473
return res
474
}
475
476
int.__reduce_ex__ = function(self){
477
return $B.fast_tuple([
478
__newobj__,
479
$B.fast_tuple([self.__class__ || int, int_value(self)]),
480
_b_.None,
481
_b_.None,
482
_b_.None])
483
}
484
491
self = int_value(self)
492
if(typeof other == "number" || _b_.isinstance(other, int)){
494
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
495
$B.long_int.$factory(other)))
501
if(typeof self == "number" || typeof self == "boolean"){
502
var cl_name = $B.class_name(self)
503
if(_b_.dir(self).indexOf(attr) > -1){
504
var msg = "attribute '" + attr + `' of '${cl_name}'` +
505
"objects is not writable"
518
int.__sub__ = function(self, other){
519
self = int_value(self)
520
if(_b_.isinstance(other, int)){
521
if(other.__class__ == $B.long_int){
522
return $B.long_int.__sub__($B.long_int.$factory(self),
523
$B.long_int.$factory(other))
524
}
525
other = int_value(other)
526
var res = self - other
527
if(res > $B.min_int && res < $B.max_int){
528
return res
529
}else{
530
return $B.long_int.__sub__($B.long_int.$factory(self),
531
$B.long_int.$factory(other))
532
}
533
}
534
return _b_.NotImplemented
535
}
536
543
if(other.__class__ === $B.long_int){
544
return new Number(self / parseInt(other.value))
545
}
546
return new Number(self / other)
552
s = _b_.bin(self)
553
s = $B.$getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
558
int.numerator = function(self){
559
return int_value(self)
560
}
561
int.denominator = function(self){
562
return int.$factory(1)
563
}
564
int.imag = function(self){
565
return int.$factory(0)
566
}
567
int.real = function(self){
568
return self
569
}
570
571
for(var attr of ['numerator', 'denominator', 'imag', 'real']){
572
int[attr].setter = (function(x){
573
return function(self, value){
574
throw _b_.AttributeError.$factory(`attribute '${x}' of ` +
575
`'${$B.class_name(self)}' objects is not writable`)
576
}
577
})(attr)
578
}
585
self = int_value(self)
586
if(typeof other == "number" || _b_.isinstance(other, int)){
587
if(other.__class__ === $B.long_int){
588
return $B.long_int.__sub__($B.long_int.$factory(self),
589
$B.long_int.$factory(other))
592
if(self > $B.max_int32 || self < $B.min_int32 ||
593
other > $B.max_int32 || other < $B.min_int32){
594
return $B.long_int.__sub__($B.long_int.$factory(self),
595
$B.long_int.$factory(other))
605
var opf = $op_func.replace(/-/gm, $op)
606
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
607
eval("int.__" + $ops[$op] + "__ = " + opf)
633
eval("int.__"+$B.$comps[$op] + "__ = " +
634
$comp_func.replace(/>/gm, $op).
635
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
636
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
640
var r_opnames = ["add", "sub", "mul", "truediv", "floordiv", "mod", "pow",
641
"lshift", "rshift", "and", "xor", "or", "divmod"]
642
643
for(var r_opname of r_opnames){
644
if(int["__r" + r_opname + "__"] === undefined &&
645
int['__' + r_opname + '__']){
646
int["__r" + r_opname + "__"] = (function(name){
647
return function(self, other){
648
if(_b_.isinstance(other, int)){
649
other = int_value(other)
650
return int["__" + name + "__"](other, self)
651
}
652
return _b_.NotImplemented
653
}
654
})(r_opname)
655
}
656
}
658
var $valid_digits = function(base) {
659
var digits = ""
660
if(base === 0){return "0"}
661
if(base < 10){
677
if(typeof value == "number" &&
678
(base === undefined || base == 10)){return parseInt(value)}
684
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
685
{"base": 10}, null, null),
686
value = $ns["x"],
687
base = $ns["base"]
708
if(base == 10){
709
if(value < $B.min_int || value > $B.max_int){
710
return $B.long_int.$factory(value)
711
}
717
var res = parseInt(value, base)
718
if(value < $B.min_int || value > $B.max_int){
719
return $B.long_int.$factory(value, base)
720
}
725
if(value === true){return Number(1)}
726
if(value === false){return Number(0)}
727
if(value.__class__ === $B.long_int){
734
function invalid(value, base){
735
throw _b_.ValueError.$factory("invalid literal for int() with base " +
736
base + ": '" + _b_.str.$factory(value) + "'")
737
}
742
if(typeof value == "string") {
743
var _value = value.trim() // remove leading/trailing whitespace
744
if(_value.length == 2 && base == 0 &&
745
(_value == "0b" || _value == "0o" || _value == "0x")){
746
throw _b_.ValueError.$factory("invalid value")
747
}
749
var _pre = _value.substr(0, 2).toUpperCase()
750
if(base == 0){
751
if(_pre == "0B"){base = 2}
752
if(_pre == "0O"){base = 8}
753
if(_pre == "0X"){base = 16}
754
}else if(_pre == "0X" && base != 16){invalid(_value, base)}
755
else if(_pre == "0O" && base != 8){invalid(_value, base)}
766
var _digits = $valid_digits(base),
767
_re = new RegExp("^[+-]?[" + _digits + "]" +
768
"[" + _digits + "_]*$", "i"),
769
match = _re.exec(_value)
796
throw _b_.TypeError.$factory(
797
"int() argument must be a string, a bytes-like " +
798
"object or a number, not '" + $B.class_name(value) + "'")
807
if(obj === null || obj === undefined ){ return false}
808
switch(typeof obj){
809
case "boolean":
810
return obj
811
case "number":
812
case "string":
813
if(obj){return true}
814
return false
815
default:
817
var klass = obj.__class__ || $B.get_class(obj),
818
missing = {},
819
bool_method = $B.$getattr(klass, "__bool__", missing)
820
if(bool_method === missing){
821
try{return _b_.len(obj) > 0}
824
var res = $B.$call(bool_method)(obj)
825
if(res !== true && res !== false){
826
throw _b_.TypeError.$factory("__bool__ should return " +
827
"bool, returned " + $B.class_name(res))
828
}
829
return res
843
$native: true,
844
$descriptors: {
845
"numerator": true,
846
"denominator": true,
847
"imag": true,
848
"real": true
849
}
853
if(_b_.isinstance(other, bool)){
854
return self && other
855
}else if(_b_.isinstance(other, int)){
856
return int.__and__(bool.__index__(self), int.__index__(other))
857
}
858
return _b_.NotImplemented
861
bool.__float__ = function(self){
862
return self ? new Number(1) : new Number(0)
863
}
864
873
if(_b_.isinstance(other, bool)){
874
return self || other
875
}else if(_b_.isinstance(other, int)){
876
return int.__or__(bool.__index__(self), int.__index__(other))
877
}
878
return _b_.NotImplemented
888
if(_b_.isinstance(other, bool)){
889
return self ^ other ? true : false
890
}else if(_b_.isinstance(other, int)){
891
return int.__xor__(bool.__index__(self), int.__index__(other))
892
}
893
return _b_.NotImplemented
896
bool.$factory = function(){
897
// Calls $B.$bool, which is used inside the generated JS code and skips
898
// arguments control.
904
bool.numerator = int.numerator
905
bool.denominator = int.denominator
906
bool.real = int.real
907
bool.imag = int.imag
908